2

Eclipseで正常に動作するc++プロジェクトを作成しました。しかし、メイクファイルでコンパイルしようとすると、エラーが発生します。このエラーを検索したとき、すべての解決策は main 関数を追加することを提案しました。私はmatrixU.cppにメイン関数を持っています。

メイクファイルは次のとおりです。

# 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
4

2 に答える 2

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

この行はbin/matrixU.o(以前にソースからコンパイルしたもの) を出力ファイルとして定義するため、破損します。

必要なのは、次のように、3 つの *.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 $@
于 2012-11-06T14:01:11.083 に答える
1

このルールは疑わしいようです:

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 ' '

すなわち

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

それ以外の

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

ただし、代わりにマクロ$@andを使用する必要があります$^。これらはターゲットに解決されます。つまり、ルール内の の左側にあるもの:と、 の右側にある依存関係:です。これにより、いくつかの間違いが防止されます。

matrixU: bin/matrixU.o bin/Student.o bin/Course.o
    g++  $^ -o $@
于 2012-11-06T13:58:56.400 に答える