从 makefile 创建两个单独的可执行文件 (g++)

发布于 2024-10-20 06:37:05 字数 478 浏览 1 评论 0原文

目前,我已经设置了 makefile 来编译并制作一个相当大的项目。我编写了第二个 cpp 文件,其主要功能用于运行测试。我希望它们单独运行,但一起构建并且它们使用相同的文件。这是如何实现的?

编辑:作为参考,这是我当前的 makefile。我不知道如何调整它。

CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp Foo.cpp Bar.cpp Test.cpp A.cpp B.cpp C.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=myprogram

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $< -o $@

Currently, I have my makefile set up to compile and make a fairly large project. I have written a second cpp file with main function for running tests. I want these to run separately, but build together and they use the same files. How is this accomplished?

edit: As reference, here is my current makefile. I'm not sure how to adjust it.

CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp Foo.cpp Bar.cpp Test.cpp A.cpp B.cpp C.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=myprogram

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) 
lt; -o $@

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

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

发布评论

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

评论(2

ぃ双果 2024-10-27 06:37:05

通常你只会有多个目标并执行如下操作:

.PHONY: all target tests

all: target tests

target: ...
    ...

tests: ...
    ...

然后你可以只 make (默认为 make all),或者只是 make target或根据需要进行测试

因此,对于上面的 makefile 示例,您可能需要这样的内容:

CC = g++
CFLAGS = -c -Wall -DDEBUG -g
LDFLAGS =
COMMON_SOURCES = Foo.cpp Bar.cpp A.cpp B.cpp C.cpp
TARGET_SOURCES = main.cpp
TEST_SOURCES = test_main.cpp
COMMON_OBJECTS = $(COMMON_SOURCES:.cpp=.o)
TARGET_OBJECTS = $(TARGET_SOURCES:.cpp=.o)
TEST_OBJECTS = $(TEST_SOURCES:.cpp=.o)
EXECUTABLE = myprogram
TEST_EXECUTABLE = mytestprogram

.PHONY: all target tests

all: target tests

target: $(EXECUTABLE)

tests: $(TEST_EXECUTABLE)

$(EXECUTABLE): $(COMMON_OBJECTS) $(TARGET_OBJECTS)
    $(CC) $(LDFLAGS) $^ -o $@

$(TEST_EXECUTABLE): $(COMMON_OBJECTS) $(TEST_OBJECTS)
    $(CC) $(LDFLAGS) $^ -o $@

.cpp.o:
    $(CC) $(CFLAGS) 
lt; -o $@

Normally you would just have multiple targets and do something like this:

.PHONY: all target tests

all: target tests

target: ...
    ...

tests: ...
    ...

Then you can just make (defaults to make all), or just make target or make tests as needed.

So for your makefile example above you might want to have something like this:

CC = g++
CFLAGS = -c -Wall -DDEBUG -g
LDFLAGS =
COMMON_SOURCES = Foo.cpp Bar.cpp A.cpp B.cpp C.cpp
TARGET_SOURCES = main.cpp
TEST_SOURCES = test_main.cpp
COMMON_OBJECTS = $(COMMON_SOURCES:.cpp=.o)
TARGET_OBJECTS = $(TARGET_SOURCES:.cpp=.o)
TEST_OBJECTS = $(TEST_SOURCES:.cpp=.o)
EXECUTABLE = myprogram
TEST_EXECUTABLE = mytestprogram

.PHONY: all target tests

all: target tests

target: $(EXECUTABLE)

tests: $(TEST_EXECUTABLE)

$(EXECUTABLE): $(COMMON_OBJECTS) $(TARGET_OBJECTS)
    $(CC) $(LDFLAGS) $^ -o $@

$(TEST_EXECUTABLE): $(COMMON_OBJECTS) $(TEST_OBJECTS)
    $(CC) $(LDFLAGS) $^ -o $@

.cpp.o:
    $(CC) $(CFLAGS) 
lt; -o $@
め可乐爱微笑 2024-10-27 06:37:05

这是一种方法:

CXXFLAGS += -std=c++11 -Wall -O3

all: myprog mytest

myprog.cpp: main.cpp
    cp -vf 
lt; $@
myprog: myprog.o Foo.o Bar.o Test.o A.o B.o C.o

mytest.cpp: main.cpp
    cp -vf 
lt; $@
mytest.o: CPPFLAGS += -DDEBUG
mytest.o: CXXFLAGS += -O0 -g
mytest: mytest.o Foo.o Bar.o Test.o A.o B.o C.o

这是有效的,因为 内置规则< /a> 用于从 C++ 源代码 (%.o: %.cpp) 编译对象并链接主程序 (%: %.o)。

另请注意变量 目标特定 值的使用 <代码>CPPFLAGS 和CXXFLAGS

Here's one way to do it:

CXXFLAGS += -std=c++11 -Wall -O3

all: myprog mytest

myprog.cpp: main.cpp
    cp -vf 
lt; $@
myprog: myprog.o Foo.o Bar.o Test.o A.o B.o C.o

mytest.cpp: main.cpp
    cp -vf 
lt; $@
mytest.o: CPPFLAGS += -DDEBUG
mytest.o: CXXFLAGS += -O0 -g
mytest: mytest.o Foo.o Bar.o Test.o A.o B.o C.o

This works because built-in rules exist for compiling objects from c++ source (%.o: %.cpp) and linking main programs (%: %.o).

Also note the use of target-specific values for the variables CPPFLAGS and CXXFLAGS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文