gpt4 book ai didi

makefile - 同一个 CMake 项目中的多个 make 目标

转载 作者:行者123 更新时间:2023-12-04 00:49:39 26 4
gpt4 key购买 nike

在我的项目中,我有一个如下所示的 makefile:

CXX = clang++
CFLAGS = -std=c++11
COMMON_SOURCES = file1.cpp file2.cpp
TARGET_SOURCES = main.cpp
TEST_SOURCES = run_tests.cpp test_file1.cpp test_file2.cpp
COMMON_OBJECTS = $(COMMON_SOURCES:.c=.o)
TARGET_OBJECTS = $(TARGET_SOURCES:.c=.o)
TEST_OBJECTS = $(TEST_SOURCES:.c=.o)
EXECUTABLE = build/application
TEST_EXECUTABLE = build/tests
.PHONY: all target tests
all: target tests
target: $(EXECUTABLE)
tests: $(TEST_EXECUTABLE)
clean:
rm build/tests & rm build/application &
$(EXECUTABLE): $(COMMON_OBJECTS) $(TARGET_OBJECTS)
$(CXX) $(CFLAGS) $(LDFLAGS) $^ -o $@
$(TEST_EXECUTABLE): $(COMMON_OBJECTS) $(TEST_OBJECTS)
$(CXX) $(CFLAGS) $(LDFLAGS) $^ -o $@
.c.o:
$(CXX) $(CFLAGS) $< -o $@

这让我可以运行 make testsmake target,它将构建适当的可执行文件。

如何设置 CMakeLists 文件以获得同样方便的构建系统?

最佳答案

除了使用 clang++,我认为如果您将以下内容放在 CMakeLists.txt 文件中,然后在构建目录(即 mkdir build; cd build; cmake .. )中运行您的 cmake configure 步骤,您应该拥有您所要求的内容。

project(myproject)

# I am not sure how you get cmake to use clang++ over g++
# CXX = clang++

add_definitions(-std=c++11)
set(COMMON_SOURCES file1.cpp file2.cpp)
set(TARGET_SOURCES main.cpp)
set(TEST_SOURCES run_tests.cpp test_file1.cpp test_file2.cpp)
add_executable(application ${COMMON_SOURCES} ${TARGET_SOURCES})
add_executable(tests ${COMMON_SOURCES} ${TEST_SOURCES})

关于makefile - 同一个 CMake 项目中的多个 make 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356199/

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