Clion error cmake python embedded

1.2k views Asked by At

I try to compiled a simple example of python embedded in Clio 1.0.3 with MingGw. The source main.cpp is:

#include <iostream>
#include "Python.h"
using namespace std;

int main() {

Py_Initialize();
PyRun_SimpleString("print('Hello World from Embedded Python!!!')");
Py_Finalize();

return 0;
}

My CMakeList.txt files is:

cmake_minimum_required(VERSION 3.2)
project(pruebapy)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("C:\\SoftwareExtras\\Python27\\include")
set(CMAKE_LIBRARY_PATH "C:\\SoftwareExtras\\Python27\\libs")
set(SOURCE_FILES main.cpp)
add_executable(pruebapy ${SOURCE_FILES})

But when buils generate the following error:

Linking CXX executable pruebapy.exe
CMakeFiles\pruebapy.dir/objects.a(main.cpp.obj): In function `main':
C:/pruebapy/main.cpp:10: undefined reference to      `_imp__Py_Initialize'
C:/pruebapy/main.cpp:12: undefined reference to `_imp__PyRun_SimpleStringFlags'
C:/pruebapy/main.cpp:14: undefined reference to `_imp__Py_Finalize'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\pruebapy.dir\build.make:87: recipe for target 'pruebapy.exe' failed
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/pruebapy.dir/all' failed
makefile:74: recipe for target 'all' failed
mingw32-make.exe[2]: *** [pruebapy.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/pruebapy.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2

I try different CMake configurations, but the error persist. How I can resolve the problem?.

Than you for your help.

1

There are 1 answers

0
Danilushka On

According to the Python documentation, the include directive for “Python.h” should appear first in the C/C++ file.

"Note Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included." https://docs.python.org/2/extending/extending.html

Try that first.

If you get long_bit errors it seems to be due to a compiler mismatch for Python and CygWWin or MinGw. Try the Early Access version of Clion with MS Visual Studio compiler support since most Pythons (all?) are compiled with that compiler.