我正在嘗試將文件寫入磁盤,然後自動重新編譯該文件。不幸的是,這似乎不起作用,我得到一個錯誤信息,我還不明白(我是一個C初學者:-)。如果我手動編譯生成的hello.c,它一切正常?!C編程 - 編寫自行編譯的文本文件
#include <stdio.h>
#include <string.h>
int main()
{
FILE *myFile;
myFile = fopen("hello.c", "w");
char * text =
"#include <stdio.h>\n"
"int main()\n{\n"
"printf(\"Hello World!\\n\");\n"
"return 0;\n}";
system("cc hello.c -o hello");
fwrite(text, 1, strlen(text), myFile);
fclose(myFile);
return 0;
}
這是我的錯誤:
/usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../../lib/crt1的.o:在功能_start': (.text+0x20): undefined reference to
主 collect2:LD返回1個退出狀態
在編寫文件之前編譯文件?所以這個文件是空的,然後你編譯它,並且gcc給出正確的錯誤,文件中沒有主函數。 – 2014-08-10 16:31:19