使用 SWIG 包装 python 的 C++ 代码。无法使用 cout 命令
Wrapping C++ code for python using SWIG. Can't use cout command
我正在尝试使用 SWIG 包装 python 这个简单的 C++ 代码:
#include "hello.h"
int helloW()
{
std::cout << "Hello, World!" ;
return 0;
}
这里是亲戚 header:
#include <iostream>
int helloW() ; // decl
作为 SWIG 输入文件,我正在使用:
/* file : pyhello.i */
/* name of module to use*/
%module pyhello
%{
#include "hello.h"
%}
%include "hello.h";
现在,我的 makefile(运行 很好)是:
all:
swig -c++ -python -Wall pyhello.i
gcc -c -fpic pyhello_wrap.cxx hello.cpp -I/usr/include/python2.7
gcc -shared hello.o pyhello_wrap.o -o _pyhello.so
因为我能够从与在线问题相关的不同来源收集信息。
现在,一旦我尝试使用命令
导入 python 我的库
>>> import pyhello
这是我得到的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pyhello.py", line 17, in <module>
_pyhello = swig_import_helper()
File "pyhello.py", line 16, in swig_import_helper
return importlib.import_module('_pyhello')
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: ./_pyhello.so: undefined symbol: _ZSt4cout
这让我觉得这个问题与命令 std::cout
相关,或者通常与标准库 <iostream>
.
相关
希望有人能给我一些关于这个问题的提示。提前致谢!!
注意:我尝试使用命令 printf()
而不是 std::cout
和库 <cstdio>
而不是<iostream>
ImportError: ./_pyhello.so: undefined symbol: _ZSt4cout
和c++filt _ZSt4cout
你会发现它是std::cout
(name mangling).
您应该使用 g++
,而不是 gcc
,特别是在您的 linker 命令中(使用 -shared
)。
或者您需要 link 明确地使用某些 -lstdc++
您的共享库。
阅读 Drepper 的 How to Write Shared Libraries (since Python is dlopen(3)-ing then dlsym(3)-ing 它)。
你最好将你的例程声明为 extern "C" int helloW(void);
(阅读 C++ dlopen minihowto)。
我正在尝试使用 SWIG 包装 python 这个简单的 C++ 代码:
#include "hello.h"
int helloW()
{
std::cout << "Hello, World!" ;
return 0;
}
这里是亲戚 header:
#include <iostream>
int helloW() ; // decl
作为 SWIG 输入文件,我正在使用:
/* file : pyhello.i */
/* name of module to use*/
%module pyhello
%{
#include "hello.h"
%}
%include "hello.h";
现在,我的 makefile(运行 很好)是:
all:
swig -c++ -python -Wall pyhello.i
gcc -c -fpic pyhello_wrap.cxx hello.cpp -I/usr/include/python2.7
gcc -shared hello.o pyhello_wrap.o -o _pyhello.so
因为我能够从与在线问题相关的不同来源收集信息。 现在,一旦我尝试使用命令
导入 python 我的库>>> import pyhello
这是我得到的错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pyhello.py", line 17, in <module>
_pyhello = swig_import_helper()
File "pyhello.py", line 16, in swig_import_helper
return importlib.import_module('_pyhello')
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
ImportError: ./_pyhello.so: undefined symbol: _ZSt4cout
这让我觉得这个问题与命令 std::cout
相关,或者通常与标准库 <iostream>
.
希望有人能给我一些关于这个问题的提示。提前致谢!!
注意:我尝试使用命令 printf()
而不是 std::cout
和库 <cstdio>
而不是<iostream>
ImportError: ./_pyhello.so: undefined symbol:
_ZSt4cout
和c++filt _ZSt4cout
你会发现它是std::cout
(name mangling).
您应该使用 g++
,而不是 gcc
,特别是在您的 linker 命令中(使用 -shared
)。
或者您需要 link 明确地使用某些 -lstdc++
您的共享库。
阅读 Drepper 的 How to Write Shared Libraries (since Python is dlopen(3)-ing then dlsym(3)-ing 它)。
你最好将你的例程声明为 extern "C" int helloW(void);
(阅读 C++ dlopen minihowto)。