0

I'm trying to create a "helloworld" app in C++, with an integrated Python code. I include Python.h, and it is seen by static analyzer fine. As I build my code I get undefined reference to '_imp__Py_Initialize', so make failed

I'm running Windows7-x64 with Python3.5-x32, MinGW-x64, CLion to write code. None of guides i found show the complete step-by-step solutions. If I have to re-install some specific versions of software, I need to know - what to do...

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)
project(sample)
find_package(PythonLibs 3.5 REQUIRED)
find_package(PythonInterp 3.5 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 14)
add_executable(sample main.cpp)

main.cpp

#include <Python.h>
int main() {
  Py_Initialize();
  PyRun_SimpleString("print('hello, python')");
  Py_Finalize();
  return 0;
}

So the subject raises, i have an undefined reference. As i add target_link_libraries(${PYTHON_LIBRARIES}) to my CMakeLists.txt i get another exception:

Cannot specify link libraries for target
"C:/Users/...../Python35-32/python35.dll"
which is not built by this target.

Please provide me some more ideas, what to do with this, if you don't mind.

5
  • 1
    target_link_libraries(${PYTHON_LIBRARIES}) takes the target as first parameter. Your target is sample, so try with target_link_libraries(sample ${PYTHON_LIBRARIES}) Commented May 27, 2019 at 13:04
  • ``` Cannot specify link libraries for target "sample" which is not built by this project ``` Commented May 27, 2019 at 13:07
  • Did you put that line before or after the add_executable? Commented May 27, 2019 at 13:10
  • It somehow works if i put it after. BUT WHY? can you, please post your answer as an actual answer so i can vote for it, and mark as a solution? Commented May 27, 2019 at 13:15
  • See if the answer also covers the"why" part ;) Commented May 27, 2019 at 13:18

1 Answer 1

1

target_link_libraries takes the target as first parameter. From the documentation of target_link_libraries:

The named target must have been created in the current directory by a command such as add_executable() or add_library(). The remaining arguments specify library names or flags.

Your target is sample, not because of the project(sample) at the beginning, but because of add_executable(sample main.cpp), so add

`target_link_libraries(sample ${PYTHON_LIBRARIES})` 

after the add_executable line.

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.