問題:
原來有單個文件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。
問題解決。