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

发布于 2024-12-01 06:20:47 字数 2140 浏览 1 评论 0原文

我正在尝试将 python 代码嵌入到 C++(Windows 7 + minGW + Python 2.7.2 + 带有 CDT 和 PyDev 的 Eclipse Indigo) 中。

所以,这是简单的代码:

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

我不明白我做错了什么。 我包含目录 C:\Python27\includeC:\Python27\lib 但我无法构建我的项目。

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 ****

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

谢谢

I'm trying to embed python code in 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\libs but I can't build my project.

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) 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?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无边思念无边月 2024-12-08 06:20:47

这是链接器错误,而不是编译器错误。您需要链接到 python。正如您所看到的,使用“CrossGCC”工具链您就差不多完成了:

-lC:/Python27/libs/libpython27.a

您需要将其更改为

-LC:/Python27/libs -lpython

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

You need to change this to

-LC:/Python27/libs -lpython
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文