title: Embedded python in c++ on raspberry pi
body: <p>Before I asked for how to call c++ in python, but that's quite difficult to me. And later on I found that if calling python in c++ seems much more easily. As I followed <a href="http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I" rel="nofollow">the tutorial on codeproject</a>, I've got the following problem while I compile it.</p>
  
  <pre><code>"pi@raspberrypi ~/New $ g++ led.cpp"
  
  led.cpp:3:20: fatal error: Python.h: No such file or directory
  compilation terminated.
  </code></pre>
  
  <p>I searched solutions whole days. I tried <code>sudo apt-get install python-dev</code> (even other versions, eg. 2.7, 3.2, etc..), but everything are already completely installed and still get the error. I can found the location of Python.h by find function of my raspberry pi.</p>
  
  <p>Any finally I found the following solution on some site:</p>
  
  <pre><code>pi@raspberrypi ~/New $ g++ $(python-config --includes) led.cpp
  </code></pre>
  
  <p>Python.h fatal error dismissed, and following error comes.</p>
  
  <pre><code>/tmp/ccSIJpeH.o: In function `main':
  led.cpp:(.text+0x30): undefined reference to `Py_Initialize'
  led.cpp:(.text+0x44): undefined reference to `PyString_FromString'
  led.cpp:(.text+0x54): undefined reference to `PyImport_Import'
  led.cpp:(.text+0x64): undefined reference to `PyModule_GetDict'
  led.cpp:(.text+0x84): undefined reference to `PyDict_GetItemString'
  led.cpp:(.text+0x94): undefined reference to `PyCallable_Check'
  led.cpp:(.text+0xbc): undefined reference to `PyObject_CallObject'
  led.cpp:(.text+0xc4): undefined reference to `PyErr_Print'
  led.cpp:(.text+0x158): undefined reference to `Py_Finalize'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Please tell me how to solve this!</p>
  
  <p><strong>Addendum</strong>: Thank you Nitori answer my question. I can compile it now. After compiled it, it created an "a.out" file, but when I runs it it done nothing...</p>
  
  <p>This is the code of led.cpp</p>
  
  <pre><code>// python functions from C code
  // 
  #include &lt;Python.h&gt;
  
  int main(int argc, char *argv[])
  {
      PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
  
      if (argc &lt; 3) 
      {
          printf("Usage: exe_name python_source function_name\n");
          return 1;
      }
  
      // Initialize the Python Interpreter
      Py_Initialize();
  
      // Build the name object
      pName = PyString_FromString(argv[1]);
  
      // Load the module object
      pModule = PyImport_Import(pName);
  
      // pDict is a borrowed reference 
      pDict = PyModule_GetDict(pModule);
  
      // pFunc is also a borrowed reference 
      pFunc = PyDict_GetItemString(pDict, argv[2]);
  
      if (PyCallable_Check(pFunc)) 
      {
          PyObject_CallObject(pFunc, NULL);
      } else 
      {
          PyErr_Print();
      }
  
      // Clean up
      Py_DECREF(pModule);
      Py_DECREF(pName);
  
      // Finish the Python Interpreter
      Py_Finalize();
  
      return 0;
  }
  </code></pre>
  
  <p>Here is my python code to call. led.py</p>
  
  <pre><code>import RPi.GPIO as GPIO
  import time
  
  def ledopen():
      GPIO.setmode(GPIO.BCM)
      GPIO.setup(17, GPIO.OUT)
      GPIO.setup(27, GPIO.OUT)
      GPIO.setup(22, GPIO.OUT)
      GPIO.setup(18, GPIO.OUT)
      GPIO.setup(23, GPIO.OUT)
      GPIO.setup(24, GPIO.OUT)
  
      GPIO.output(27, True)
      GPIO.output(22, True)
      GPIO.output(18, True)
      GPIO.output(23, True)
      GPIO.output(24, True)
      GPIO.output(17, True)
  
      time.sleep(5)
  
      GPIO.output(27, False)
      GPIO.output(22, False)
      GPIO.output(18, False)
      GPIO.output(23, False)
      GPIO.output(24, False)
      GPIO.output(17, False)
  
      GPIO.cleanup()
      return
  ledopen()
  </code></pre>
  
  <p>and I called the program by entering "sudo ./a.out led led ledopen". And it done nothing.</p>