在C ++中嵌入python代码(Windows + minGW + Python 2.7.2 + Eclipse) [英] Embed python code in C++ (Windows + minGW + Python 2.7.2 + Eclipse)

查看:1675
本文介绍了在C ++中嵌入python代码(Windows + minGW + Python 2.7.2 + Eclipse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 C ++(Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT和PyDev)中嵌入Python代码

所以,这是一个简单的代码:

So, this is the simple code:

#include <Python.h> //Python.h
#include <iostream> //iostream
using namespace std;
int main(int argc, char *argv[])
{
    Py_Initialize();
    PyRun_SimpleString("from time import time,ctime\n"
    "print('Today is', ctime(time()))\n");
    Py_Finalize();
    return 0;
}

我不知道我做错了什么。
我包括dirrctories C:\Python27 \include C:\Python27\lib

And I couldn't understant what am I doing wrong. I include dirrctories C:\Python27\include and C:\Python27\libs but I can't build my project.

1)当我尝试构建我的项目时,我遇到了这个错误:

1) When I trying to build my project I got this error:

**** Internal Builder is used for build               **** g++
-IC:\Python27\include -IC:\Python27\libs -O0 -g3 -Wall -c
-fmessage-length=0 -o main.o ..\main.cpp g++ -o testpy2.exe main.o
main.o: In function `main':
C:\Users\const\workspace\testpy2\Debug/../main.cpp:7: undefined
reference to `_imp__Py_Initialize'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:9: undefined
reference to `_imp__PyRun_SimpleStringFlags'
C:\Users\const\workspace\testpy2\Debug/../main.cpp:10: undefined
reference to `_imp__Py_Finalize' 
collect2: ld returned 1 exit status
Build error occurred, build is stopped Time consumed: 1507  ms.

2)如果我将Eclipse中的当前工具链从minGW更改为CrossGCC发生此错误:

2) And if I change current toolchain in Eclipse from "minGW" to "CrossGCC" .. I got this error:

**** Build of configuration Release for project testpy ****

make all  Building file: ../main.cpp Invoking: Cross G++ Compiler g++
-I"C:\Python27\include" -I"C:\Python27\libs" -O3 -Wall -c
-fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o"
"../main.cpp" Finished building: ../main.cpp   Building target:
testpy.exe Invoking: Cross G++ Linker g++  -o "testpy.exe"  ./main.o  
-l"C:/Python27/libs/libpython27.a" -l"C:/Python27/libs/python27.lib"
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/libpython27.a
c:/mingw/bin/../lib/gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe:
cannot find -lC:/Python27/libs/python27.lib collect2: ld returned 1
exit status make: *** [testpy.exe] Error 1

**** Build Finished ****

有人能告诉我我的代码或设置或其他什么问题吗?

Could anybody tell me what's wrong with my code or settings or something else?

谢谢

推荐答案

这是一个链接器错误,而不是编译器错误。你需要链接到python。正如你所看到的,使用CrossGCC工具链你几乎可以:

That is a linker error, not a compiler error. You need to link to the python. As you can see, with the "CrossGCC" toolchain you are almost there:

-lC:/Python27/libs/libpython27.a

您需要将其更改为

-LC:/Python27/libs -lpython

这篇关于在C ++中嵌入python代码(Windows + minGW + Python 2.7.2 + Eclipse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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