Here is the error:
g++ -lpthread -pthread -std=c++11 -g -D_GNU_SOURCE src/QuoridorMain.o src/Tree.o src/utility.o src/Game.o src/storage.o -o qbot
/usr/bin/ld: error: src/QuoridorMain.o: file too short
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: qbot] Error 1
And here is the Makefile I'm using.
# Makefile for qbot
# Compiler options
CXX = g++ # use g++ compiler
CXXFLAGS = -lpthread -pthread -std=c++11 -g -D_GNU_SOURCE -no-pie# openmp and pthread, g for debugging
.SUFFIXES: .o .cpp
OFILES = src/QuoridorMain.o src/Tree.o src/utility.o src/Game.o src/storage.o
qbot: $(OFILES)
$(CXX) $(CXXFLAGS) $(OFILES) -o qbot
@echo Produced qbot executable
clean:
$(RM) *.o *~
# Dependency rules for *.o files
src/Tree.o: src/Tree.cpp src/Tree.h src/utility.cpp src/Global.h
src/QuoridorMain.o: src/QuoridorMain.cpp src/Tree.cpp src/Game.cpp src/Global.h
src/utility.o: src/utility.h src/utility.cpp src/Global.h
src/Game.o: src/Game.cpp src/Game.h src/Global.h
src/storage.o: src/storage.cpp src/storage.h
I'm not super good at determining what is necessary and what is cruft in a makefile, and linker errors are often my downfall. That being said, this was compiling just fine before, and the only thing that I think changed between compiling perfectly and this error is the addition of some std::cout
statements. So I'm pretty lost as to what's up.
Perhaps relevant stuff: I'm compiling on a new computer compared to what I developed most of the code on, and was getting a ld
related error earlier, which I fixed by adding the -no-pie
flag. However, that made sense to me, and even removing the -no-pie
flag now gets the same compile error about. Also: it is likely there are significant errors in the run-time of my code. Earlier I was getting segfaults or non-segfaults during runs based on whether certain std::cout
statements are commented out or not. I was told this was a sign of major problems elsewhere, but if you could be specific as to what those problems often are, and how they would now affect compiling rather than just run-time, I'd be very appreciative. Thanks!
EDIT: At the suggestions of commenters, I have taken out all .cpp files from the .o dependency rules. I am still receiving the same error after a make clean
. I suspect I will need to do more moving things about to make it actually work instead of just removing .cpp files, but that doesn't appear to be the direct issue at play.
The core problem is that your .o file got damaged some time in the past.
The secondary problem is that your clean
target does not actually clean up the OFILES, as it only removes *.o
, not src/*.o
.
## EDIT: removed -lpthread
CXXFLAGS = -pthread -std=c++11 -g -D_GNU_SOURCE -no-pie # openmp and pthread, g for debugging
OFILES = src/QuoridorMain.o src/Tree.o src/utility.o src/Game.o src/storage.o
## EDIT: Used $@ and $^ instead
qbot: $(OFILES)
$(CXX) $(CXXFLAGS) $@ -o $^
@echo Produced qbot executable
## EDIT: fixed clean target and designated it as .PHONY, which stops Make from looking
## EDIT: for a file named "clean"
.PHONY: clean
clean:
$(RM) -f qbot $(OFILES) *~
## EDIT: Removed .cpp files from dependency list.
## EDIT: Keeping the similarly-named cpp files, even though they are redundant.
# Dependency rules for *.o files
src/Tree.o: src/Tree.cpp src/Tree.h src/Global.h
src/QuoridorMain.o: src/QuoridorMain.cpp src/Global.h
src/utility.o: src/utility.cpp src/utility.h src/Global.h
src/Game.o: src/Game.cpp src/Game.h src/Global.h
src/storage.o: src/storage.cpp src/storage.h