Implementing Python in C, the link does not work with an undefined reference to `Py_Initialize '

I am trying to compile this example from docs https://docs.python.org/2.7/extending/embedding.html and my code looks exactly the same as in the file under 5.1:

#include <Python.h> int main(int argc, char *argv[]) { Py_SetProgramName(argv[0]); Py_Initialize(); PyRun_SimpleString("from time import time, ctime\n" "print 'Today is', ctime(time())\n"); Py_Finalize(); return 0; } 

I use the following compilation command, which works fine for me and gives me the desired object file:

 gcc -c $(python2.7-config --cflags) embedpy.c 

To bind it, I use the following command, which ends with the following error:

 gcc $(/usr/bin/python2.7-config --ldflags) embedpy.o embedpy.o: In function `main': /home/miguellissimo/embedpy.c:6: undefined reference to `Py_SetProgramName' /home/miguellissimo/embedpy.c:7: undefined reference to `Py_Initialize' /home/miguellissimo/embedpy.c:8: undefined reference to `PyRun_SimpleStringFlags' /home/miguellissimo/embedpy.c:11: undefined reference to `Py_Finalize' collect2: error: ld returned 1 exit status 

I cannot find out what I am doing wrong or what I forgot for this example to work.

PS: the python2.7-config command outputs the following result on my Xubuntu machine:

 >>> python2.7-config --cflags -I/usr/include/python2.7 -I/usr/include/x86_64-linux-gnu/python2.7 -fno-stri ct-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size= 4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-pr ototypes >>> python2.7-config --ldflags -L/usr/lib/python2.7/config-x86_64-linux-gnu -L/usr/lib -lpthread -ldl -luti l -lm -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions 
+6
source share
2 answers

Libraries should appear after the object files when linking, so that:

 gcc embedpy.o $(/usr/bin/python2.7-config --ldflags) 
+11
source

I reproduced the same problem on a 32-bit Lubuntu14.04 virtual machine running on a Win7 machine.

I did the following things to replicate the problem, primarily where the C ++ code is written in the Eclipse C / C ++ IDE. A C ++ Eclipse project called "test" is defined. The source file contains the following C ++ code, which is the same as Miguellissimo mentioned above.

C ++ Code ::

 #include "python2.7/Python.h" int main(int argc, char *argv[]) { Py_Initialize(); PyRun_SimpleString("print \"Hello, world!\""); Py_Finalize(); return 0; } 

Errors ::

test.o: In main': /home/ros/workspace/test/src/test.cpp:15: undefined reference to Py_Initialize' /home/ros/workspace/test/src/test.cpp:17: undefined link on PyRun_SimpleStringFlags' /home/ros/workspace/test/src/test.cpp:18: undefined reference to Py_Finalize' collect2: error: ld returned 1 exit status

The output of the following commands was the same as previously mentioned by Miguellissimo,

 ros@rosPC :~/workspace/test/src$ python2.7-config --cflags -I/usr/include/python2.7 -I/usr/include/i386-linux-gnu/python2.7 -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes ros@rosPC :~/workspace/test/src$ python2.7-config --ldflags -L/usr/lib/python2.7/config-i386-linux-gnu -L/usr/lib -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions 

Properties of a C ++ Eclipse project project called "test" ::

Inside I had the following C ++ build settings

GCC C ++ Compiler

 Command: g++ All options: -I/opt/ros/indigo/include -O0 -g3 -Wall -c -fmessage-length=0 Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Includes include paths: /opt/ros/indigo/include 

GCC C Compiler

 Command: gcc All options: -I/opt/ros/indigo/include -I/usr/lib/python2.7/config-i386-linux-gnu -O0 -g3 -Wall -c -fmessage-length=0 Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Includes include paths: /opt/ros/indigo/include /usr/lib/python2.7/config-i386-linux-gnu 

GCC C ++ Linker

 Command: g++ All options: Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Libraries: Libraries(-I): Library search path(-L): 

Decision::

The following C ++ build settings are defined in the Project Properties of the C ++ Eclipse Project

GCC C ++ Compiler

 Command: g++ All options: -I/opt/ros/indigo/include -I/usr/include/python2.7 -O0 -g3 -Wall -c -fmessage-length=0 Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Includes include paths: /opt/ros/indigo/include /usr/include/python2.7 

GCC C Compiler

 Command: gcc All options: -I/opt/ros/indigo/include -I/usr/include/python2.7 -O0 -g3 -Wall -c -fmessage-length=0 Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Includes include paths: /opt/ros/indigo/include /usr/include/python2.7 

GCC C ++ Linker

 Command: g++ All options: -L/usr/lib/python2.7/config-i386-linux-gnu Command line pattern: ${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} Libraries: Libraries(-I): python2.7 Library search path(-L): /usr/lib/python2.7/config-i386-linux-gnu 

Result :: Linker errors that were obtained earlier from the compilation of the C ++ Eclipse project no longer occur.

0
source

Source: https://habr.com/ru/post/980219/


All Articles