CMake 로 빌드시스템 생성하기

숲사람·2022년 5월 4일
0

UNIX & C

목록 보기
8/12

개요

CMake는 빌드시스템이 아니라 빌드시스템을 만들어주는 도구다. 가령 빌드시스템을 make를 쓴다면 CMake가 Makefile을 만들어주고. ninja를 쓴다면 .ninja 빌드파일을 만들어 줄것. GNU autotools 와 유사한 역할이다. autotools는 너무 오래된 도구이기 때문에 이제는 업계 표준으로 자리잡은 CMake를 사용하는게 더 바람직할것이다.

참고:

파일 준비

CMakeLists.txt 파일과 소스파일 그리고 build 디렉터리 생성. cmake명령을 새 디렉토리를 만들고 거기서 해야함. 안그러면 수많은 파일들이 소스디렉토리에 생겨서 지저분해질것임.

$ ls
CMakeLists.txt   main.c   build/
  • CMakeLists.txt
# CMake 프로그램 최소 버전
cmake_minimum_required(VERSION 3.0)

# 프로그램 정보
project(
  CmakeTest
  VERSION 0.1
  LANGUAGES C)

add_executable (program main.c)

컴파일러를 따로 명시할 필요는 없는것같고 자동으로 찾아주는듯.
참고로 테스트한 우분투 환경의 cmake version 3.5.1

  • main.c
#include <stdio.h>

int main(int argc, char *argv[])
{
        printf("Hello CMake\n");
        return 0;
}

Makefile 생성

$ 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
profile
기록 & 정리 아카이브 용도 (보다 완성된 글은 http://soopsaram.com/documentudy)

0개의 댓글