0

So this is my first time trying to use python within a C++ program, and i am running into some problems with getting the library linked.

So i use the Code Blocks IDE with the GNU GCC compiler, and i have the following main program:

#include <Python.h> #include <iostream> using namespace std; int main() { cout<<"starting interpreter."<<endl; Py_Initialize(); PyRun_SimpleString("print 'Im in python!'"); Py_Finalize(); return 0; } 

and my linking settings are as follows (within code blocks GUI for compiler&debugger settings):

linker settings: link libraries: C:\Python27\libs\libpython27.a search directories: linker: C:\Python27\libs 

Is there something that i am missing? or am i doing this the wrong way?

Build messages:

C:\Users\users_name\Desktop\PythonIntegratedTest\main.cpp|1|error: Python.h: No such file or directory| C:\Users\users_name\Desktop\PythonIntegratedTest\main.cpp||In function 'int main()':| C:\Users\users_name\Desktop\PythonIntegratedTest\main.cpp|9|error: 'Py_Initialize' was not declared in this scope| C:\Users\users_name\Desktop\PythonIntegratedTest\main.cpp|10|error: 'PyRun_SimpleString' was not declared in this scope| C:\Users\users_name\Desktop\PythonIntegratedTest\main.cpp|11|error: 'Py_Finalize' was not declared in this scope| 
7
  • You should copy, paste and post the exact error you're getting -- it matters. Commented Jun 26, 2012 at 20:00
  • 1
    Change ~\libs to ~\include in your search directories, pls. Commented Jun 26, 2012 at 20:27
  • I tried that, the same error occured :/ do i have to change my linked library aswell? Commented Jun 26, 2012 at 20:33
  • Are you sure you changed it at the correct point and the errors keep being exactly the same? For CodeBlocks, goto Settings - Compiler and debugger - Search directories - Compiler and add C:\Python27\include to the list. This should fix the Python.h not found issue and the others along. Commented Jun 26, 2012 at 20:41
  • @NiklasR ok, so i changed that, and now im just getting errors about undefined references to the Python functions. Commented Jun 26, 2012 at 20:47

2 Answers 2

1

The error message says Python.h: No such file or directory. This means, the compiler cannot find the requested file. The path in your search directories is not correct. Header-files that need to be included are usually in a directory called include. For Python on Windows, this is C:\Python27\include in your case.

In CodeBlocks, you can modify the include search directories under Settings - Compiler and debugger - Search directories - Compiler.

After you've done that, you will get undefined reference to errors. The error message tells you, that you have used a function in your code the compiler can not find the implementation for, but only the declaration (in the header file).

The implementation can be either available in a source-file or in a static library. CPython on Windows comes with pre-built static libraries. It is located under C:\Python27\libs\python26.lib in your case

After changing that as well, compilation should succeed.

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

Comments

0
main.cpp|1|error: Python.h: No such file or directory 

is a compile-time error because your compiler can't find Python.h in the include search path. If you were using gcc on the command line, I'd tell you to specify your Python install's include folder, as in:

-I/path/to/Python27/include 

(on my Windows Python 2.7 install, its C:\Python27\include)

I'm not sure how you would do this in CodeBlocks but certainly there is a way to specify your "header include paths".

Just note that this isn't the same as "library search path" or "linker search path" -- its specifically for the compiler and the header search locations.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.