Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld: duplicate symbol

Tags:

c++

makefile

I have rand.cpp and rand.hpp file and have rand_unif() function. I have included rand.hpp file in sim_engine.hpp file.

In main.cpp file, I have included sim_engine.hpp file. If I run makefile then I got this error

ld: duplicate symbol rand_unif()    in sim_engine.o and main.o for architecture x86_64
collect2: ld returned 1 exit status

sim_engine.hpp is the only place where includes rand.hpp. main.cpp does not include rand.hpp but sim_engine.hpp.

I don't understand why I am getting the duplicate symbol error.

#mod_simu.make project makefile

mod_simu : main.o rand.o graph.o street.o isection.o vehicle.o event.o FEL.o sim_engine.o clock.o
    g++ -o mod_simu main.o rand.o graph.o street.o isection.o vehicle.o event.o FEL.o sim_engine.o clock.o

main.o : main.cpp   
    g++ -c main.cpp

rand.o : rand.cpp
    g++ -c rand.cpp

graph.o : graph.cpp graph.hpp street.hpp isection.hpp
    g++ -c graph.cpp

street.o : street.cpp street.hpp
    g++ -c street.cpp

isection.o : isection.cpp isection.hpp street.hpp
    g++ -c isection.cpp

vehicle.o : vehicle.cpp vehicle.hpp
    g++ -c vehicle.cpp

event.o : event.cpp event.hpp
    g++ -c event.cpp

FEL.o : FEL.cpp FEL.hpp
    g++ -c FEL.cpp

sim_engine.o : sim_engine.cpp sim_engine.hpp
    g++ -c sim_engine.cpp

clock.o : clock.cpp clock.hpp
    g++ -c clock.cpp
clean:
    rm *.o mod_simu

#end

This is the makefile I have.

like image 714
codereviewanskquestions Avatar asked Feb 15 '12 19:02

codereviewanskquestions


People also ask

What is duplicate symbol?

Duplicate symbols occur when you have both added an implementation file (. cpp) to your project and #included it. This way, the implementation file (.

What does duplicate symbol for architecture x86_ 64?

What does this mean? Well, it means we're trying to link the same symbol name (in our case, a method) from two (or more) different source files. The fix was easy: rename one of the methods by updating the header file, the source file (. c or .


1 Answers

You have evidently defined rand_unif multiple times in your program. You probably only define it once in textual code, but through header inclusion, that code gets compiled into multiple .o files.

You probably defined rand_unif in rand.hpp. That header gets included is included by sim_engine.hpp, so any .cpp file that includes sim_engine.hpp will automatically get a copy of rand_unif. Apparently, both main.cpp and sim_engine.cpp include sim_engine.hpp, so both those .o files get a copy of the function. C++ forbids having multiple definitions of the same function, but it does not require enforcement of that requirement. (It's called the one-definition rule.) The linker has caught you.

There are two typical solutions to the problem:

  • Move the definition of the function into a .cpp file; rand.cpp seems like a good candidate. Make sure that rand.hpp contains only a declaration, not a definition.

  • Change the definition in the header file to be inline. C++ makes an exception to the one-definition rule for inline functions. Add inline before the declaration in rand.hpp.

like image 107
Rob Kennedy Avatar answered Sep 18 '22 06:09

Rob Kennedy