Based on your code snippet:
os.system("./my_program " + my_arg)
You're just trying to send an argument input to your program. You don't need to use preprocessor definitions (e.g. #define SOMETHING
) for it. (In fact, you can't, because the binary has already been built.)
Instead, you modify your main
function from int main()
to int main(int argc, char *argv[])
.
You'd use it in a way similar to what's below:
#include <iostream>
int main(int argc, char *argv[]) {
for(int i = 0; i < argc; ++i)
std::cout << "Argument " << i << ": " << argv[i] << std::endl;
return 0;
}
Sample runs from the code above, which was placed in the test.cpp
file and used from the Python3 interactive interpreter:
? /tmp g++ test.cpp -o argtest
? /tmp ./argtest
Argument 0: ./argtest
? /tmp ./argtest a b c d e 123
Argument 0: ./argtest
Argument 1: a
Argument 2: b
Argument 3: c
Argument 4: d
Argument 5: e
Argument 6: 123
? /tmp python3
Python 3.4.3+ (default, Oct 14 2015, 16:03:50)
[GCC 5.2.1 20151010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from os import system
>>> system('./argtest a b c 123')
Argument 0: ./argtest
Argument 1: a
Argument 2: b
Argument 3: c
Argument 4: 123
0
>>>
The zero at the end is the return code from Python's system
call saying everything went fine.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…