gpt4 book ai didi

c++ - 如何使 Makefile 适应 make 测试目标?

转载 作者:行者123 更新时间:2023-11-30 03:31:08 30 4
gpt4 key购买 nike

我的文件夹结构是这样的:

src
--main.cpp
tests
--src
----main_test.cpp
Makefile

我想制作一个像make main_test这样的目标,以便能够以./main_test的形式运行测试程序。

最初我有一个Makefile,很久没有修改了。所以我开始修改它的内容,以创建新的 main_test 目标。

生成文件

CXX = g++
CXXFLAGS = -O2 -Wall -std=c++11 -Iinclude
LDFLAGS =

EXE = main
SRCDIR = src
BINDIR = bin
OBJDIR = tests/bin

OBJECTS = $(patsubst $(SRCDIR)/%.cpp,$(BINDIR)/%.o,$(wildcard
$(SRCDIR)/*.cpp))

all: $(EXE)

$(EXE): $(BINDIR) $(OBJECTS)
$(CXX) $(OBJECTS) -o $(EXE) $(LDFLAGS)

$(BINDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c -MMD -o $@ $<

include $(wildcard $(BINDIR)/*.d)

$(OBJDIR):
mkdir -p $(OBJDIR)

main_test: $(OBJDIR) tests/bin/main_test.o
g++ -O2 -Wall -std=c++11 tests/bin/main_test.o -o tests/main_test

tests/bin/main_test.o: $(OBJDIR) tests/src/main_test.cpp
g++ -O2 -Wall -std=c++11 tests/src/main_test.cpp -o tests/bin/main_test.o

clean_tests:
rm -rf $(OBJDIR)

$(BINDIR):
mkdir -p $(BINDIR)

clean:
rm -rf $(BINDIR) $(EXE)

.PHONY: clean all

不幸的是,出了点问题:

make main_test
mkdir -p tests/bin
g++ -O2 -Wall -std=c++11 tests/src/main_test.cpp -o tests/bin/main_test.o
g++ -O2 -Wall -std=c++11 tests/bin/main_test.o -o tests/main_test
tests/bin/main_test.o: In function `_start':
(.text+0x1f0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
tests/bin/main_test.o: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
tests/bin/main_test.o:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
tests/bin/main_test.o: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
tests/bin/main_test.o: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here
tests/bin/main_test.o: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
tests/bin/main_test.o:(.data+0x10): first defined here
/usr/bin/ld: error in tests/bin/main_test.o(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
Makefile:26: recipe for target 'main_test' failed
make: *** [main_test] Error 1

我该如何解决这个问题?

最佳答案

tests/bin/main_test.o 的编译规则中,您错过了 -c 命令行选项。


使用 order-only dependency对于输出目录,例如:

main_test: tests/bin/main_test.o | $(OBJDIR) 

关于c++ - 如何使 Makefile 适应 make 测试目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44440228/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com