c++ - 需要帮助使用 C++ 模块扩展 Python 3.2

标签 c++ python swig

有没有人对如何使用 C++ 专门扩展 Python 3 有建议?我试过使用 SWIG,但在尝试访问我的计算机上不存在的库文件(Python_d.lib 或类似文件)时出现致命链接错误。

编辑:

我采取的步骤是:

1) 从 http://www.swig.org/download.html 下载 swigwin-2.0.4

2) 设置环境变量(PYTHON_INCLUDE 和 PYTHON_LIB)

3) 构建 swigwin 包中包含的示例之一。下面发布了此代码。这是在 MVSC++ 2010 中构建的。

/* File : example.c */

#include "example.h"
#define M_PI 3.14159265358979323846

/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
    x += dx;
    y += dy;
}

int Shape::nshapes = 0;

double Circle::area(void) {
    return M_PI*radius*radius;
}

double Circle::perimeter(void) {
    return 2*M_PI*radius;
}

double Square::area(void) {
    return width*width;
}

double Square::perimeter(void) {
    return 4*width;
}

我从中得到的输出的相关部分是:

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(990,5): warning MSB8012: TargetPath(C:\Users\David\Downloads\swigwin-2.0.4\Examples\python\class\.\Debug\example.dll) does not match the Linker's OutputFile property value (C:\Users\David\Downloads\swigwin-2.0.4\Examples\python\class\_example.pyd). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(991,5): warning MSB8012: TargetExt(.dll) does not match the Linker's OutputFile property value (.pyd). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).

1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(992,5): warning MSB8012: TargetName(example) does not match the Linker's OutputFile property value (_example). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).

1>LINK : fatal error LNK1104: cannot open file 'python32_d.lib'
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:04.27

最佳答案

在 MSVC 中将构建配置从 Debug 更改为 Release,或查看 this question 的答案.

关于c++ - 需要帮助使用 C++ 模块扩展 Python 3.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9394926/

相关文章:

c++ - 是否有用于拆分 std::pair 的标准 C++ 函数对象?

C++正则表达式多行替换

python b64decode不正确的填充

python - Django 在一次调用中序列化多个对象

c++ - 在 SWIG 中将结构从 C++ 函数返回到 Python

c++ - Linux (Ubuntu 18) 上的按键模拟问题

c++ - 为 SDL 事件过滤创建指向成员函数的非常量指针

python - 在Python中从ip查找操作系统

python - swig 将元组列表传递给 C++ 函数

c++ - Python ctypes 回调函数到 SWIG