Ctypes在使用CMake创建的共享库中找不到符号 [英] Ctypes not finding symbols in shared library created using CMake

查看:157
本文介绍了Ctypes在使用CMake创建的共享库中找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux中创建共享库的CMake设置类似

My CMake setting to create a shared lib in linux is something like

SET (CMAKE_CXX_FLAGS "-fPIC")

SET (LIB_UTILS_SRC
    Utils.cpp
)

ADD_LIBRARY (UTILS SHARED
    ${LIB_UTILS_SRC}
)

来源Utils.cpp

Source Utils.cpp

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

当尝试使用类似CType的类型访问'addTwoNumber'函数时

When trying to access 'addTwoNumber' function using CTypes like

import os
import ctypes as 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 am getting some message like.

AttributeError: /home/AP/workspace/LearningCPP/lib/libUTILS.so:
undefined symbol: addTwoNumber

我使用 nm --demangle libUTILS.so命令检查了libUTILS.so,它清楚地在其中显示了 addTwoNumber符号。

I checked the libUTILS.so using the "nm --demangle libUTILS.so" command and it clearly shows the 'addTwoNumber' symbol in it.

为什么我仍然从python收到未定义符号消息?
我猜必须设置一些编译器标志,以便正确地处理符号。任何建议将不胜感激!

Why am I still getting the "undefined symbol" message from python ? I am guessing there must be some compiler flags to be set so that symbols are mangle properly. Any suggestion would be appreciated !

推荐答案

有趣的是,我通常使用 numpy.ctypes 因为我经常需要处理大型数据集,而且从来没有任何问题,但是我想我知道这是怎么回事,这是因为名称正被g ++编译器所破坏,所以我以这种方式使其工作:

Interesting, I usually use numpy.ctypes since I constantly have to deal with large data sets, and never had any issues but I think I know whats going on here, it's that the names are being mangled by the g++ compiler, I made it work this way:

Makefile:

g++ -Wall -fPIC -O2 -c Utils.cpp
g++ -shared -Wl -o libUTILS.so Utils.o

Utils.cpp

Utils.cpp

extern "C" double addTwoNumber(double x, double y)
{
    return x + y;
}

test.py

import os
import ctypes as 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

输出:

$ python test.py
5.8

请注意 extern 关键字可确保编译器不会不要弄乱名字,在Windows下,您必须做一些额外的事情,我确实找到了 http:// wolfprojects .altervista.org / dllforpyinc.php 很有意思。

note the extern keyword this makes sure the compiler doesn't mangle the name, you have to do some extra stuff when under windows, I did find http://wolfprojects.altervista.org/dllforpyinc.php which was kind of interesting.

我希望这会有所帮助。

我的机器:

$ g ++ --version

i686-apple-darwin10-g ++-4.2.1(GCC)4.2.1(Apple Inc. build 5666)(dot 3)
版权所有(C)2007自由软件基金会,公司。
这是免费软件;仅供参考。请参阅复制条件的来源。没有
保修;

$ 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. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ uname -a

Darwin MacBookPro 10.8.0 Darwin Kernel Version 10.8.0:Tue甚至不是针对特定目的的适销性或适用性。

$ 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

这篇关于Ctypes在使用CMake创建的共享库中找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆