Daiver
Daiver

Reputation: 1508

c++ extension for python with boost doesn't works

I have small c++ module: (this code is terrible, but it is only prototype)

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));
    }
};

I want to use function "work" in my python code I try to compile C++ library: (may i miss something here?)

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

Python code:

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 Is there a way out? PS sorry for my writing mistakes. English in not my native language

Upvotes: 0

Views: 196

Answers (1)

isedev
isedev

Reputation: 19611

C++ mangles names (of functions, classes, structures, unions, etc...) (see here).

You can find the names that are actually exported in the shared object by running:

/usr/bin/nm libtest.so.1.0

In your case, the work() function is probably being mangled to _Z4workv().

Upvotes: 2

Related Questions