조쉬 (Josh)의 답변은 규모를 확장하려는 경우 좋지만 간단한 예제가 어떻게 보이는지 알고 싶습니다. 대부분의 작업은 subprocess
모듈을 통해 수행됩니다. 여기에 리눅스 컴퓨터를 사용하고 있습니다. 제 컴파일러는 gcc
이지만 특정 시스템 아키텍처에 어떻게 의존하는지 쉽게 알 수 있습니다.
import subprocess
code = r'''
#include <stdio.h>
int main() {
for(int i=0;i<5;i++) {
printf("Testing %d\n",i);
}
return 0;
}
'''
f_cpp = "test.cpp"
with open(f_cpp,'w') as FOUT:
FOUT.write(code)
# Compile the program
f_exec = "./myprogram"
compile_cmd = "gcc {} -o {}".format(f_cpp, f_exec)
subprocess.call(compile_cmd, shell=True)
# Run the program
p = subprocess.Popen(f_exec, shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
# Step through the output
for line in p.stdout:
print line
이 출력으로 제공합니다
Testing 0
Testing 1
Testing 2
Testing 3
Testing 4
확인합니다. 파이썬은 다른 종류의 파일을 생성 할 수있는 것처럼 쉽게 C++ 파일을 생성 할 수 있습니다 .. open ("test.cpp", "w") with file : file.write ("int main() {printf ('Hello, 세계! '); return 0;} ")' – Kevin