Clion cmake python error inline

I am trying to compile a simple python example built in Clio 1.0.3 with MingGw. Source main.cpp:

#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:

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 generates 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 am using different CMake configurations but the error persists. How can I solve the problem?

Than you for your help.

+3


source to share


1 answer


According to the Python documentation, the include directive for "Python.h" must appear first in the C / C ++ file.

"Note: Since Python may define some preprocessor definitions that affect standard headers on some systems, you must include Python.h before including any standard headers." https://docs.python.org/2/extending/extending.html



Try it first.

If you get long_bit errors, it looks like it is due to a compiler mismatch for Python and CygWWin or MinGw. Try the early access version of Clion with MS Visual Studio compiler support as most Pythons (all?) Are compiled with this compiler.

+1


source







All Articles