2014-02-06 47 views
0

我有makefile的錯誤,當我嘗試編譯可執行文件.o文件 錯誤列表:的Makefile和gktmm-3的.o錯誤

g++ -g -Wall -o main main.o `pkg-config --cflags --libs gtkmm-3.0` 
main.o: In function `_start': 
(.text+0x0): multiple definition of `_start' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.text+0x0): first defined here 
main.o: In function `_fini': 
(.fini+0x0): multiple definition of `_fini' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crti.o:(.fini+0x0): first defined here 
main.o:(.rodata+0x0): multiple definition of `_IO_stdin_used' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.rodata.cst4+0x0): first defined here 
main.o: In function `data_start': 
(.data+0x0): multiple definition of `__data_start' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crt1.o:(.data+0x0): first defined here 
main.o: In function `data_start': 
(.data+0x8): multiple definition of `__dso_handle' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/crtbegin.o:(.data+0x0): first defined here 
main.o: In function `_init': 
(.init+0x0): multiple definition of `_init' 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib/crti.o:(.init+0x0): first defined here 
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.8.2/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__' 
main.o:(.data+0x10): first defined here 
/usr/bin/ld: error in main.o(.eh_frame); no .eh_frame_hdr table will be created. 
collect2: error: ld returned 1 exit status 
makefile:12: recipe for target 'main' failed 
make: *** [main] Error 1 

和Makefile是

NAME=main 
CFLAGS=-g -Wall -o $(NAME) 
LIBFLAGS=`pkg-config --cflags --libs gtkmm-3.0` 
SRCS=main.cpp 
CC=g++ 
OBJECTS=$(SRCS:.cpp=.o) 
all: main 
main: $(SRCS) 
    $(CC) $(CFLAGS) $(OBJECTS) $(LIBFLAGS) 
.cpp.o: 
     $(CC) $(CFLAGS) $< -o [email protected] 

有人可以解釋我的錯誤嗎?我知道這不是最好的makefile。

回答

1

您必須確保您在創建目標文件的所有編譯行上使用-c標誌。沒有這個標誌,編譯器前端(g++)將創建一個可執行文件,而不是一個目標文件。

在您的例子後綴規則.cpp.o正在創建一個名爲main.o文件,但它實際上是鏈接到一個可執行文件。如果你跑./main.o,它會起作用。然後,當您嘗試將該可執行文件鏈接到另一個名爲main的可執行文件時,會發生不良情況。

更改您的規則:

.cpp.o: 
     $(CC) $(CFLAGS) -c -o [email protected] $<