在Windows上将Python链接到我的C ++代码中-链接器错误 [英] Linking Python into my C++ code on windows - linker error

查看:66
本文介绍了在Windows上将Python链接到我的C ++代码中-链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将python链接到我的C ++代码时出现错误.

I have an error trying to link python into my C++ code.

这是我得到的错误行:

C:\Python27\libs/libpython27.a(dmmes00855.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'

collect2.exe: error: ld returned 1 exit status

对于背景,这是最小的情况:

For background, here is minimal case:

#include <Python.h>

int main()
{
    Py_Initialize();
}

我正在使用scons进行构建:

I am using scons to build:

import os
env = Environment(ENV = os.environ)
env.Append(CPPPATH = "C:/Python27/include/")
env.Append(LIBPATH = "C:/Python27/libs/")
env.Append(LIBS = "python27")
env.Program("test", "test.cpp")

它将生成以下命令:

g++ -o test.o -c -IC:\Python27\include test.cpp
g++ -o test.exe test.o -LC:\Python27\libs -lpython27

编译器是Qt下载的一部分.

Compiler is the mingw that I have as part of my Qt download.

推荐答案

g++命令没有问题.显然,这是一个 bug .

错误修正版本2.7.10当前可用.首先升级到2.7.10

A bugfix release 2.7.10 is currently available. First of all Upgrade to 2.7.10

然后您需要使用

gendef.exe python27.dll

dlltool.exe --dllname python27.dll --def python27.def --output-lib libpython27.a

并将其放置在

and place it in C:\Python27\libs

现在使用MinGW编译可以正常工作.

Now compiling with MinGW will work fine.

这篇关于在Windows上将Python链接到我的C ++代码中-链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆