我试图用cython包装用c++编写的DLL。最终我将无法访问
源代码,所以在编译包装器的过程中不能使用c++源代码,它包含.dll
、.lib
和{
我的python3.5发行版也是由vs2015构建的,为32位。在
$ python -iu
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:38:48) [MSC v.1900 32 bit (Intel)] on win32
当我使用一个包含返回字符串的函数的伪c++源代码进行编译时, 它只是起作用。使用dll它不起作用。
在设置.py公司名称:
^{pr2}$在测试.pyx公司名称:
# distutils: language = c++
# distutils: libraries = MyDLL
# distutils: include_dirs = .
# distutils: library_dirs = .
from libcpp.string cimport string
cdef extern from "MyDLL.h" namespace "somenamespace":
int ReturnSomeInt()
string ReturnSomeString()
def run():
cdef string s = string(b'abcdefg')
print(s)
cdef int i = ReturnSomeInt()
print(i)
cdef string problem = ReturnSomeString()
print(problem)
MyDLL.h:
__declspec(dllexport) int ReturnSomeInt();
__declspec(dllexport) std::string ReturnSomeString();
编译MyDL:
的C++代码片段__declspec(dllexport) int ReturnSomeInt() { return 42; }
__declspec(dllexport) std::string ReturnSomeString() { cout << "debug..." << endl; return "Hello world!"; }
在主.py公司名称:
from test import run
run()
我使用命令编译
$ python setup.py build_ext --inplace --force && python -u main.py
打印出这个
b'abcdefg'
42
debug... # printed by the dll function ReturnSomeString()
b'' # Should've printed: b'Hello World!'
我们可以验证MyDLL中的ReturnSomeString()是否被实际调用,因为它向stdout发送了一些文本。在
我没查到什么?在
感谢@hakala指出的this answer,我发现调试/发布模式很重要。我没想到会这样。我引用:
实际上,在Release模式下编译DLL使OP中的示例起作用。在
相关问题 更多 >
编程相关推荐