Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
- Recently wrote a number of back-end code, the main achievement verification code is sent to the designated mailbox.
- As part of the write send mail using c ++, the code more complicated, written in python before, the use of third-party libraries, e-mail on the matter a few lines of code.
- So I want to be able to call python with c ++ program ah, Baidu, really can, but find that you need to do some work, and this invocation numerous bug.
- So think of following this approach.
- First, the packaging code into an executable program python
- Then let c ++ to execute the executable program, the perfect solution, eliminating a lot of trouble.
- Specific examples are as follows: First, to write with a python helloworld.py, as follows:
import sys
arg = sys.argv[1:]
if len(arg) < 1:
print("argumen is less!")
sys.exit(0)
name = arg[0]#从命令行读取参数
print("Hello",name)
sys.exit(0)
-
Then package it into an executable program helloworld.
- If not able to find pyinstaller, can be installed using pip (
pip install pyinstaller
), the installation is still prompted to restart the terminal.
- If not able to find pyinstaller, can be installed using pip (
#include <iostream>
using namespace std;
int main()
{
system("./helloworld XiaoMing");
return 0;
}
- After the package is over, the
dist/
extra will be a nexthelloworld
, take it out and put test.cpp the same directory. - Then use c ++ (test.cpp) to call it.