中的简单python c++ctypes中的WindowsError异常访问冲突

2024-05-23 14:00:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个非常简单的测试用例,我无法开始工作,我正在尝试使用ctypes将c++与python接口。 我在使用double时出错,在这个例子中,我试图在c++中使用“cout”。在

错误是:

WindowsError: exception: access violation writing 0x.....

问题出在以下c++代码的cout行中:

^{pr2}$

它具有以下标题(testgeo.h),包括一个extern C部分:

class TestGeo {

    public:
    TestGeo();
    ~TestGeo(){};

    private:
    double td_;
    int ti_;

};
extern "C" {
    __declspec(dllexport) TestGeo* TestGeo_new() {
        return new TestGeo();
    }
}   

运行这个的python代码是(测试地理.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()

编辑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

运行代码:

python testgeo.py

编辑3:代码在我的linux机器上运行。。。这意味着我仍然不确定我的windows问题。不过,希望它能为局势提供一些线索。在


Tags: 代码pyself编辑newlib错误extern
1条回答
网友
1楼 · 发布于 2024-05-23 14:00:09

在注意到其他问题之后,我发现编译器的配置是问题所在,重新安装/不同的编译器允许此代码运行。在

相关问题 更多 >