How to embed Python into C ++ applications?

Source: Internet
Author: User

This article describes how to embed Python into a C ++ application. First, let everyone know what Python is? In fact, Python is a simple, easy-to-learn and powerful interpreted programming language. It has a concise and clear syntax and an efficient high-level data structure.

The introduction of extended APIs has made great progress in PHP3. The extended API mechanism makes it easy for the PHP development community to develop dozens of extensions. Now, the two versions have passed, and the API is still very similar to PHP3. The main idea of extension is to hide the internal mechanism and Script Engine of PHP from the extension writer as much as possible. Developers only need to familiarize themselves with the API.

There are two reasons to write your own PHP extensions. The first reason is that PHP needs to support a technology that she does not yet support. This usually includes some ready-made C function libraries to provide PHP interfaces. For example, if a database named FooBase has been released to the market.

You need to create a PHP extension to help you call the C function library of FooBase from PHP. This job may be done by only one person and shared by the entire PHP community if you want ). The second reason is not very general: You need to write some business logic for performance or functional reasons.

If the above two reasons have nothing to do with you and you feel that you are not adventurous, you can skip this chapter.

This chapter teaches you how to compile relatively simple PHP extensions and use some extension API functions. For most PHP extension developers who intend to develop custom PHP extensions, it includes enough information. One of the best ways to learn a programming course is to make some extremely simple examples. These examples are the clues of this chapter.

Once you understand the basics, You can enrich yourself on the Internet by reading the text block, the original code, or participating in the discussion of the list of message groups. Therefore, this chapter focuses on how to get you started. In UNIX, a script called ext_skel is used to create an extension skeleton.

The skeleton information is obtained from the definition file that describes the Extended Interface. Therefore, you need to use UNIX to build a skeleton. Windows developers can use Windows ext_skel_win32.php instead of ext_skel. However, this chapter's guidance on compiling PHP with your extended extensions only covers UNIX compiling systems. All the interpretations of APIs in this chapter are related to extensions developed in UNIX and Windows.

After reading this chapter, you can learn how to build a simple business logic extension. We recommend package extension for a C function library, especially for some standard C file operation functions such as fopen ()

Python is a simple, easy-to-learn and powerful interpreted programming language. It has simple and clear syntax and an efficient high-level data structure, allowing you to easily and effectively implement object-oriented programming, it is especially suitable for fast application development and can also be used to develop large-scale important commercial applications. Python is an ideal scripting language. Python is free and open-source, and can be transplanted to multiple operating systems. As long as you do not need to use features dependent on specific operating systems, Python programs can run on various platforms without modification.

Python has all the powerful functions of modern programming languages. The Python standard library is very large and can help developers deal with various tasks, such: graphical user interface, file processing, multimedia, regular expressions, document generation, unit testing, thread, database, network communication, web browser, CGI, FTP, email, XML, HTML, WAV file, password system, Tk, and other system-related operations. As long as Python is installed, these functions are available in addition to the standard library, there are many other high-quality libraries, such as wxPython, Twisted, and Python graphics libraries.

Python is easy to expand and embed. Many standard modules provided by Python support C or C ++ interfaces. Python and C can work together and can be embedded into C or C ++ applications. Therefore, the Python language can provide scripting interfaces for applications, because it supports cross-language development.

You can use Python to design a conceptual application and gradually port it to C. You do not have to use C to rewrite the application. (Jython enables Python to work with Java, so that developers can call Java packages in Python or use Python objects in Java. Even better, because the Jython interpreter is fully written in Java, you can deploy Python programs on any platform that supports Java, and even run Python scripts on WEB browsers .)

In a C ++ application, we use a set of plug-ins to implement functions with unified interfaces. We use Python to replace the plug-ins in the form of dynamic link libraries, in this way, you can easily rewrite the script code as needed, rather than re-compile the dynamic link library of the link binary. The powerful functions of Python are sufficient, but some specific functions of the operating system need to be implemented using C ++, which is then called by Python. Therefore, we need:

1. Embed Python into the C ++ application, call the Python function in the C ++ program, and obtain the value of the variable;
2. Compile the extension module (Dynamic Link Library) for Python using C ++, and call the extension function developed by C ++ In the Python program.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of 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.