2010-09-18 295 views
14

我編譯這個程序:GCC編譯的二進制文件給予 「不能執行二進制文件」

#include <stdio.h> 

int main() 
{ 
    printf("Hello World!"); 
    return 0; 
} 

使用這個命令:

gcc -c "hello.c" -o hello 

當我嘗試執行你好,我得到

bash: ./hello: Permission denied 

因爲權限是

-rw-r--r-- 1 nathan nathan 856 2010-09-17 23:49 hello 

由於某種原因??

但是修改的權限,並試圖再次執行後什麼...,我得到

bash: ./hello: cannot execute binary file 

我用gcc(Ubuntu的4.4.3-4ubuntu5)4.4.3

上午什麼我在這裏做錯了嗎?這是顯而易見的...現在對我來說,只是用我疲憊的眼睛來試圖弄清楚這個簡單的問題,爲時已晚。......

P.S.我確實(有時)在比Hello World更復雜的程序上工作,但gcc正在全線實現這一點...

+2

執行:*文件*打招呼,結果貼在這裏。你應該編譯:gcc hello.c -o你好 – karlphillip 2010-09-18 04:01:43

+0

呃,-c是它,謝謝。 – 2010-09-18 04:07:09

+0

**總是**詢問所有的警告和調試信息(例如'gcc -Wall -g hello.c -o hello') – 2016-01-06 15:53:32

回答

22

取出-c。這是爲了製作目標文件,而不是可執行文件。

+0

只需添加,你所做的'hello'文件就是一個目標文件(如果那不是明顯)。如果使用'-c'選項構建,它應該命名爲'hello.o'。 – Starkey 2010-09-18 04:03:40

6

-c標誌告訴它不要鏈接,所以你有一個目標文件,而不是二進制可執行文件。

事實上,如果您在沒有-o標誌的情況下運行此標誌,則會發現默認輸出文件爲hello.o

供參考(笑聲和),在-c標誌的人進入:

-c Compile or assemble the source files, but do not link. The linking stage simply is not done. 
    The ultimate output is in the form of an object file for each source file. 

    By default, the object file name for a source file is made by replacing the suffix .c, .i, .s, 
    etc., with .o. 

    Unrecognized input files, not requiring compilation or assembly, are ignored.