4

I have created two files embed.py useEmbed.cppin my home directory.

embed.py

import os

print os.getcwd()

useEmbed.cpp

  #include <iostream>
  #include "Python.h"
  using namespace std;

  int main()
  {
       Py_Initialize();
       PyRun_SimpleFile("embed.py");
       Py_Finalize();

      return 0;
  }

Command g++ useEmbed.cpp -o useEmbed returns Python.h not found, What should i do next step to make .cpp file compiled successfully and return the right answer? Thank for tips about how to set environment to make this test OK.

Thank you!

UPDATE: Thanks for tips from David and Alexander. Problem has been solved after install package python-devel in my Fedora Linux.

2 Answers 2

3

On linux, you can use python-config to get the compiler flags (python-config --cflags) and linker flags (python-config --ldflags).

For example:

#> python-config --cflags
-I/usr/include/python2.5 -I/usr/include/python2.5 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes

#> python-config --ldflags
-L/usr/lib/python2.5/config -lpthread -ldl -lutil -lm -lpython2.5

To compile your program you can run g++ useEmbed.cpp -o embed "cflags" "ldflags" :

#> g++ useEmbed.cpp -o embed -I/usr/include/python2.5 -I/usr/include/python2.5 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -L/usr/lib/python2.5/config -lpthread -ldl -lutil -lm -lpython2.5

I had to change useEmbed.cpp a little bit:


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

    using namespace std;

    int main()
    {
      Py_Initialize();
      FILE *file = fopen("embed.py","r+");
      PyRun_SimpleFile(file,"embed.py");
      Py_Finalize();
      fclose(file);

      return 0;
    }


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

Comments

3

Make sure you point the compiler to the directory where Python.h is located, i.e. use the -I<path> switch with gcc. Of course you need to have the Python development files installed.

1 Comment

With python source code directory, use gcc -I <path to Include/> useEmbed.cpp -o embed, returns pyconfig.h not found. How to do with python source file directoty?

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.