'Python/C++: Running .cpp in Python

Total beginner here, so please be gentle. I have a example.cpp file which has one main function which accepts some input parameters and returns an integer value.

How would I run this .cpp file from within Python such that I can specify the input parameters from within Python and access the output of the .cpp so that it is stored in Python?

Thanks

I've modified the main function to accept command line input. What do you mean by "returns its result as its exit code", kindall?

Also, want to add that I'd want to return a vector created from the .cpp so that the variable is in Python



Solution 1:[1]

You can't run a .cpp file, as you need to compile a binary from your .cpp file before you can execute it. first, try to compile it using your terminal, after that you might port it to python, and automate it in a Python script (I guess, that's why you want to do it in Python).

Solution 2:[2]

You could take a look at https://docs.python.org/2.7/library/subprocess.html to see if it matches your needs. The module allows you to spawn new processes, so if you have the cpp source compiled you can just call the executable and process the output as you wish.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fodma1
Solution 2