gpt4 book ai didi

python - WindowsError 异常访问冲突 - 在简单的 python c++ ctypes 接口(interface)中

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:51:08 26 4
gpt4 key购买 nike

我有一个非常简单的测试用例,我无法开始工作,我正在尝试使用 ctypes 将 c++ 与 python 连接起来。我在使用 double 时遇到错误,在这种情况下尝试在 C++ 中使用“cout”。

错误是:

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

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

#include "testgeo.h"
#include <iostream>
TestGeo::TestGeo() : td_(0),
ti_(0) {
std::cout<<td_<<std::endl; // problem line
}

它具有以下 header (testgeo.h),包括一个外部 C 部分:

class TestGeo {

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

private:
double td_;
int ti_;

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

运行它的 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()

编辑 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 问题。但是,希望它可以提供一些有关情况的信息。

最佳答案

在注意到其他问题后,结果证明我的编译器配置有问题,重新安装/不同的编译器允许此代码运行。

关于python - WindowsError 异常访问冲突 - 在简单的 python c++ ctypes 接口(interface)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23105779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com