2013-01-31 29 views
0

我有小C++模塊: (此代碼是可怕的,但它僅僅是原型)C++的擴展與提升,Python不工作

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代碼 我嘗試編譯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抱歉我寫錯了。英文不是我的母語

回答

2

C++損壞的名稱(函數,類,結構,聯合等)(請參閱here)。

您可以找到由運行實際出口的共享對象的名稱:

/usr/bin/nm libtest.so.1.0 

在你的情況下,work()功能可能正在錯位,以_Z4workv()

+0

你說得對,/ usr/bin/nm libtest.so.1.0 | grep work => ... T _Z4work。感謝您的支持 –

+0

不要忘記接受答案:) – isedev