Embedding Python in C, linking fails with undefined reference to `Py_Initialize'

前端 未结 5 1410
陌清茗
陌清茗 2021-02-14 02:21

I am trying to compile the example from the docs https://docs.python.org/2.7/extending/embedding.html and my code looks exactly like the one under 5.1:

#include          


        
5条回答
  •  感动是毒
    2021-02-14 02:59

    I replicated the same problem on Lubuntu14.04 32bit virtual machine running on Win7 machine.

    I did the following things to replicate the problem in the first place where the C++ code is written in Eclipse C/C++ IDE. Defined a C++ Eclipse project titled "test". The source file contains the following C++ code which is the same as mentioned above by Miguellissimo.

    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 function main': /home/ros/workspace/test/src/test.cpp:15: undefined reference toPy_Initialize' /home/ros/workspace/test/src/test.cpp:17: undefined reference to PyRun_SimpleStringFlags' /home/ros/workspace/test/src/test.cpp:18: undefined reference toPy_Finalize' collect2: error: ld returned 1 exit status

    The output of the following commands was the same as mentioned before 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
    

    Project Properties of C++ Eclipse Project titled "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): 
    

    Solution::

    Specified the following C++ Build Settings in the Project Properties of C++ Eclipse Project "test"

    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 which resulted previously from the compilation of C++ Eclipse project doesn't occur anymore.

提交回复
热议问题