2016-03-21 103 views
0

雖然與此生成的文件中的gcc編譯我收到的錯誤GCC無效的命令行選項

cannot specify -o with -c, -S or -E with multiple files" 

現在我想它有事情做,試圖編譯相同文件兩次,但我不知道如何。

assembler : main.o assembler.o utils.o 
    gcc -g -ansi -Wall -pedantic -lm main.o assembler.o utils.o -o assembler 
main.o : main.c assembler.h utils.h 
    gcc -c -ansi -Wall -pedantic -lm main.c assembler.h utils.h -o main.o 
assembler.o : assembler.c 
    gcc -c -ansi -Wall -pedantic -lm assembler.c -o assembler.o 
utils.o : utils.c structs.h 
    gcc -c -ansi -Wall -pedantic -lm utils.c structs.h -o utils.o 

文件都包含這樣的:

structs.h is included in utils.c, 
utils.c is included in utils.h, 
utils.h is included in assembler.c, 
assembler.c is included in assembler.h, 
assembler.h is included in main.c. 

(:不知道爲什麼,它讓我做的代碼生成...

+0

指定多個源文件時纔有意義被調用。 – EOF

+0

你不會「編譯」頭文件,將它們從compil命令中取出,但它們確實是依賴文件 – Guiroux

+0

順便提一下:_utils.c包含在utils.h_中,_assembler.c包含在assembler.h_中並不真正使感。 .h文件包含在.c文件中,但不是其他方式。 –

回答

0

您probbaly需要此生成的文件(只需從海灣合作委員會的命令刪除.h文件):

assembler : main.o assembler.o utils.o 
    gcc -g -ansi -Wall -pedantic -lm main.o assembler.o utils.o -o assembler 

main.o : main.c assembler.h utils.h 
    gcc -c -ansi -Wall -pedantic main.c -o main.o 

assembler.o : assembler.c 
    gcc -c -ansi -Wall -pedantic assembler.c -o assembler.o 

utils.o : utils.c structs.h 
    gcc -c -ansi -Wall -pedantic utils.c -o utils.o 
+1

在編譯main.c的行中不應該包含「-lm」或「-o main.o」,對於assembler.c和utils.c行也應該類似。 – FredK

0

你的Makefile被錯誤地將所有的依賴於編譯命令而不是第一個,結果是你試圖編譯頭文件作爲翻譯單元,這是不恰當的。

這是正確的在鏈接命令中添加所有依賴關係,該命令將多個目標文件組合爲可執行文件。但是,如錯誤消息所示,當使用-c選項創建目標文件時,提供多個源文件將不起作用。

所以編譯配方通常會使用$<(第一依賴關係),而鏈接配方將使用$^(所有依賴關係)。請參閱make手冊的automatic variables chapter

0

也許有更好的方式:如果連接器將

assembler:: main.o assembler.o utils.o 
    gcc -o [email protected] main.o assembler.o utils.o -lm 
.c.o: 
    gcc -c -g -ansi -Wall -pedantic $*.c