1. 程式人生 > >cmake處理多原始檔目錄

cmake處理多原始檔目錄

cmake處理多原始檔目錄

假設我們的原始檔全部在src中,則我們需要在子檔案src中建立檔案CmakeLists.txt,內容如下:

AUX_SOURCE_DIRECTORY(. DIR_TEST_SRCS)
ADD_LIBRARY (Test ${DIR_TEST_SRCS})

在主目錄的CmakeLists.txt中新增兩行內容:

ADD_SUBDIRECTORY( src )
target_link_libraries(main Test)

加上前面cmake模板,模板總結如下:

cmake_minimum_required (VERSION 2.6)
project (TEST)

set (TEST_VERSION 0.1)

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)

aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRC)

include_directories(${PROJECT_SOURCE_DIR}/src)

ADD_SUBDIRECTORY( src )

link_directories(${PROJECT_SOURCE_DIR}/lib)

add_executable(main ${DIR_SRC})

target_link_libraries(main Test)
target_link_libraries(main -lm)
target_link_libraries(main -lpthread)