Makefile 将不同目录中的源文件中的目标文件放入单个单独的目录中?

发布于 2024-09-30 21:21:13 字数 1250 浏览 1 评论 0原文

我使用 UnitTest++ 来为一些 C++ 代码(应在 Linux 或 Mac OS X 上构建)创建单元测试。我有一个像这样的目录结构:

src
- Foo.cpp
- Bar.cpp
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
UnitTest++
- libUnitTest++.a

这个 Makefile(改编自 UnitTest++ Makefile)工作得很好(与 GNU make 一起):

test = TestFooAndBar

src = ../src/Foo.cpp \
    ../src/Bar.cpp

test_src = Main.cpp \
    FooTest.cpp \
    BarTest.cpp

lib = ../UnitTest++/libUnitTest++.a

objects = $(patsubst %.cpp,%.o,$(src))
test_objects = $(patsubst %.cpp,%.o,$(test_src))


.PHONY: all
all: $(test)
    @echo Running unit tests...
    @./$(test)

$(test): $(lib) $(test_objects) $(objects)
    @echo Linking $(test)...
    @$(CXX) $(LDFLAGS) -o $(test) $(test_objects) $(objects) $(lib)

.PHONY: clean
clean:
    -@$(RM) -f $(objects) $(test_objects) $(test) 2> /dev/null

%.o : %.cpp
    @echo $<
    @$(CXX) $(CXXFLAGS) -c $< -o $(patsubst %.cpp,%.o,$<)

但我想将所有 .o 文件放在“test”目录的“obj”子目录中。我该如何修改这个 Makefile 来做到这一点?

我尝试将“obj/”添加到对象和 test_objects 变量中,但我不知道如何修改 %.o 规则,以便它知道 .o 文件在哪里并引用正确的 .cpp 文件。我是否需要创建两个单独的规则,每个规则对应一组 .cpp 文件?

如果我不定义 src 和 test_src 变量,而是让 Makefile 为所有 .cpp 文件(都在与 Makefile 相同的目录中和 ../src/ 中)构建 .o (到 obj/ 中),会更简单吗? ?

I'm using UnitTest++ to allow me to create unit tests for some C++ code (that should build on Linux or Mac OS X). I have a directory structure like this:

src
- Foo.cpp
- Bar.cpp
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
UnitTest++
- libUnitTest++.a

And this Makefile (adapted from the UnitTest++ Makefile) works nicely (with GNU make):

test = TestFooAndBar

src = ../src/Foo.cpp \
    ../src/Bar.cpp

test_src = Main.cpp \
    FooTest.cpp \
    BarTest.cpp

lib = ../UnitTest++/libUnitTest++.a

objects = $(patsubst %.cpp,%.o,$(src))
test_objects = $(patsubst %.cpp,%.o,$(test_src))


.PHONY: all
all: $(test)
    @echo Running unit tests...
    @./$(test)

$(test): $(lib) $(test_objects) $(objects)
    @echo Linking $(test)...
    @$(CXX) $(LDFLAGS) -o $(test) $(test_objects) $(objects) $(lib)

.PHONY: clean
clean:
    -@$(RM) -f $(objects) $(test_objects) $(test) 2> /dev/null

%.o : %.cpp
    @echo 
lt;
    @$(CXX) $(CXXFLAGS) -c 
lt; -o $(patsubst %.cpp,%.o,
lt;)

But I want to put all the .o files in an "obj" subdirectory of the "test" directory. How do I modify this Makefile to do so?

I've tried adding "obj/" to the objects and test_objects variables, but I can't figure out how to modify the %.o rule so it knows where the .o files are and refers to the correct .cpp files. Do I need to create two separate rules, one for each set of .cpp files?

Would it be simpler if instead of defining the src and test_src variables, I just have the Makefile build a .o (into obj/) for all .cpp files (both in the same directory as the Makefile and in ../src/)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-10-07 21:21:13

有不止一种方法可以做到这一点,但这是一个非常好的方法(我真的应该有热键)。

vpath %.cpp ../src

src = Foo.cpp Bar.cpp 
test_src = Main.cpp FooTest.cpp BarTest.cpp 

objects = $(patsubst %.cpp,obj/%.o,$(src)) 
test_objects = $(patsubst %.cpp,obj/%.o,$(test_src)) 

$(objects): | obj

obj:
  @mkdir -p $@

obj/%.o : %.cpp
  @echo 
lt; 
  @$(CXX) $(CXXFLAGS) -c 
lt; -o $@

There's more than one way to do it, but this is a pretty good one (I really should have that hotkeyed).

vpath %.cpp ../src

src = Foo.cpp Bar.cpp 
test_src = Main.cpp FooTest.cpp BarTest.cpp 

objects = $(patsubst %.cpp,obj/%.o,$(src)) 
test_objects = $(patsubst %.cpp,obj/%.o,$(test_src)) 

$(objects): | obj

obj:
  @mkdir -p $@

obj/%.o : %.cpp
  @echo 
lt; 
  @$(CXX) $(CXXFLAGS) -c 
lt; -o $@
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文