我有一个非常简单的测试用例,我无法工作,我正在尝试使用ctypes将c ++与python接口.我在使用双打时遇到错误,在这种情况下尝试在c ++中使用"cout".
错误是:
WindowsError: exception: access violation writing 0x.....
Run Code Online (Sandbox Code Playgroud)
问题在于以下c ++代码的cout行:
#include "testgeo.h"
#include <iostream>
TestGeo::TestGeo() : td_(0),
ti_(0) {
std::cout<<td_<<std::endl; // problem line
}
Run Code Online (Sandbox Code Playgroud)
其中包含以下标题(testgeo.h),包括extern C部分:
class TestGeo {
public:
TestGeo();
~TestGeo(){};
private:
double td_;
int ti_;
};
extern "C" {
__declspec(dllexport) TestGeo* TestGeo_new() {
return new TestGeo();
}
}
Run Code Online (Sandbox Code Playgroud)
运行它的python代码是(testgeo.py):
import ctypes
lib = ctypes.cdll.LoadLibrary('testgeo.dll')
class TestGeo(object):
lib.TestGeo_new.argtypes = []
lib.TestGeo_new.restype = ctypes.c_void_p
def __init__(self):
self.obj = lib.TestGeo_new()
if __name__ == "__main__":
testGeoObj = TestGeo()
Run Code Online (Sandbox Code Playgroud)
编辑1:仍在努力,我对编程很陌生.无论如何我可以进一步调查内存错误,这可能会给我一些线索吗?
编辑2:我想我会分享我如何编译,以防出现错误:
x86_64-w64-mingw32-g++ -c testgeo.cpp -o testgeo.o -std=c++11 -O2 -Wall -Wextra -Weffc++ -pedantic
x86_64-w64-mingw32-g++ -shared -o testgeo.dll testgeo.o
Run Code Online (Sandbox Code Playgroud)
运行代码:
python testgeo.py
Run Code Online (Sandbox Code Playgroud)
编辑3:代码在我的linux机器上运行...这意味着我仍然不确定我的Windows问题.但希望它可以为这种情况提供一些启示.