簡體   English   中英

從c程序調用時,gcc會出錯

[英]gcc gives error when called from c program

我正在嘗試從其他c程序編譯一個簡單的c文件。 我通過一個簡短的shell腳本調用gcc(我知道我可以直接執行此操作,但這是一個測試)。 這是我的代碼:

我的主程序(小塊):

    char strCommand[100];
    sprintf(strCommand, "./compile.sh %s %u", fileName, nr);

    FILE *pipe = popen(strCommand, "r");
    if (pipe == NULL) {
        perror("Unable to open compile script");
        exit(-1);
    }

    char path[LINE_BUFSIZE];
    while (fgets(path, LINE_BUFSIZE, pipe) != NULL)
           printf("%s", path);

    pclose(pipe);

我的shell腳本:

#!/bin/bash 
gcc $1 -o foo-$2

和應該編譯的文件;-):

#include <stdio.h>
#include <stdlib.h>

int main(){
    printf("Hello world");
}

當我直接從終端調用bash腳本時,一切正常。 但是,當從c程序調用我的腳本時,會發生以下錯誤:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

此錯誤表示我的Hello World程序中沒有主要功能。 但顯然有一個。 我錯過了什么?

確保您的hello world程序具有.c擴展名而不是.cpp(或類似)擴展名

gcc可以編譯C ++程序,但它不鏈接在C ++標准庫中,它鏈接在標准C庫中。

如果您的程序是C ++,那么您的主要功能將被名稱損壞,這將解釋為什么C庫無法找到它。

如果要使用C ++標准庫編譯C ++,請使用g ++。

您可以嘗試使用C中的system函數來調用bash腳本。

system("bash /path/to/compile.sh %s %u");

我經常使用這種非常簡單的方法。 只需確保使用#include <stdlib.h>

您收到的錯誤是鏈接器錯誤。 通常它是由於您未嘗試運行的C / C ++代碼中的int main()定義引起的。 雖然hello world C代碼編譯並運行正常,但是你的主C代碼可能缺少int main()函數。 檢查以確保主程序具有int main()函數,如果仍然遇到鏈接器錯誤,請使用system調用函數,該函數將命令發送到Linux機器上的默認shell解釋器。 在Ubuntu中,這通常是bash。 如果shell腳本在使用系統調用時運行,那么您就知道只有語法錯誤導致鏈接器錯誤。

暫無
暫無

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

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