0

I am facing a linking error for Py_initialize though I have a reference to my Python library in my CMake. The Python.h is found but the link is not established.

inpainting.cpp:(.text+0x47843): undefined reference to `Py_Initialize'
/usr/bin/ld: inpainting.cpp:(.text+0x47854): undefined reference to 
`PyRun_SimpleStringFlags'
/usr/bin/ld: inpainting.cpp:(.text+0x47940): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/imgCodecs.dir/build.make:552: imgCodecs] Error 1
make[1]: *** [CMakeFiles/Makefile2:84: CMakeFiles/imgCodecs.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Here is my CMakeList. https://pastebin.com/QkihgBGm

Edit: Adding how I am trying to compile. I do Cmake and a make on my makefile.

Here is the C++ code that I am trying to compile.

// *********** Begin: Run Python script ************************************************************
// Initialize the Python Interpreter
Py_Initialize();
PyRun_SimpleString("import sys");
char buff[FILENAME_MAX]; //create string buffer to hold path
getcwd( buff, FILENAME_MAX);
string current_working_dir(buff);
string shore_working_dir = current_working_dir + 
"/shore_imputation/";
const char * shore_working_dir_c = shore_working_dir.c_str();
qDebug() << shore_working_dir_c;
// Finish the Python Interpreter
Py_Finalize();
// *********** End: Run Python script 
**************************************************************
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Scheff's Cat Feb 27 '22 at 12:21
  • Whether or not you used the correct `#include` hasn't anything to do with an undefined reference. It ensures that your code (which is using `PyInitialize()`) _compiles_ properly. The Undefined Reference rather indicates that you didn't _link_ against the (proper) Python _library_. Please, [edit] and expose how you _compile and link_ your C++ code. – Scheff's Cat Feb 27 '22 at 12:22
  • FYI: [Python Doc.: Compiling and Linking under Unix-like systems](https://docs.python.org/3/extending/embedding.html#compiling-and-linking-under-unix-like-systems) – Scheff's Cat Feb 27 '22 at 12:34
  • I am not sure if that really answers and help me identify where I am struggling. – theNinthElement Feb 27 '22 at 21:06
  • Did you add Python dependencies somewhere in your CMake scripts? What did you add? CMake provides a find script for Python: [FindPython](https://cmake.org/cmake/help/latest/module/FindPython.html). I just had a look how we did add Python dependencies in our S/W. There is just `PYTHON` in the resp. `target_link_libraries()`. However, I would take this with a grain of salt as there is quite a lot of hand-knitted stuff in our scripts, and we don't use a "regular" installation of Python but copied the relevant binaries in an own directory (for deploy and other reasons). – Scheff's Cat Feb 28 '22 at 06:04

1 Answers1

0

After many hours of investigation, it seems Cmake is case sensitive and it required an explicit call to link to the libraries.

target_link_libraries(SOURCE ${OpenCV_LIBS})
target_link_libraries(SOURCE ${PYTHON_LIBRARIES})

This made it work and bingo... Cheers!