Linker error during Make

2019-07-18 23:50发布

问题:

I wrote a c++ project which works fine in eclipse. but when trying to compile with a makefile i get an error. when i searched for this error all solutions suggested adding main function. i do have a main function in matrixU.cpp.

here is the make file:

# All Targets
all: matrixU

# Tool invocations
# Executable "matrixU" depends on the files matrixU.o and Student.o and Course.o
matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    @echo 'Building target: matrixU'
    @echo 'Invoking: C++ Linker'
    g++ -o bin/matrixU.o bin/Student.o bin/Course.o
    @echo 'Finished building target: matrixU'
    @echo ' '

# Depends on the source and header files
bin/matrixU.o: src/matrixU.cpp include/matrixU.h
    g++ -g -Wall -Weffc++ -c -Linclude -o bin/matrixU.o src/matrixU.cpp

# Depends on the source and header files 
bin/Student.o: src/Student.cpp include/Student.h
    g++ -g -Wall -Weffc++ -c -Linclude -o bin/Student.o src/Student.cpp

# Depends on the source and header files 
bin/Course.o: src/Course.cpp include/Course.h
    g++ -g -Wall -Weffc++ -c -Linclude -o bin/Course.o src/Course.cpp

#Clean the build directory
clean: 
    rm -rf bin/*

here is the error:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 20 has invalid symbol index 19
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

回答1:

 g++ -o bin/matrixU.o bin/Student.o bin/Course.o

This line defines bin/matrixU.o (which you previously compiled from source) as the output file, resulting in corruption.

What you want is having the three *.o input files linked into a different output file, which should be the same as the target of the rule, like this:

 g++ -o bin/matrixU bin/matrixU.o bin/Student.o bin/Course.o

Additionally, may I humbly suggest leaving header dependencies up to the compiler, and using pattern-matching build rules, so you don't have to edit the Makefile everytime you add / remove a file or dependency.

I wrote a tutorial for that once, but the short version of it is:

# Assuming GNU make
SRCFILES := $(shell find $(PROJDIRS) -type f -name "\*.cpp")
DEPFILES := $(patsubst src/%.cpp,bin/%.d,$(SRCFILES))
OBJFILES := $(patsubst src/%.cpp,bin/%.o,$(SRCFILES))
CXXFLAGS_LOCAL := -g -Wall -Weffc++ -Linclude

.PHONY: all clean

all: matrixU

clean: 
    rm -rf bin/*

-include $(DEPFILES)

matrixU: $(OBJFILES)
    $(CXX) -o $@ $^

bin/%.o: src/%.cpp
    $(CXX) $(CXXFLAGS_LOCAL) $(CXXFLAGS) -MMD -MP -c $< -o $@


回答2:

This rule looks suspect:

matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    @echo 'Building target: matrixU'
    @echo 'Invoking: C++ Linker'
    g++ -o bin/matrixU.o bin/Student.o bin/Course.o
    @echo 'Finished building target: matrixU'
    @echo ' '

You depend on bin/matrixU.o, and you create a file with the same name. You probably need

matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    @echo 'Building target: matrixU'
    @echo 'Invoking: C++ Linker'
    g++  bin/matrixU.o bin/Student.o bin/Course.o -o matrixU
    @echo 'Finished building target: matrixU'
    @echo ' '

i.e

g++  bin/matrixU.o bin/Student.o bin/Course.o -o matrixU

instead of

g++ -o bin/matrixU.o bin/Student.o bin/Course.o

But you should use the macros $@ and $^ instead, which resolve to the target, i.e. what is on the left hand side of the : in the rule, and the dependencies, which are on the right of the :, respectively. This will prevent some mistakes:

matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    g++  $^ -o $@