3

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
  • Hello I can resolve the conflict. I detect in the installation of gnu for windows exists two version 3.4 and 4.9 of gcc. Then when linker the object file, the linker take version 3.4, but the sources is compiled with 4.9. I unisntall 3.4 and resolve the problem. Thank You. Commented Jun 17, 2015 at 14:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.