C + + interacts with Python

Source: Internet
Author: User

Recently done the project wants to play, point flexible routines, processing logic to let the business custom to go, so from the elder brother game service came to think of the load script routines.

Everyone on Earth knows that Lua uses the most of the script in the game, but this time our project is monitoring and alerting, there may be complex operations, so the sub-LUA library is poorer, choose to use Python, so I need to make Python and C + + to do the interaction.

My requirement is this: 1 The main program is C + +, the user will go to the main program registration, this time the Python script is registered.

2 in some cases to run the Python script, Python will call a specific C + + interface, complete some of the python is not easy to complete functions, such as the operation of the database, call the text interface and so on.

Lazy people do not want to write the original ecological code, see that one by one C + + to Python registration interface do not love to do, find a Chinese writing three-party library Ffpython, ran his example, or good, very simple, can complete C + + static function, C + + class want to register Python , with this, by the way, his example in the Test_reg_function interface of the second parameter should be passed int, do not know that the script is written in a double row, resulting in an exception.


ffpython_t::init_py();


ffpython_t::add_path("/USERS/BOJUE/SENTRY2/SENTRY-ALARM/SRC") ;


Cinterface_api M_api;

python. Reg_class < Cinterface_api,pyctor() > ("Cinterface_api")

    . Reg (&cinterface_api::getgroupinfo,"GetGroupInfo") ;

python. Init ("db_operate");

python. Pager < void > ("fftest","Test_db_api", &m_api);

ffpython_t::init_py();


So a few lines of code is OK, you can cinterface_api the object registered to Python, the need for detailed research friends to download his code to see the example bar. Very simple.



C + + interacts with Python

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.