文件编译时出现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。

问题解决。



转载于:https://www.cnblogs.com/java20130726/archive/2012/07/26/3218546.html

你可能感兴趣的:(文件编译时出现multiple definition of `xxxxxx'的解决办法)