简体   繁体   English

在C ++中嵌入python代码(Windows + minGW + Python 2.7.2 + Eclipse)

[英]Embed python code in C++ (Windows + minGW + Python 2.7.2 + Eclipse)

I'm trying to embed python code in C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev) . 我正在尝试在C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev)嵌入python代码C++ (Windows 7 + minGW + Python 2.7.2 + Eclipse Indigo with CDT and PyDev)

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;
}

And I couldn't understant what am I doing wrong. 我无法理解我做错了什么。 I include dirrctories C:\\Python27\\include and C:\\Python27\\lib s but I can't build my project. 我包括dirrctories C:\\Python27\\includeC:\\Python27\\lib s但我无法构建我的项目。

1) When I trying to build my project I got this error: 1)当我尝试构建我的项目时,我收到此错误:

**** 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) And if I change current toolchain in Eclipse from "minGW" to "CrossGCC" .. I got this error: 2)如果我将Eclipse中的当前工具链从“minGW”更改为“CrossGCC”..我收到此错误:

**** 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? 有人能告诉我我的代码或设置有什么问题吗?

Thank you 谢谢

That is a linker error, not a compiler error. 这是链接器错误,而不是编译器错误。 You need to link to the python. 你需要链接到python。 As you can see, with the "CrossGCC" toolchain you are almost there: 正如您所看到的,使用“CrossGCC”工具链,您几乎就在那里:

-lC:/Python27/libs/libpython27.a

You need to change this to 您需要将其更改为

-LC:/Python27/libs -lpython

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM