简体   繁体   中英

How to execute python from C++

I have a C++ program that needs to make certain calls to a library which is written in Python. (As opposed to rewriting the entire library in C++...)

What's the simplest way to do this? I need to pass 2 numbers into the library, and get 2 numbers back.

I need it to be portable and run in both Windows and Linux, so for example using POSIX popen is not possible.

So far the best solution I have is to write a python wrapper, called by a "system" call in the C++ code, that takes command line parameters, calls the function, and writes the result to a file, which is then opened by the C++ program.

If you know anything better please let me know...

如果可以选择boost,则可以使用boost.python嵌入

You can directly embed the python interpreter into your application. See the official documentation for a full explanation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM