2015-07-11 48 views
3

在我的項目我有一個makefile,看起來像這樣:make目標

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 [email protected] 
$(TEST_EXECUTABLE): $(COMMON_OBJECTS) $(TEST_OBJECTS) 
    $(CXX) $(CFLAGS) $(LDFLAGS) $^ -o [email protected] 
.c.o: 
    $(CXX) $(CFLAGS) $< -o [email protected] 

這讓我跑make testsmake target,它會建立相應的可執行文件。

如何設置一個CMakeLists文件以獲得相同的方便構建系統?

回答

3

除了使用鏗鏘聲++,我認爲如果你把下面的代碼放在CMakeLists.txt文件中,然後在構建目錄中運行你的cmake configure步驟(即mkdir build; cd build; cmake ..),你應該有你所要求的。

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}) 
+0

我得到的錯誤'add_executable調用的arguments' 不正確的數字也許是一個基本的步驟,我不知道嗎?我對cmake知之甚少。 –

+0

對不起,我剛剛用'SOURCES'替換了'OBJECTS',它工作正常。 –

+0

是的,對不起。當我把你的makefile邏輯轉錄到cmake時一個粗心的錯誤。我只是用這個更正來編輯我的答案。 – Phil

1

每個add_custom_target()(和一些其他命令,如add_executable)實際上增加了目標在make sence。

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

所以,test_executable將是建立在make tests,但不是簡單的make情況。