生成期间发生链接器错误 我写了一个C++程序,它在Eclipse中运行良好。 但是当试图用makefile编译时,我得到了一个错误。 当我搜索这个错误时,所有的解决方案都建议添加main函数。 我在matrixU.cpp中确实有一个主函数

生成期间发生链接器错误 我写了一个C++程序,它在Eclipse中运行良好。 但是当试图用makefile编译时,我得到了一个错误。 当我搜索这个错误时,所有的解决方案都建议添加main函数。 我在matrixU.cpp中确实有一个主函数,c++,compilation,linker,makefile,C++,Compilation,Linker,Makefile,以下是make文件: # 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++

以下是make文件:

# 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/*
以下是错误:

/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

这条规则看起来可疑:

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 ' '
您依赖于
bin/matrixU.o
,创建了一个同名文件。你可能需要

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++ -o bin/matrixU.o bin/Student.o bin/Course.o
但是您应该使用宏
$@
$^
,它们分别解析为目标,即规则中
左侧的内容,以及
右侧的依赖项。这将避免一些错误:

matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    g++  $^ -o $@
此行将
bin/matrixU.o
(您以前从源代码处编译的)定义为输出文件,从而导致损坏

您需要的是将三个*.o输入文件链接到不同的输出文件中,该文件应与规则的目标相同,如下所示:

 g++ -o bin/matrixU bin/matrixU.o bin/Student.o bin/Course.o
另外,我谦虚地建议将头依赖项留给编译器,并使用模式匹配构建规则,这样您就不必每次添加/删除文件或依赖项时都编辑Makefile

我曾经写过一篇文章,但它的简短版本是:

# 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 $@
#假设GNU制造
SRCFILES:=$(shell find$(PROJDIRS)-类型f-名称“\*.cpp”)
DEPFILES:=$(patsubst src/%.cpp,bin/%.d,$(SRCFILES))
OBJFILES:=$(patsubst src/%.cpp,bin/%.o,$(SRCFILES))
CXXFLAGS_LOCAL:=-g-Wall-Weffc++-Linclude
.骗子:都是干净的
全部:matrixU
清洁:
rm-rf箱/*
-包括$(数据文件)
matrixU:$(OBJFILES)
$(CXX)-o$@$^
bin/%.o:src/%.cpp
$(CXX)$(CXXFLAGS_本地)$(CXXFLAGS)-MMD-MP-c$<-o$@

@jrok你知道他们的意思。两次否决票,没有理由,没有反馈。太好了。@user1803263:Yikes。。。我忘了神奇的配料了!确保查看我的最新编辑。。。如果没有编译器规则的
-M
选项,整个“编译器完成的依赖项”部分将无法工作。在Makefile世界中,最好使用“-o$@”而不是“-o matrixU”。
 g++ -o bin/matrixU bin/matrixU.o bin/Student.o bin/Course.o
# 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 $@