簡體   English   中英

分叉一個孩子並打電話給gcc

[英]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.
  1. 有什么想法有什么不對嗎? 為什么不成功編譯文件? 請注意,我確實成功通過終端手動編譯它們。
  2. 我還沒有找到答案的另一個問題是,如何強制在c文件的目錄中創建comp.out文件? 因為如果我使用文件的完整路徑調用gcc,則會在主目錄中創建.out文件。

我曾嘗試谷歌和研究,但無法找到這兩個問題的答案。 感謝幫助。

您可以使用

execlp()

代替。 它在PATH環境變量中搜索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM