views:

25

answers:

1

I have compiled a library using cmake add_library(object3d SHARED some_file.h some_file.cpp).

After compilation, I get a file: libobject3d.so

I would like to call a function in this library. This function definition in some_file.h is:

void ComputeGeometryImage(char * input_image, int geometry_image_size, float * output);

I did check that this method exists in my library by doing:

nm libobject3d.so
0000000000202058 d DW.ref.__gxx_personality_v0
0000000000201d40 a _DYNAMIC
0000000000201fe8 a _GLOBAL_OFFSET_TABLE_
0000000000000cbd t _GLOBAL__I_export_object3d_lib.cpp
                 w _Jv_RegisterClasses
0000000000000bdc T _Z20ComputeGeometryImagePciPf
0000000000000c75 t _Z41__static_initialization_and_destruction_0ii
                 U _Z7load_3dPKci
0000000000000cd2 W _ZN7Image2DIfE10get_accessEv
0000000000000ce8 W _ZN7Image2DIfED1Ev
                 U _ZNK8Object3D18convert_to_Image2DEi
                 U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
                 U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
0000000000202070 b _ZStL8__ioinit
                 U _ZdaPv@@GLIBCXX_3.4
                 U _ZdlPv@@GLIBCXX_3.4
0000000000201d20 d __CTOR_END__
0000000000201d10 d __CTOR_LIST__
0000000000201d30 d __DTOR_END__
0000000000201d28 d __DTOR_LIST__
0000000000000e70 r __FRAME_END__
0000000000201d38 d __JCR_END__
0000000000201d38 d __JCR_LIST__
0000000000202060 A __bss_start
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000d30 t __do_global_ctors_aux
0000000000000b30 t __do_global_dtors_aux
0000000000202050 d __dso_handle
                 w __gmon_start__
                 U __gxx_personality_v0@@CXXABI_1.3
0000000000202060 A _edata
0000000000202078 A _end
0000000000000d68 T _fini
0000000000000a40 T _init
0000000000000b10 t call_gmon_start
0000000000202060 b completed.7382
0000000000202068 b dtor_idx.7384
0000000000000bb0 t frame_dummy
                 U memcpy@@GLIBC_2.2.5

However, when I try to load this library into ctypes:

lib = np.ctypeslib.load_library('libobject3d.so', '.')

This lib object does not have the object ComputeGeometryImage. That is lib.ComputeGeometryImage does not exist.

Is this a problem of compiling my library? How do I expose this method from C++ into ctypes?

+3  A: 

Your C++ compiler mangles the function name to _Z20ComputeGeometryImagePciPf. You need to tell your compiler to stop mangling the funtion name. In some_file.h:

extern "C" void ComputeGeometryImage(char * input_image, 
                                     int geometry_image_size, 
                                     float * output);

You can also declare multiple functions to have none mangled names with a block:

extern "C"
{
    void foo(int i);
    void bar(char c);
}

If you need to expose classes from C++ I would recomend Boost.Python.

Arlaharen
Thank you. This solves the problem. I want to use Boost.Python but I also want to pass numpy arrays to my class/functions. I have not found a way to pass my numpy array to a C++ function. Perhaps you have a suggestion?As you notice, the parameter (float *) output will be given by using ctypes functions of numpy array class. If I can do something similar in Boost, my life will be much better.
Dat Chu
I haven't used numpy so I can't really say. Using Boost.Python you can have C++ methods accepting Python objects though. See boost::python::object.
Arlaharen