文件編譯時湧現multiple definition of 'xxxxxx'的詳細處理辦法。本站提示廣大學習愛好者:(文件編譯時湧現multiple definition of 'xxxxxx'的詳細處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是文件編譯時湧現multiple definition of 'xxxxxx'的詳細處理辦法正文
成績:
本來有單個文件tcpclient.c,運轉gcc -o tcpclient tcpclient.c可以順遂完成編譯,並能與下載到目的板中的tcpserver勝利通信;
如今把tcpclient.c中的底層通信部門抽掏出來,零丁放到一個文件中nettrans.c中,並有netrans.h作為頭文件;
然後,應用敕令
gcc -o tcpclient nettrans.c tcpclient.c
編譯,也總能勝利編譯。
然則,應用Makefile來編譯,老是湧現以下multiple definition of類的毛病:
dingq@u1110-120628:~/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket$ make
gcc nettrans.o tcpclient.o -o tcpclient
tcpclient.o: In function `nettrans':
/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket/nettrans.c:30: multiple definition of `nettrans'
nettrans.o:/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket/nettrans.c:30: first defined here
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [client] Error 1
Makefile的內容:
CC = gcc
CROSS_COMPILE = arm-linux-
CROSS_CC = $(CROSS_COMPILE)gcc
CCFLAGS = -g3 -Wall -o0
LDFLAGS =
RM = -rm -rf
SRC_CLIENT = nettrans.c tcpclient.c
SRC_SERVER = tcpserver.c
OBJ_CLIENT = $(SRC_CLIENT:%.c=%.o)
OBJ_SERVER = $(SRC_SERVER:%.c=%.o)
.PHONY : all
all : client
$(OBJ_CLIENT) : $(SRC_CLIENT)
$(CC) $(CCFLAGS) -c $< -o $@
$(OBJ_SERVER) : $(SRC_SERVER)
$(CROSS_CC) $(CCFLAGS) -c $< -o $@
client : $(OBJ_CLIENT)
$(CC) $(LDFLAGS) $(OBJ_CLIENT) -o tcpclient
server : $(OBJ_SERVER)
$(CROSS_CC) $(LDFLAGS) $(OBJ_SERVER) -o tcpserver
.PHONY : clean
clean :
$(RM) *.o tcpclient tcpserver
處理方法:
1. 應用通俗敕令編譯可以勝利,然則應用Makefile就不勝利,解釋成績出在Makefile上;
再細心檢討Makefile詳細的編譯履行進程:
dingq@u1110-120628:~/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket$ make clean;make
rm -rf *.o tcpclient tcpserver
gcc -g3 -Wall -o0 -c nettrans.c -o nettrans.o
gcc -g3 -Wall -o0 -c nettrans.c -o tcpclient.o
gcc nettrans.o tcpclient.o -o tcpclient
tcpclient.o: In function `nettrans':
/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket/nettrans.c:30: multiple definition of `nettrans'
nettrans.o:/home/dingq/hwsvn/2sw/1prj_linux/pdu/src/branches/pdu-isocket/isocket/nettrans.c:30: first defined here
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [client] Error 1
本來生成nettrans.o和tcpclient.o都應用了雷同的源文件nettrans.c;
所以,是Makefile中
$(OBJ_CLIENT) : $(SRC_CLIENT)
$(CC) $(CCFLAGS) -c $< -o $@
這個處所出了成績,應當應用形式規矩來停止目的文件和依附文件的婚配;
更改Makefile以下:
CC = gcc
CROSS_COMPILE = arm-linux-
CROSS_CC = $(CROSS_COMPILE)gcc
CCFLAGS = -g3 -Wall -o0
LDFLAGS =
RM = -rm -rf
SRC_CLIENT = nettrans.c tcpclient.c
SRC_SERVER = tcpserver.c
OBJ_CLIENT = $(SRC_CLIENT:%.c=%.o)
.PHONY : all
all : client
%.o : %.c
$(CC) $(CCFLAGS) -c $< -o $@
client : $(OBJ_CLIENT)
$(CC) $(LDFLAGS) $(OBJ_CLIENT) -o tcpclient
server : $(OBJ_SERVER)
$(CROSS_CC) $(CCFLAGS) -c $(SRC_SERVER) -o $(OBJ_SERVER)
$(CROSS_CC) $(LDFLAGS) $(OBJ_SERVER) -o tcpserver
.PHONY : clean
clean :
$(RM) *.o tcpclient tcpserver
從新編譯,勝利生成tcpclient。
成績處理。