How to add an external library (path to .h file) ? CMakeLists not updating

Hello all,
I would like to run some Python commands from “jetson-inference/imagenet-camera/imagenet-camera.cpp”. For example,

PyRun_SimpleString("import sys");

In the beginning of the “imagenet-camera.cpp” I have included #include <Python.h>. Also I have added the path to Python.h from Python2.7 and Python3.6m in CMakeLists.txt. I ran “cmake …/” and “make” again, but still the compiler cannot find the path to Python.h. I have also tried to include the full path like #include </usr/include/python2.7/Python.h> but it did not work either

I added the path to CMakeLists.txt as following:

# build C/C++ interface
include_directories(${PROJECT_INCLUDE_DIR} ${GIE_PATH}/include)
include_directories(/usr/include/gstreamer-1.0 /usr/lib/aarch64-linux-gnu/gstreamer-1.0/include /usr/include/glib-2.0 /usr/include/libxml2 /usr/lib/aarch64-linux-gnu/glib-2.0/include/ /usr/include/python3.6m/ /usr/include/python2.7/)

Here is the error I am getting:

CMakeFiles/imagenet-camera.dir/imagenet-camera.cpp.o: In function `main':
imagenet-camera.cpp:(.text+0x1b4): undefined reference to `Py_Initialize'
imagenet-camera.cpp:(.text+0x1c4): undefined reference to `PyRun_SimpleStringFlags'
imagenet-camera.cpp:(.text+0x1d4): undefined reference to `PyRun_SimpleStringFlags'
imagenet-camera.cpp:(.text+0x1e4): undefined reference to `PyRun_SimpleStringFlags'
imagenet-camera.cpp:(.text+0x1f4): undefined reference to `PyRun_SimpleStringFlags'
imagenet-camera.cpp:(.text+0x204): undefined reference to `PyRun_SimpleStringFlags'
CMakeFiles/imagenet-camera.dir/imagenet-camera.cpp.o:imagenet-camera.cpp:(.text+0x214): more undefined references to `PyRun_SimpleStringFlags' follow
CMakeFiles/imagenet-camera.dir/imagenet-camera.cpp.o: In function `main':
imagenet-camera.cpp:(.text+0x91c): undefined reference to `Py_Finalize'
collect2: error: ld returned 1 exit status
imagenet-camera/CMakeFiles/imagenet-camera.dir/build.make:110: recipe for target 'aarch64/bin/imagenet-camera' failed
make[2]: *** [aarch64/bin/imagenet-camera] Error 1
CMakeFiles/Makefile2:179: recipe for target 'imagenet-camera/CMakeFiles/imagenet-camera.dir/all' failed
make[1]: *** [imagenet-camera/CMakeFiles/imagenet-camera.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

Thank you in advanced!

Hi gharibimo, did you run cmake again after making a change to the CMakeLists.txt?

Also, you may try editing the CMakeLists for the imagenet-camera program, located at jetson-inference/imagenet-camera/CMakeLists.txt

Baring that, are you sure the Python header files you are trying to include are actually there?

Hello @dusty_nv Thank you so much for your reply!

Yes, I run cmake then make after every change.

and yes, the header files exist in the path I am providing.

Well, I am adding the paths only in the general CMake jetson-inference/CMakeLists.txt. However, I am not sure how exactly should I add the paths in jetson-inference/imagenet-camera/CMakeLists.txt

Would you please let me know what exactly should I add. This is what currently there:

file(GLOB imagenetCameraSources *.cpp)
file(GLOB imagenetCameraIncludes *.h )

cuda_add_executable(imagenet-camera ${imagenetCameraSources})

target_link_libraries(imagenet-camera jetson-inference)

install(TARGETS imagenet-camera DESTINATION bin)

Hi gharibimo, try this jetson-inference/imagenet-camera/CMakeLists.txt and see if this helps:

file(GLOB imagenetCameraSources *.cpp)
file(GLOB imagenetCameraIncludes *.h )

include_directories(/usr/include/python3.6/ /usr/include/python2.7/)

cuda_add_executable(imagenet-camera ${imagenetCameraSources})

target_link_libraries(imagenet-camera jetson-inference)

install(TARGETS imagenet-camera DESTINATION bin)

Hi @dusty_nv , that did not work :(

I deleted all the build directory then made the changes you mentioned in jetson-inference/imagenet-camera/CMakeLists.txt , and then cmake and make again. But still the same error. The CMake file still not including the new paths :(

OK, run make as make VERBOSE=1 and make will print out the full command line to the compiler, and you should see your python libraries included on the command line there. That will let you know if CMake is actually including them or not, or if something is wrong with the paths.

@dusty_nv Thank you so much for your help!

After many attempts I found out that any change to jetson-inference/CMakeLists.txt or jetson-inference/imagenet-camera/CMakeLists.txt will not affect anything.

The only way to add the executable library “Python.h” is to add -I/usr/include/python2.7 in build/imagenet-camera/CMakeLists/flags.make. Also you need to add the link -lpython2.7 in build/imagenet-camera/CMakeLists/links.txt.

Finally, build the project using “make imagenet-camera” instead of building the entire repo using only “make”.