Interoperability Between Python and C ++

Source: Internet
Author: User
Use the C ++ module in Python to directly import the dynamic link library file of C ++. In Windows, you need to change the DLL file extension to. PYD. Use. So directly in Linux/Unix. Python modules written in C ++ are dynamic link library files. This is the key to changing the use of common functions in windows. Code : Static pyobject * Ge (pyobject * Self, pyobject * ARGs) {STD: String STS; STS = "help me"; return py_buildvalue ("S", Sts. c_str ();} // This is the processing function static pymethoddef allmehod [] ={{ "Ge", GE, meth_varargs}, {null, null }}; // map the function to a python method # ifdef build_dll # define dll_export _ declspec (dllexport) # else # define dll_export _ declspec (dllimport) # endifextern "C" dll_export void initpyt () {pyobject * m, * D; M = py _ Initmodule ("PYT", allmehod); D = pymodule_getdict (m);} // create a function dictionary, it is really uncomfortable to write a dynamic connection library for exposing functions in Windows due to PE structure problems. However, it is much better in Linux, and it does not need to be imported or exported. However, the boost: Python library in the quasi-standard library of C ++ further optimizes this process. # Include <string> # include <boost/python. HPP> using namespace boost: Python; # pragma comment (Lib, "boost_python.lib") // here boost_python.lib is linked in windows, in Linux, remove this sentence and add the path of the static library boost_python.a to the link. STD: String strtmp; char const * Fe () {strtmp = "returned data...: "; return strtmp. c_str ();} boost_python_module (GE) {def ("Fe", Fe);} ha, boost is really good, very powerful !!!

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.