c++ - Ctypes not finding symbols in shared library created using CMake -


my cmake setting create shared lib in linux like

set (cmake_cxx_flags "-fpic")  set (lib_utils_src     utils.cpp )  add_library (utils shared     ${lib_utils_src} ) 

source utils.cpp

double addtwonumber(double x, double y) {     return x + y; } 

when trying access 'addtwonumber' function using ctypes like

import os import ctypes c  libpath = '/home/ap/workspace/learningcpp/lib/libutils.so' libutils = c.cdll.loadlibrary(libpath)  prototype = c.cfunctype(         c.c_double,                     c.c_double,                     c.c_double                 ) addtwonumber = prototype(('addtwonumber', libutils))  res = addtwonumber(c.c_double(2.3), c.c_double(3.5) ) 

i getting message like.

attributeerror: /home/ap/workspace/learningcpp/lib/libutils.so: undefined symbol: addtwonumber 

i checked libutils.so using "nm --demangle libutils.so" command , shows 'addtwonumber' symbol in it.

why still getting "undefined symbol" message python ? guessing there must compiler flags set symbols mangle properly. suggestion appreciated !

interesting, use numpy.ctypes since have deal large data sets, , never had issues think know whats going on here, it's names being mangled g++ compiler, made work way:

makefile:

g++ -wall -fpic -o2 -c utils.cpp g++ -shared -wl -o libutils.so utils.o 

utils.cpp

extern "c" double addtwonumber(double x, double y) {     return x + y; } 

test.py

import os import ctypes c  libutils = c.cdll.loadlibrary('libutils.so')  prototype = c.cfunctype(         c.c_double,                     c.c_double,                     c.c_double                 ) addtwonumber = prototype(('addtwonumber', libutils))  res = addtwonumber(c.c_double(2.3), c.c_double(3.5) ) print res 

output:

$ python test.py 5.8 

note extern keyword makes sure compiler doesn't mangle name, have stuff when under windows, did find http://wolfprojects.altervista.org/dllforpyinc.php kind of interesting.

i hope helps.

my machine:

$ g++ --version
i686-apple-darwin10-g++-4.2.1 (gcc) 4.2.1 (apple inc. build 5666) (dot 3) copyright (c) 2007 free software foundation, inc. free software; see source copying conditions. there no warranty; not merchantability or fitness particular purpose.

$ uname -a
darwin macbookpro 10.8.0 darwin kernel version 10.8.0: tue jun 7 16:33:36 pdt 2011; root:xnu-1504.15.3~1/release_i386 i386


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -