簡體   English   中英

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

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

我正在嘗試在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)

所以,這是簡單的代碼:

#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\\includeC:\\Python27\\lib s但我無法構建我的項目。

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)如果我將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 ****

有人能告訴我我的代碼或設置有什么問題嗎?

謝謝

這是鏈接器錯誤,而不是編譯器錯誤。 你需要鏈接到python。 正如您所看到的,使用“CrossGCC”工具鏈,您幾乎就在那里:

-lC:/Python27/libs/libpython27.a

您需要將其更改為

-LC:/Python27/libs -lpython

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM