[英]How do I make a target in a makefile invoke another target in the makefile
[英]How to adapt Makefile for make tests target?
我的文件夹结构如下所示:
src
--main.cpp
tests
--src
----main_test.cpp
Makefile
我想要创建一个像make main_test
这样的目标,以便能够将测试程序作为./main_test
运行。
最初我有一个Makefile,我很长时间没有修改过。 因此,我开始修改其内容,以创建新的main_test
目标。
Makefile文件
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
命令行选项。
对输出目录使用仅订购的依赖关系 ,例如:
main_test: tests/bin/main_test.o | $(OBJDIR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.