소스 트리
디렉토리 및 소스파일은 윈디하나의 솔라나라: 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
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
테스트
빌드하고 설치해보자. 출력내용은 색상으로 구분되기 때문에 배경을 검은색으로 변경했다.
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
패키징
CMakeLists.txt
에 INCLUDE(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* 파일을 보면 안에 바이너리 파일들이 패키징 되어있는것을 확인할 수 있다.
디버깅
make VERBOSE=1
와 같이 사용하면 빌드하는 커맨드를 출력해준다.
- cmake 옵션 중
CMAKE_VERBOSE_MAKEFILE
, CMAKE_RULE_MESSAGES
을 사용해서 출력 메시지를 상세하게 조절할 수 있다. 예를들어 cmake -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_VERBOSE_MAKEFILE=ON .
을 추가하거나 CMakeLists.txt
에 set(CMAKE_VERBOSE_MAKEFILE ON)
를 추가하면 된다.