Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用链接库中的方法时出现属性错误 我试图在我的python代码中使用一些C++方法,我使用的是 cType < /Cuth>库。我用一个简单的C++代码编写了一个简单的C++代码,它没有参数,还有一个简单的方法,叫做代码> Primalgs,它采用了类型 char *的参数。我还编写了一个简单的python代码来导入这两个方法。我使用以下命令创建了两个链接库(一个.so和一个.a,因为我使用的是Debian): g++ -o hello.so -shared -fPIC hello.cpp_C++_Python_Ctypes_Undefined Symbol - Fatal编程技术网

在python中使用链接库中的方法时出现属性错误 我试图在我的python代码中使用一些C++方法,我使用的是 cType < /Cuth>库。我用一个简单的C++代码编写了一个简单的C++代码,它没有参数,还有一个简单的方法,叫做代码> Primalgs,它采用了类型 char *的参数。我还编写了一个简单的python代码来导入这两个方法。我使用以下命令创建了两个链接库(一个.so和一个.a,因为我使用的是Debian): g++ -o hello.so -shared -fPIC hello.cpp

在python中使用链接库中的方法时出现属性错误 我试图在我的python代码中使用一些C++方法,我使用的是 cType < /Cuth>库。我用一个简单的C++代码编写了一个简单的C++代码,它没有参数,还有一个简单的方法,叫做代码> Primalgs,它采用了类型 char *的参数。我还编写了一个简单的python代码来导入这两个方法。我使用以下命令创建了两个链接库(一个.so和一个.a,因为我使用的是Debian): g++ -o hello.so -shared -fPIC hello.cpp,c++,python,ctypes,undefined-symbol,C++,Python,Ctypes,Undefined Symbol,然后使用导出LD_LIBRARY_PATH=/the/PATH/to/the/linked/libraries/directory 获取main方法没有问题,但是当我尝试获取printArgs时,我会获取AttributeError。下面是C++代码: #include <iostream> using namespace std; int printArgs(char * args_array){ for (int i = 0; i < 5; i++){

然后使用导出LD_LIBRARY_PATH=/the/PATH/to/the/linked/libraries/directory

获取
main
方法没有问题,但是当我尝试获取
printArgs
时,我会获取
AttributeError
。下面是C++代码:

#include <iostream>
using namespace std;

int printArgs(char * args_array){
    for (int i = 0; i < 5; i++){
        cout<<i<<"- "<<args_array[i]<<"\n";
    }
    return 0;
}

int main(){
    cout<<"Hello\n";
    return 0;
}
我得到这个输出:

<CDLL 'hello.a', handle 9c45488 at b73d1e6c>
<_FuncPtr object at 0xb73e67e4>
Traceback (most recent call last):
    File "testHelloInCPP.py", line 9, in <module>
        helloInCPPPrint = helloInCPP_lib.printArgs(None)
    File "/usr/lib/python2.6/ctypes/__init__.py", line 366, in __getattr__
        func = self.__getitem__(name)
    File "/usr/lib/python2.6/ctypes/__init__.py", line 371, in __getitem__
        func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /etc/linked_libraries/hello.a: undefined symbol: printArgs

回溯(最近一次呼叫最后一次):
文件“testHelloInCPP.py”,第9行,在
HelloinCPPRINT=helloInCPP_lib.printArgs(无)
文件“/usr/lib/python2.6/ctypes/_init__uuu.py”,第366行,在_getattr中__
func=self.\uuuu getitem\uuuuu(名称)
文件“/usr/lib/python2.6/ctypes/_init__uuuu.py”,第371行,在_getitem中__
func=self.\u FuncPtr((名称或顺序,self))
AttributeError:/etc/linked_libraries/hello.a:未定义的符号:printArgs
我还尝试了
cdll.LoadLibrary(“hello.so”)
和/或
helloincpprint=helloInCPP_lib.printArgs(无)
;在两种情况下都得到相同的错误。有什么想法吗


我在VMWare工作站和Python 2.6上使用Debian 32位

使用以下命令声明
printArgs


关于
argtypes
,请参阅。

我厌倦了使用@falsetru编译解决方案

g++ -O3 -shared -std=c++11 -fPIC code.cpp -o lib.so
但是,根据他们在回答中提供的链接,如果没有对代码c的这一小小修改,就不能:

#包括
使用名称空间std;
外部“C”int printArgs(字符*参数\u数组){
对于(int i=0;i<5;i++){

对于
printArgs
@ZeinabAbbasi,您是否有
分段错误
,您将什么传递给
printArgs
?@ZeinabAbbasi,我用稍微修改的Python代码更新了答案。请检查。谢谢,这就是答案。@ZeinabAbbasi,请参阅和。
#include <iostream>
using namespace std;

extern "C" {
    int printArgs(char * args_array);
}

int printArgs(char * args_array){
    for (int i = 0; i < 5; i++){
        cout<<i<<"- "<<args_array[i]<<"\n";
    }
}

int main(){
    cout<<"Hello\n";
}
...
helloInCPPPrint = helloInCPP_lib.printArgs
helloInCPPPrint.argtypes = [c_char_p]
print helloInCPPPrint("ABCDE")
g++ -O3 -shared -std=c++11 -fPIC code.cpp -o lib.so
#include <iostream>
using namespace std;

extern "C" int printArgs(char * args_array){
    for (int i = 0; i < 5; i++){
        cout<<i<<"- "<<args_array[i]<<"\n";
    }
}