0

I want to add some functions written in Python to my C++ program:

#include <iostream>
#include <Python.h>

using namespace std;

int main(){
    int a = 0;
    cout << a;
    return 0;
}

But when I compile this program using commend g++ main.cpp -Wall -o main I have an error: fatal error: Python.h: No such file or directory

I was trying to solve the problem by installing python2.7 -dev by sudo apt-get install python2.7-dev, but it didn't help. Can someone suggest what more I can do to fix this problem?

2 Answers 2

2

Additional 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.

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

1 Comment

This should be a comment rather than an answer, as it does not solve the problem, but rather attempts to head off an upcoming additional problem after the main one is fixed.
1

You need to tell the compiler where to find the Python headers. For example, on some systems you'd do this:

g++ -I /usr/include/python2.7 ...

4 Comments

I made g++ -I usr/include/python2.7/Python.h main.cpp -Wall -o main but I still have the same error. Am I doing something wrong?
You added Python.h to the command line. Don't do that, do what I wrote (the ... is meant to be your main.cpp and the rest). Read the manual regarding the -I option.
@Rop you're also missing a leading / on that path
@JohnZwinck For my the commend: g++ -I/usr/include/python2.7 -lpython2.7 main.cpp -Wall -o main works good now.

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.