[Calling cpp code during python code execution]

cpp code

#include <iostream>
using namespace std;

int add(int argc, char** argv)
{   
    
    if (argc < 4) {
        cout << "please input parameters:"
            << "for example:\nadd 2 3\nyou will get a return 5"
            << endl;
                   
        return 0;
    }
    std::string func = argv[1];
    double num1 = atof(((std::string)argv[2]).c_str());
    double num2 = atof(((std::string)argv[3]).c_str());

    if (func == "add") {
        cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
    }
    if (func == "sub") {
        cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
    }
    return 0;
}

[Inter-language call] python calls C++ exe and enters parameters

python code

import os
import subprocess
print("苹果")
result = subprocess.run(['./Project1.exe','add','2','3'])

python running results

Insert image description here
Unfortunately, my requirement: the parameters passed to the cpp code are very large and cannot be passed through parameters o(TヘTo). What
I want is to pass them through reading and writing files.

So, first write a blog and record it in case of emergency

Guess you like

Origin blog.csdn.net/Peanut31434331/article/details/133552064