Reputation: 331
I've got a linker error that I'm having trouble understanding. Here's the error message:
duplicate symbol _main in:
objs/MyFile.o
/var/folders/h7/f0t4_h4130bcvkjm7ms7y9_w0000gp/T/MyFile-f334a7.o
ld: 1 duplicate symbol for architecture x86_64
At first after looking around for an answer, I thought I might have accidentally declared something in a header file. I went ahead and removed all of my own header files, but I'm still getting the error.
Coupled with the fact that the two files in the error message are not both /var/folder
files like many of the declaration-in-header errors, this makes me think it might have something to do with my Makefile, which I have been recently playing around with.
Here's the Makefile:
CXX = clang++
LDFLAGS = -lm -lxml2
SRCDIR = src
OBJDIR = objs
SOURCES = $(wildcard src/*.cc)
HEADERS = $(wildcard src/*.h)
OBJECTS = $(patsubst src/%.cc,$(OBJDIR)/%.o,$(SOURCES))
objs/%.o : src/%.cc
@echo "Building object $@"
@$(CXX) $(CXXFLAGS) -o $@ -c $<
TARGETS = src/MyFile
default: $(TARGETS)
src/MyFile: $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $@ -I $^ $(LDFLAGS) src/MyFile.cc
Makefile.dependencies:: $(SOURCES) $(HEADERS)
$(CXX) $(CXXFLAGS) -MM $(SOURCES) > Makefile.dependencies
-include Makefile.dependencies
.PHONY: clean spartan
clean:
@rm -f $(TARGETS) $(OBJECTS) core Makefile.dependencies
spartan: clean
@rm -f *~ .*~
Any and all help in figuring out what could be causing this is much appreciated!
Upvotes: 0
Views: 163
Reputation: 5252
I don't understand this line and think that it causes the error:
$(CXX) $(CXXFLAGS) -o $@ -I $^ $(LDFLAGS) src/MyFile.cc
Firstly, -I $^
doesn't make much sense as -I
should be followed by include directory path, you probably want this to be -I . $^
.
Secondly, you don't need src/MyFile.cc
as it already should be present in $(OBJECTS)
as objs/MyFile.o
.
Changing linker command to this should help:
$(CXX) $(CXXFLAGS) -o $@ -I . $^ $(LDFLAGS)
Your final link command looks similar to:
compiler file.o file.cpp
Compiler driver (clang++
here) sees source file in argument list and compiles it placing output into temporary file (/var/folders/h7/f0t4_h4130bcvkjm7ms7y9_w0000gp/T/MyFile-f334a7.o
here) and actual link command becomes:
linker file.o differently/named/file.o
Hence duplicated symbol error: you have two object files of the same source file in argument list for the linker.
Upvotes: 2