Python mixed programming with C + +

Source: Internet
Author: User

Python calls each other with A/C + + module

Python calls c dynamic-link library
Python's call to C library is simple, packaged into so without any encapsulation, and then using Python's ctypes call.
<test.cpp Generating source files for dynamic libraries >

    1. #include <stdio.h>
    2. extern "C" {
    3. void display () {
    4. printf ("This is Display function\n");
    5. }
    6. }
    7. g++ Test.cpp-fpic-shared-o libtest.so

<call.py calling the source file of the dynamic library >

    1. Import cTYPES
    2. so = cTYPES. Cdll ("./libtest.so")
    3. So.display ()

It is important to note that when you use g++ to compile a function or method in a code that generates a dynamic library, you need to use extern "C" to compile

Python calls C + + (with class, overloaded) dynamic-link libraries
But call C + + so is a bit of a hassle, the Internet to find the next, most of the need for extern "C" to assist, that is, can only call C function can not call the method directly but can parse C + + method.
<test.cpp Generating source files for dynamic libraries >

  1. #include <Akita/Akita.h>
  2. class testlib{
  3. Public:
  4. void display ();
  5. void display (int a);


  6. };
  7. void TestLib::d isplay () {
  8. cout<<"First display"<<endl;
  9. }


  10. void TestLib::d isplay (int a) {
  11. cout<<"Second display"<<endl;
  12. }
  13. extern "C" {
  14. TestLib obj;
  15. void display () {
  16. Obj.display ();
  17. }
  18. void Display_int () {
  19. Obj.display (2);
  20. }
  21. }

g++ Test.cpp-fpic-shared-o libtest.so
Using this method is a bit of a hassle but can solve the problem. Notice that there will still be an extern "C" or the built-in dynamic link library does not have the symbolic table of these functions.

<call.py calling the source file of the dynamic library >

    1. Import cTYPES
    2. so = cTYPES. Cdll ("./libtest.so")
    3. So.display ()
    4. So.display_int (1)

The results of the operation are as follows:

    1. ^[[email Protected]:~/projects/nugget/kvdb-py] #python call.py
    2. First display
    3. Second Display

C + + Call Python module

<test.cpp >

  1. #include <Akita/Akita.h>
  2. #include <Python.h>
  3. int Main () {
  4. Py_initialize ();
  5. if (! Py_isinitialized ()) return FALSE;
  6. Pyrun_simplestring ("Import sys");
  7. Pyrun_simplestring ("sys.path.append ('./')");

  8. //import Module
  9. pyobject* pmodule = pyimport_importmodule ("Hello");
  10. if (!pmodule) {
  11. cout<<"Can ' t import module!/n"<<endl;
  12. return -1;
  13. }

  14. pyobject* pdict = pymodule_getdict (pmodule);
  15. if (!pdict) {
  16. return -1;
  17. }

  18. //fetch Function
  19. pyobject* Pfunhi = pydict_getitemstring (pdict, "display");
  20. Pyobject_callfunction (Pfunhi, "s", "Crazybaby");
  21. Py_decref (PFUNHI);

  22. //release
  23. Py_decref (Pmodule);
  24. Py_finalize ();
  25. return 0;
  26. }

#g + + test.cpp-i/usr/local/include/python2.7-ldl-lutil-lpthread-lpython2.7


<call.py>

    1. def Display (name):
    2. Print "HI", name

———

C + + writes extension modules for Python
Python provides scripting interfaces for C + +.

The interaction between the two is very convenient.


From:blog.csdn.net/crazyjixiang Crazyjixiang

Python mixed programming with C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.