Unix

CMakeLists.txt Example

ForceCore 2010. 1. 6. 17:13
이거면 간단한 프로젝트 정도는 cmake화 할 수 있을 것이다.

코드는 test/src/CMakeLists.txt
# equiv to -I. of gcc param.
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# we have ${CMAKE_CURRENT_BINARY_DIR}, too. Never used it before, though

# need GLPK
find_package(GLPK REQUIRED)
include_directories(${GLPK_INCLUDE_DIRS})

# just specify source files here.
# header file requirements are scanned automatically by cmake.
set(ctgen_SRCS main.cpp)
set(ctgen_SRCS ${ctgen_SRCS} solver.cpp glpk_wrapper.cpp)
add_executable(test ${ctgen_SRCS})

# ctgen binary file will be placed at parent dir.
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})

# link against glpk
target_link_libraries(test ${GLPK_LIBRARIES})

test/src에 소스코드를 두었다면, test/ 에 이 CMakeLists.txt 를 두면 된다.
cmake_minimum_required(VERSION 2.6)

set(CMAKE_CXX_FLAGS "-g")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
message( ${CMAKE_MODULE_PATH} )

project(test)
add_subdirectory(src)
흐음...;;

이걸 실제로 쓰려면
test/bld
디렉토리를 만들고 그 안에서...
[test/bld]$ cmake ..
[test/bld]$ make

이렇게 한다. 필자는 test/Makefile을 작성해두길 더 좋아하는데: (왜냐면 그냥 make만 찍어주면 되니까)
all: bld
    cd bld && cmake .. && make

bld:
    mkdir bld

clean:
    rm -rf bld
이런 내용 정도로 해주면 된다.