Linux under Python calls C module

Source: Internet
Author: User

When C calls the Python module to initialize the Python interpreter, import modules, etc., but Python calls the C module is relatively simple, the following is a description of HELLOWORLD.C and main.py:

 (1) Write C code, HELLO.C code is simple, just output "Hello world! ":       (2) Compile the C code into the form of a dynamic link library, specific commands: in this case, the libhello.so dynamic link library is generated in the current directory:      (3) Import the dynamic link library in main.py and call the C functionhere ctypes is an external library of Python, providing the C language compatible data types to facilitate the call C compiled static libraries and functions in the dynamic library, it can be sure that the ctypes in the C data type is converted, using only import cTYPES, then import the dynamic library. (4) Enter Python main.py under terminal to run the result:      Print out the contents of the HELLOWORLD.C, stating that Python calls C module successfully!

Linux under Python calls C module

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.