g++ python。h,如何编译

时间:2021-11-16 20:27:36

I compile one test code with g++ without any issue.

我用g++编译了一个测试代码,没有任何问题。

#include "Python.h"  

int main(int argc, char** argv)  
{  
    Py_Initialize();  
    PyRun_SimpleString("import pylab");  
    PyRun_SimpleString("pylab.plot(range(5))");  
    PyRun_SimpleString("pylab.show()");  
    Py_Exit(0);  
} 

g++ -o test test.cpp -I/usr/include/python2.7/ -lpython2.7 works fine and runs.

g++ - o测试测试。cpp -I/usr/include/python2.7/ -lpython2.7运行良好并运行。

But when I try to embed this code into another project, it fails. It really confuses me.

但是当我试图将这些代码嵌入到另一个项目中时,它就失败了。真的让我迷惑。

Makefile is like the following.

Makefile就像下面这样。

CXX=g++  
CXXFLAGS=-DIB_USE_STD_STRING -Wall -Wno-switch -g  
ROOT_DIR=..  
BASE_SRC_DIR=${ROOT_DIR}/PosixSocketClient  
INCLUDES=-I${ROOT_DIR}/Shared/ -I${BASE_SRC_DIR} -I/usr/include/python2.7  
LIBRARY=-L/usr/lib/python2.7/config  
TARGET=eu  

$(TARGET):  
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o EClientSocketBase.o -c   $(BASE_SRC_DIR)/EClientSocketBase.cpp  
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o EPosixClientSocket.o -c   $(BASE_SRC_DIR)/EPosixClientSocket.cpp  
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o PosixTestClient.o -c PosixTestClient.cpp  
    $(CXX) $(CXXFLAGS) $(INCLUDES) -o Main.o -c Main.cpp
    $(CXX) $(LIBRARY) -lpython2.7 -o $@ EClientSocketBase.o EPosixClientSocket.o PosixTestClient.o Main.o 

clean:  
    rm -f $(TARGET) *.o  

This project compiles fine and runs, the only change I made was adding the test code in the Main.cpp file. warning/error message shows:

这个项目编译良好并运行,我所做的唯一更改就是在Main中添加测试代码。cpp文件。警告/错误消息显示:

In file included from /usr/include/python2.7/Python.h:8:0,
from Main.cpp:15:
/usr/include/python2.7/pyconfig.h:1158:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
/usr/include/features.h:163:0: note: this is the location of the previous definition
/usr/include/python2.7/pyconfig.h:1180:0: warning: "_XOPEN_SOURCE" redefined [enabled by default]
/usr/include/features.h:165:0: note: this is the location of the previous definition
g++ -L/usr/lib/ -lpython2.7 -ldl -lutil -o eu EClientSocketBase.o EPosixClientSocket.o PosixTestClient.o Main.o
Main.o: In function main':
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:81: undefined reference to
Py_Initialize'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:82: undefined reference to PyRun_SimpleStringFlags'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:83: undefined reference to
PyRun_SimpleStringFlags'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:84: undefined reference to PyRun_SimpleStringFlags'
/home/bbc/TWS/IBJts/cpp/eu-ats/Main.cpp:85: undefined reference to
Py_Exit'
collect2: ld returned 1 exit status
make: * [eu] Error 1

在文件中,包括/usr/include/python2.7/ python2。h:译者注,从主。cpp:15:/usr/include/python2.7/pyconfig.h:1158:0:警告:“_POSIX_C_SOURCE”重新定义[启用默认]/usr/include/features。h:163:0:注意:这是前面定义/usr/include/python2.7/pyconfig的位置。h:1180:0:警告:“_XOPEN_SOURCE”重新定义[启用默认]/usr/include/features。h:165:0:注意:这是前一个定义的位置g++ -L/usr/lib/ -lpython2.7 -ldl -lutil -o eu EClientSocketBase。o EPosixClientSocket。o PosixTestClient。o主要。o主要。o:在功能main': /home/ bbc/tws/ibjts/cpp/eu-ats/main。cpp:81:未定义的引用toPy_Initialize' /home/ bbc/tws/ibjts/cpp/eu-ats/main。cpp:82:未定义对PyRun_SimpleStringFlags的/home/ bbc/tws/ibjts/cpp/eu-ats/main。cpp:83:未定义的引用toPyRun_SimpleStringFlags的/home/ bbc/tws/ibjts/cpp/eu-ats/main。cpp:84:未定义的对PyRun_SimpleStringFlags的/home/ bbc/tws/ibjts/cpp/eu-ats/main。cpp:85:未定义的引用toPy_Exit' collect2: ld返回1退出状态:* [eu] Error 1。

any help? thank you!

任何帮助吗?谢谢你!

1 个解决方案

#1


14  

Take a look at Lucas's comment for the answer:

看看卢卡斯的评论吧:

"To get rid of the _POSIX_C_SOURCE warning, make sure to include Python.h before all other header files."

要清除_POSIX_C_SOURCE警告,请确保包含Python。在所有其他头文件之前。

I had the same problem. I use Boost Python, so for me I moved the include of boost/python.hpp to the first line in my .cpp file.

我遇到了同样的问题。我使用Boost Python,因此我移动了Boost / Python的include。hpp到我的.cpp文件的第一行。

(Lukas, post your comment as an answer so the person who asked can mark it as the right answer, and the question won't remain 'unanswered' in *.)

(Lukas,把你的评论作为一个答案,这样提问的人就可以把它标记为正确的答案,而这个问题不会在*中保持“没有答案”。)

#1


14  

Take a look at Lucas's comment for the answer:

看看卢卡斯的评论吧:

"To get rid of the _POSIX_C_SOURCE warning, make sure to include Python.h before all other header files."

要清除_POSIX_C_SOURCE警告,请确保包含Python。在所有其他头文件之前。

I had the same problem. I use Boost Python, so for me I moved the include of boost/python.hpp to the first line in my .cpp file.

我遇到了同样的问题。我使用Boost Python,因此我移动了Boost / Python的include。hpp到我的.cpp文件的第一行。

(Lukas, post your comment as an answer so the person who asked can mark it as the right answer, and the question won't remain 'unanswered' in *.)

(Lukas,把你的评论作为一个答案,这样提问的人就可以把它标记为正确的答案,而这个问题不会在*中保持“没有答案”。)