Reputation: 367
I started working with CMake yesterday, so still very, very, novice. I am trying to embed some python in c, more precisely I want to call a python script from my c program. I have been going off a few resources online and I have gotten the best results following this post, CMake not linking Python.
As for embedding my python code, my implementation is pretty much a 1:1 of this python documentation example, section 5.3.
In my CMakeLists.txt I have
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_library(TARS SHARED ${SOURCES})
target_link_libraries(TARS ${PYTHON_LIBRARIES})
which builds but gives me a resulting libTARS.so. My problem is such that I need this to be an executable and I am not sure how to do just that. I have played around with something similar to
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_library(TARS SHARED ${SOURCES})
target_link_libraries(TARS ${PYTHON_LIBRARIES})
add_executable(TARSEXEC ${SOURCES})
target_link_libraries(TARSEXEC TARS)
which gives me a few errors for undefined references to some python wrapper functions. Like I said, super new to CMake so I might be making some very obvious and silly mistakes. Any help or nudges in the right direction would be appreciated.
Cheers
Update:
I have gone for the following CMake config
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(TARS ${SOURCES})
target_link_libraries(TARS ${PYTHON_LIBRARIES})
And it is giving me the following errors
/home/alxhoff/git/siemens/siemens-personal/TARS/src/main.c:101: undefined reference to `PyString_FromString'
/home/alxhoff/git/siemens/siemens-personal/TARS/src/main.c:139: undefined reference to `PyInt_AsLong'
/home/alxhoff/git/siemens/siemens-personal/TARS/src/main.c:160: undefined reference to `Py_Finalizer'
After some grepping I have found that the default 3.5 version of python lacks these functions whilst 2.7 contains them. I now just need to force CMake to use 2.7.
Solved using the following
set(PYTHON_EXECUTABLE "/usr/bin/python2.7")
set(PYTHON_INCLUDE_DIR "/usr/include/python2.7")
set(PYTHON_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpython2.7.so")
Upvotes: 3
Views: 2453
Reputation: 367
Adding the following to CMakeLists.txt
find_package(PythonLibs REQUIRED)
set(PYTHON_EXECUTABLE "/usr/bin/python2.7")
set(PYTHON_INCLUDE_DIR "/usr/include/python2.7")
set(PYTHON_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpython2.7.so")
include_directories(${PYTHON_INCLUDE_DIRS})
add_executable(TARS ${SOURCES})
target_link_libraries(TARS ${PYTHON_LIBRARIES})
thus forcing the use of python 2.7
Upvotes: 2