[英]Forking a child and calling gcc
我正在嘗試創建一個獲取目錄路徑的程序,打開目錄然后編譯內部的c文件。
//open current directory
currDir=opendir(fullpath);
//get the c file, ignore hidden files
while((cfile=readdir(currDir))!=NULL)
{
if(cfile->d_name[0]!='.')
break;
}
/*compile c file*/
//child process
if((pid=fork())==0)
{
fullpath=realloc(fullpath, sizeof(char)*(strlen(fullpath)+strlen(cfile->d_name)+1));
strcat(fullpath,cfile->d_name);
execl("/usr/bin/gcc", "/usr/bin/gcc", "-o", "comp.out", fullpath,NULL);
}
else
{
wait(NULL);
}
如你所見,在子進程中我正在創建c文件的完整路徑(否則它將找不到它),然后調用gcc,但是我收到以下錯誤:
collect2: fatal error: cannot find 'ld'
compilation terminated.
我曾嘗試谷歌和研究,但無法找到這兩個問題的答案。 感謝幫助。
您可以使用
execlp()
代替。 它在PATH環境變量中搜索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.