0

我有一个小的 c++ 模块:(这段代码很糟糕,但它只是原型)

librecv.cpp:

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <stdio.h>
#include <cstdlib>
typedef boost::interprocess::allocator<float, boost::interprocess::managed_shared_memory::segment_manager>  ShmemAllocator;
typedef boost::interprocess::vector<float, ShmemAllocator> DataVector;
void _init()
{
    printf("Initialization of shared object\n");
}
void _fini()
{
    printf("Clean-up of shared object\n");
}
void work()
{
    boost::interprocess::managed_shared_memory segment(boost::interprocess::open_only, "MySharedMemory");
    DataVector *myvector = segment.find<DataVector>("MyVector").first;
    for(int i = 0; i < 100; ++i)  //Insert data in the vector
    {
        printf("%f ", (float)myvector->at(i));
    }
};

我想在我的 python 代码中使用函数“work” 我尝试编译 C++ 库:(我可能错过了什么吗?)

g++ -fPIC -c librecv.cpp  -lboost_system -lrt 
g++ -shared -o libtest.so.1.0 -lc librecv.o

Python代码:

from ctypes import *
libtest = cdll.LoadLibrary('./libtest.so.1.0') #python should call function "_init"  here, but nothing happens
libtest.work
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/ctypes/__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: ./libtest.so.1.0: undefined symbol: work

操作系统:Ubuntu 有出路吗?PS对不起我的写作错误。非我母语的英语

4

1 回答 1

2

C++ 破坏名称(函数、类、结构、联合等)(参见此处)。

您可以通过运行以下命令找到在共享对象中实际导出的名称:

/usr/bin/nm libtest.so.1.0

在您的情况下,该work()功能可能被修改为_Z4workv().

于 2013-01-31T15:41:39.683 回答