CMake는 빌드시스템이 아니라 빌드시스템을 만들어주는 도구다. 가령 빌드시스템을 make를 쓴다면 CMake가 Makefile을 만들어주고. ninja를 쓴다면 .ninja 빌드파일을 만들어 줄것. GNU autotools 와 유사한 역할이다. autotools는 너무 오래된 도구이기 때문에 이제는 업계 표준으로 자리잡은 CMake를 사용하는게 더 바람직할것이다.
참고:
CMakeLists.txt 파일과 소스파일 그리고 build 디렉터리 생성. cmake명령을 새 디렉토리를 만들고 거기서 해야함. 안그러면 수많은 파일들이 소스디렉토리에 생겨서 지저분해질것임.
$ ls
CMakeLists.txt main.c build/
# CMake 프로그램 최소 버전
cmake_minimum_required(VERSION 3.0)
# 프로그램 정보
project(
CmakeTest
VERSION 0.1
LANGUAGES C)
add_executable (program main.c)
컴파일러를 따로 명시할 필요는 없는것같고 자동으로 찾아주는듯.
참고로 테스트한 우분투 환경의 cmake version 3.5.1
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello CMake\n");
return 0;
}
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jihuun/project/c_language/cmake_test/build
build/ 경로에서 cmake ..
수행시 현재 디렉터리에 Makefile 생성됨. cmake
의 역할은 여기까지
이제 make로 빌드하고 실행하는것은 사용자의 몫. CMakeLists.txt 에 실행파일명을 program이라고 명시했고. 해당 executable파일이 생성된것 확인.
$ make
Scanning dependencies of target program
[ 50%] Building C object CMakeFiles/program.dir/main.c.o
[100%] Linking C executable program
[100%] Built target program
$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile program
$ ./program
Hello CMake