CMake - 윈디하나의 솔라나라

목차

개요

설치

간단한 사용방법

CMake 의 사용 방법은 CMake Tutorial이나 CMake Reference Documentation을 참고하자. cmake-buildsystem(7) 이나 cmake-language(7)을 참조할 수도 있다. 아래는 간단한 예제다.
  1. 소스 트리

    디렉토리 및 소스파일은 윈디하나의 솔라나라: Autotools - 디렉토리 및 소스파일와 동일하다. 소스에 대한 자세한 설명은 링크를 참고하자.
    windy@wl ~/cmake/src $ wget https://www.solanara.net/contents/includes/autotools/main.c
    windy@wl ~/cmake/src $ wget https://www.solanara.net/contents/includes/autotools/helloworld.h
    windy@wl ~/cmake/src $ wget https://www.solanara.net/contents/includes/autotools/helloworld.c
    
  2. CMakeLists.txt 작성

    소스 트리의 각 디렉토리마다 CMakeLists.txt파일을 작성해야 한다. 우선 최상위 디렉토리에 아래와 같이 작성한다.
    windy@wl ~/cmake $ vi CMakeLists.txt
    cmake_minimum_required(VERSION 3.0)
    PROJECT(helloworld C)
    INCLUDE(CPack)
    #SET(CMAKE_VERBOSE_MAKEFILE ON)
    ADD_SUBDIRECTORY(src)
    OPTION(ENABLE64BIT "Build 64bit Library" OFF)
    SET(EXTRAMESSAGE "" CACHE STRING "")
    configure_file(config.h.in config.h)
    
    src 디렉토리에도 아래와 같이 작성하자.
    windy@wl ~/cmake # vi ./src/CMakeLists.txt
    if (ENABLE64BIT)
            SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m64")
    endif (ENABLE64BIT)
    
    add_definitions(-DHAVE_CONFIG_H)
    
    ADD_EXECUTABLE(hello main.c)
    TARGET_LINK_LIBRARIES(hello helloworld)
    ADD_LIBRARY(helloworld SHARED helloworld.c)
    SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
    INSTALL(TARGETS hello DESTINATION bin)
    INSTALL(TARGETS helloworld DESTINATION lib)
    INSTALL(FILES helloworld.h DESTINATION include)
    
    INCLUDE_DIRECTORIES(AFTER ${CMAKE_BINARY_DIR} ${hello_SOURCE_DIR})
    
    config.h.in파일도을 생성하자.
    windy@wl ~/cmake $ vi config.h.in
    #ifndef CONFIG_H_
    #define CONFIG_H_
    
    #cmakedefine ENABLE64BIT
    #cmakedefine EXTRAMESSAGE "@EXTRAMESSAGE@"
    
    #endif
    
  3. 테스트

    빌드하고 설치해보자. 출력내용은 색상으로 구분되기 때문에 배경을 검은색으로 변경했다.
    windy@wl ~/cmake $ mkdir build
    windy@wl ~/cmake $ cd build
    windy@wl ~/cmake/build $ cmake ..
    -- The C compiler identification is SunPro 5.15.0
    -- Check for working C compiler: /opt/developerstudio12.6/bin/cc
    -- Check for working C compiler: /opt/developerstudio12.6/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /export/home/windy/cmake/build
    windy@wl ~/cmake/build $ make
    Scanning dependencies of target helloworld
    [ 25%] Building C object src/CMakeFiles/helloworld.dir/helloworld.c.o
    [ 50%] Linking C shared library libhelloworld.so
    [ 50%] Built target helloworld
    Scanning dependencies of target hello
    [ 75%] Building C object src/CMakeFiles/hello.dir/main.c.o
    [100%] Linking C executable hello
    [100%] Built target hello
    windy@wl ~/cmake/build $ sudo make install
    [ 50%] Built target helloworld
    [100%] Built target hello
    Install the project...
    -- Install configuration: ""
    -- Installing: /usr/local/bin/hello
    -- Set runtime path of "/usr/local/bin/hello" to ""
    -- Installing: /usr/local/lib/libhelloworld.so
    -- Installing: /usr/local/include/helloworld.h
    
    make help명령을 사용하면 사용 가능한 옵션을 확인할 수 있다.
    windy@wl ~/cmake/build $ make help
    The following are some of the valid targets for this Makefile:
    ... all (the default if no target is provided)
    ... clean
    ... depend
    ... install/strip
    ... install/local
    ... package_source
    ... package
    ... rebuild_cache
    ... list_install_components
    ... edit_cache
    ... install
    ... helloworld
    ... hello
    
  4. 패키징

    CMakeLists.txtINCLUDE(CPack)를 넣어주면 make 시에 package, package_source 옵션을 사용할 수 있다. package 의 경우 install 에 필요한 파일을 자동으로 패키지로 묶어준다.
    windy@wl ~/cmake/build $ make package
    [ 50%] Built target helloworld
    [100%] Built target hello
    Run CPack packaging tool...
    CPack: Create package using STGZ
    CPack: Install projects
    CPack: - Run preinstall target for: helloworld
    CPack: - Install project: helloworld
    CPack: Create package
    CPack: - package: /export/home/windy/cmake/build/helloworld-0.1.1-SunOS.sh generated.
    CPack: Create package using TGZ
    CPack: Install projects
    CPack: - Run preinstall target for: helloworld
    CPack: - Install project: helloworld
    CPack: Create package
    CPack: - package: /export/home/windy/cmake/build/helloworld-0.1.1-SunOS.tar.gz generated.
    CPack: Create package using TZ
    CPack: Install projects
    CPack: - Run preinstall target for: helloworld
    CPack: - Install project: helloworld
    CPack: Create package
    CPack: - package: /export/home/windy/cmake/build/helloworld-0.1.1-SunOS.tar.Z generated.
    
    생성된 helloworld-0.1.1-SunOS* 파일을 보면 안에 바이너리 파일들이 패키징 되어있는것을 확인할 수 있다.
  5. 디버깅

ccmake

ccmake(1)은 cmake 패키지 설치시 번들되는 CMake의 curses interface 실행파일이다. 기본적으로 cmake와 동일하다.
windy@wl ~/cmake/build $ ccmake ..
                                                     Page 1 of 1
 CMAKE_BUILD_TYPE                                                               
 CMAKE_INSTALL_PREFIX             /usr/local                                    
 ENABLE64BIT                      OFF                                           
 EXTRAMESSAGE                                                                   














CMAKE_BUILD_TYPE: Choose the type of build, options are: None Debug Release RelW
Press [enter] to edit option Press [d] to delete an entry   CMake Version 3.14.4
Press [c] to configure
Press [h] for help           Press [q] to quit without generating
Press [t] to toggle advanced mode (Currently Off)
cmake-gui(1)은 Qt 4.2 이상이 있는 경우 빌드할 수 있다. ccmake 와 유사하지만 GUI 형식으로 제공된다.

cmake 와 autotools(configure)의 명령어 비교

CMake 와 Configure 명령을 비교해 보았다.
구분 cmake 명령 autotools 명령
설정 cmake . ./configure
도움말 보기 cmake . -LH
ccmake .
./configure --help
설치 경로 지정 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/xxx ./configure --prefix=/usr/local/xxx
클린 make clean; rm CMakeCache.txt make clean; rm config.cache;
빌드 상세 make VERBOSE=1 make V=1
RSS ATOM XHTML 5 CSS3