Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ with python.h, how to compile

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

#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.

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

Makefile is like the following.

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:

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

any help? thank you!

like image 808
bbc Avatar asked Apr 07 '12 16:04

bbc


People also ask

Where is the Python h file?

The header files are typically installed with Python. On Unix, these are located in the directories prefix/include/pythonversion/ and exec_prefix/include/pythonversion/ , where prefix and exec_prefix are defined by the corresponding parameters to Python's configure script and version is '%d. %d' % sys.

What is python3 dev?

Package: python3-devel description: Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax.


1 Answers

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."

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.

(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 StackOverflow.)

like image 161
D. A. Avatar answered Oct 19 '22 23:10

D. A.