A specific solution to the multiple definition of ' xxxxxx ' appears when the file is compiled _c language

Source: Internet
Author: User
Problem:
Originally has a single file tcpclient.c, run Gcc-o tcpclient tcpclient.c can successfully complete the compilation, and download to the target board tcpserver successful communication;
Now the tcpclient.c of the bottom part of the communication is extracted, placed in a single file nettrans.c, and Netrans.h as a header file;
Then, use the command
Copy Code code as follows:

Gcc-o tcpclient nettrans.c tcpclient.c

compilation, and can always be compiled successfully.
However, using makefile to compile, there are always the following multiple definition of class errors:
Copy Code code as follows:

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

content of Makefile:
Copy Code code as follows:

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

Solution:
1. The use of ordinary command compilation can be successful, but the use of makefile is not successful, indicating that the problem on the makefile;
Double-check the makefile specific compilation execution process:
Copy Code code as follows:

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

The original generation NETTRANS.O and TCPCLIENT.O both use the same source file nettrans.c;
So, it's makefile.
Copy Code code as follows:

$ (obj_client): $ (src_client)
$ (CC) $ (ccflags)-C $<-O $@

There is a problem with this place, the pattern rule should be used to match the target file and the dependent file;
change makefile as follows:
Copy Code code as follows:

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

Recompile to successfully generate TcpClient.
solve the problem.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.