Skip to content Skip to sidebar Skip to footer

Compile C++ With Embedded Python Using Cmake Import Error

I'm trying to include a python file in a C++ project compiled using CMake. First I did this standalone using these two files: #include #include in

Solution 1:

So I finally found the answer myself.

The problem was that when linking in CMake the linkage with python libraries are only loaded locally (RTLD_LOCAL), which means the actual python script isn't linked with the python libraries, only C++.

To fix this just load the python libraries with global symbol resolution the first thing in your C++ code.

dlopen("libpython2.6.so.1.0", RTLD_NOW | RTLD_NOLOAD | RTLD_GLOBAL);

This is a quick and dirty fix in that it is specific to python2.6, you should make this portable by defining the variable in CMake based on the version installed on the machine.

You will have to import dlfcn to use dlopen:

#include<dlfcn.h>

Post a Comment for "Compile C++ With Embedded Python Using Cmake Import Error"