2015-06-23 63 views
-2

這是一個非常奇怪的問題,但我仍然好奇。我有一個源代碼a.c,其中,我有一些魔術代碼,編譯不同代碼b.c在同一目錄中。因此,在運行時,b.c被編譯並且可能運行。在運行時編譯c源代碼中的另一個源代碼

我很抱歉這個問題含糊不清,但這是以前做過的事嗎?如果沒有,有沒有辦法做到這一點?

+0

爲什麼downvote這個問題?我是唯一一個好奇如何做到這一點的人? –

回答

2

變交流

#include <stdio.h> 

int main() { 
    puts("a: compiling b.c"); 
    system("gcc -o b b.c"); 
    puts("a: executing b"); 
    system("./b"); 
    puts("a: done"); 
    return 0; 
} 

b.c

#include <stdio.h> 

int main() { 
    puts("b: hello world"); 
    return 0; 
} 

輸出

a: compiling b.c 
a: executing b 
b: hello world 
a: done 
1

是的,你可以forkexec運行編譯器,然後再運行編譯代碼forkexec。另一個選項是使用system函數來運行編譯器,然後再次調用system來運行編譯後的代碼。