Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple make targets in the same CMake project

In my project I have a makefile that looks like this:

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 $@

This lets me run make tests or make target and it will build the appropriate executable.

How do I set up a CMakeLists file to get the same convenient build system?

like image 396
Anonymous Entity Avatar asked Jul 11 '15 10:07

Anonymous Entity


2 Answers

Except for using clang++, I think if you put the following in a CMakeLists.txt file and then run your cmake configure step in a build directory (i.e., mkdir build; cd build; cmake ..), you should have what you are asking for.

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})
like image 110
Phil Avatar answered Sep 20 '22 03:09

Phil


Every add_custom_target() (and some other commands, like add_executable) actually adds target in the make sence.

add_custom_target(tests) # Note: without 'ALL'
add_executable(test_executable ...) # Note: without 'ALL'
add_dependencies(tests test_executable)

So, test_executable will be build on make tests, but not in case of simple make.

like image 36
Tsyvarev Avatar answered Sep 23 '22 03:09

Tsyvarev