Python C/C ++ Mixed Programming

Source: Internet
Author: User
Tags gtk

 

Pythonpdf book: http://www.oschina.net/bbs/thread/9780python: includes calling APIs in C ++ in Python and calling Python scripts in C ++. Build a python and C ++ hybrid system: Drawing World Program: Drawing http://bambooice.blog.hexun.com/37655834_d.html#include "python. H "Compilation option. You must manually specify the include path and link path of Python. Code: G ++ pythonconsole. CPP-I/usr/local/include/python2.7-L/usr/local/lib /Python2.7-lpython2.7c language editing method gcc emu. c-l/usr/lib/python2.2/config-lpython2.2-lpthread-lm-LDL-lutil/usr/local/include/python2.7 // usr/local/lib/python2.7 // in usr/local/share/man/Man1/python2.7.1c/C ++, how to call the Python script in the python method C ++ is not worth mentioning, at least you can regard it as a dynamic link library in the form of text, and you can change it as needed. As long as the interface is not changed, once the C ++ program is compiled, it's not that easy to change. first look at the Python code: # test function def add (A, B): Print "in Python function Add" print "A =" + STR () Print "B =" + STR (B) print "ret =" + STR (a + B) Return def Foo (): print "in Python function foo" print "A =" + STR (a) print "ret =" + STR (A * A) return save the above Python code as pytest. py is followed by the code of C ++: # include "python. H "int main (INT argc, char ** argv) {// initialize Python // before using the python system, you must use py_initialize to initialize it. It loads Python built-in modules and adds the system path // path to the module search path. This function has no return value. You need to use py_isinitialized to check whether the system is initialized successfully. Py_initialize (); // check whether Initialization is successful if (! Py_isinitialized () {return-1;} // Add the current path // run the input string directly as the Python code. If the returned value is 0, it indicates that the input string is successful, and if the value is-1, it indicates that the input string is incorrect. Most errors are caused by syntax errors in the string. Pyrun_simplestring ("Import sys"); pyrun_simplestring ("sys. path. append ('. /') "); pyobject * pname, * pmodule, * pdict, * pfunc, * pargs; // load the script pname = pystring_fromstring (" pytest ") named pytest "); pmodule = pyimport_import (pname); If (! Pmodule) {printf ("can't find pytest. py"); getchar (); Return-1;} pdict = pymodule_getdict (pmodule); If (! Pdict) {return-1;} // find the function named add pfunc = pydict_getitemstring (pdict, "add"); If (! Pfunc |! Pycallable_check (pfunc) {printf ("can't find function [add]"); getchar (); Return-1;} // stack the parameter * pargs; pargs = pytuple_new (2); // pyobject * py_buildvalue (char * format ,...) // convert the C ++ variable into a python object. This function is used when you need to pass variables from // C ++ to Python. This function // is somewhat similar to C's printf, but the format is different. The commonly used formats include // s to represent strings, // I to represent integer variables, // F to represent floating-point numbers, and // o to represent a python object. Pytuple_setitem (pargs, 0, py_buildvalue ("L", 3); pytuple_setitem (pargs, 1, py_buildvalue ("L", 4 )); // call the python function pyobject_callobject (pfunc, pargs); // find the function Foo and execute Foo pfunc = pydict_getitemstring (pdict, "foo"); If (! Pfunc |! Pycallable_check (pfunc) {printf ("can't find function [Foo]"); getchar (); Return-1;} pargs = pytuple_new (1); pytuple_setitem (pargs, 0, py_buildvalue ("L", 2); // pyobject_callobject (pfunc, pargs); py_decref (pname); py_decref (pargs); py_decref (pmodule ); // disable the python py_finalize (); Return 0;} compilation option. You must manually specify the include path and link path of Python. The code is g ++ pythontest. CPP-I/usr/local/include/python2.7-L/usr/local /Lib/python2.7-lpython2.7-lpthread-LDL-lm-lutil if your Python version number is different from mine, Please modify your own version number original address: http://coredump.blog.bokee.net/bloggermodule/blog_printEntry.do? Id = 909014 reference: 1. Python Chinese community: export ls Python *-llrwxrwxrwx 1 Root 9 Python-> export 1 Root 9 python2-> python2.6-rwxr-XR-x 1 Root 2230352 python2.6/ usr/bin $ which Python/usr/local/bin/pythonimport GTK importer Ror: no module named GTK/usr/lib/python2.5/Site-packages/usr/local/lib/python2.7/Site-packages/usr/local/lib/python2.6/Site-packagesthis directory exists so that 3rd party packages can be installedhere. read the source for site. PY for more details. site. when installing py, choose GTK> Import sys> print sys. path ['', '/usr/local/lib/python27.zip', '/usr/local/lib/python2.7', '/usr/local/lib/python2.7/Plat-l Inux2 ','/usr/local/lib/python2.7/lib-TK ','/usr/local/lib/python2.7/lib-old ', '/usr/local/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/Site-packages']> print sys. path ['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-TK ', '/usr/lib/python2.6/lib-old','/usr/lib/python2.6/lib-dynload ','/usr/lib/python2.6/dist-packages ', '/usr/lib/python2.6/dist-Package S/PIL ','/usr/lib/python2.6/dist-packages/gst-0.10 ','/usr/lib/pymodules/python2.6 ', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0 ', '/usr/local/lib/python2.6/dist-packages'] >>> display the python site-packages path: $ Python-c "from distutils. sysconfig import get_python_lib; print get_python_lib () "/usr/local/lib/python2.7/Site-packagesconfiguring additional built-in Modules --------------------------------------- Starting with Python 2.1, the setup. PY script at the top of the sourcedistribution attempts to detect which modules can be built andautomatically compiles them. autodetection doesn' t always work, soyou can still customize the configuration by editing the modules/setupfile; but this shocould be considered a last resort. the rest of thissection only applies if yo U decide to edit the modules/setup file. you also need this to enable static linking of certain modules (whichis needed to enable profiling on some systems ). after you manually compile and install python, ibus cannot use the manual compilation and installation of python2.6, and later ibus cannot be used. The following traceback (most recent call last) is displayed: file "/usr/share/ibus/Setup/main. PY ", line 28, in <module> Import gtkimporterror: No module named GTK. However, python2.5.4 has been okay and won't be compiled after python, should all the software related to it be re-compiled ?? Cough... It seems yes... at least all Python modules should be re-compiled, for example, pygtk or something. Re-compiled python, all Python modules must be compiled. To compile pygtk, you must compile a lot of dependencies. Therefore, if you only want your own python, compile it to a different directory from the system and rename the python main program. To install pygtk on Linux, install the pygtk package for python 2.6.5 on Linux as required by the project. One thing that I thought was very simple, or install it using easy_install or source code compilation and installation, is very simple. I didn't want to do it for a day, but did not solve the problem during the period, the dependency between installation files is too long. First, installing pygtk using easy_install won't work. Because it is not only necessary to compile C code, but also to check the machine status, it is necessary to install it in the form of regular source code compilation, such as configure, make, and make install. In addition, the installation of pygtk depends on pygobject and gobject-introspection. To install gobject-introspection, you must install glib. To install glib, you may install and upgrade gettext, because the old gettext version does not recognize some new methods in glib. In addition, when installing these packages, the version number must not be lower than that of XX. As a result, the installation and uninstallation are complicated and fail. I just read the pygtk official website and it seems that it can be installed in windows. Try again later this weekend to try to install pygtk successfully. Python-gobject

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.