使用boost的python的c ++扩展不起作用

时间:2013-01-31 15:39:33

标签: c++ python boost

我有一个小的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

OS:Ubuntu的 有出路吗? PS对不起我的写作错误。英语不是我的母语

1 个答案:

答案 0 :(得分:2)

C ++破坏名称(函数,类,结构,联合......等)(见here)。

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

/usr/bin/nm libtest.so.1.0

在您的情况下,work()函数可能会被修改为_Z4workv()