我正在嘗試與家庭高管。
第一個代碼是:
int main(int argc, char *argv[]){
// Program changes permission because exec can't execute the file if permission is not accurated
chmod("src2/child.c", S_IRWXU | S_IRWXU);
// Now program creates one child process
pid_t pid = fork();
// If fork failed exit with error
if (pid == -1)
{
perror("error in fork");
exit(1);
}
if (pid == 0)
{
execlp("src2/child.c", "child.c", "Ciao");
// if execlp return something is error
perror("error in exec");
exit(1);
}
// father exit
exit(0);
}
檔案 child.c 是:
int child(int argc, char *argv[])
{
// write the term passed
printf("%s\n", argv[1]);
exit(0);
}
makefile 編譯了這兩個檔案。但是在運行中,終端回傳 'src2/child.c: 6: Syntax error: "(" unexpected' 我不明白為什么
uj5u.com熱心網友回復:
由于src2/child.c不是二進制檔案并且沒有 shebang ( #!) 行,因此 shell 期望檔案包含要執行的 shell 命令。但事實并非如此。
您需要執行trygame(可執行檔案),而不是src2/child.c(用于創建該可執行檔案的源代碼的一部分)。
您之前已經表明,有問題的 C 檔案是包含make檔案的專案的一部分。該make檔案指示make執行以下操作:
gcc -Wall -std=c99 -I./inc -c src/main.c -o src/main.o
gcc -Wall -std=c99 -I./inc -c src/errExit.c -o src/errExit.o
gcc -Wall -std=c99 -I./inc -c src/semaphore.c -o src/semaphore.o
gcc -Wall -std=c99 -I./inc -c src/child.c -o src/child.o
gcc src/main.o src/errExit.o src/semaphore.o src/child.o -o trygame
這會編譯在 中找到的四個檔案src/并將它們鏈接到一個名為trygame.
您顯然已重命名src為src2并且需要make相應地調整檔案。完成后,運行make以生成trygame可執行檔案。
要運行程式,您需要執行 this trygame,而不是src2/child.c.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/487778.html
上一篇:這fscanf行為是否不一致?
下一篇:用亂數初始化一個const陣列
