상세 컨텐츠

본문 제목

CMake - 1부 (Tutorial 시작)

Development

by chbae 2023. 4. 18. 05:59

본문

728x90
반응형

cmake?

CMake는 쉽게 말하면 Makefile 파일을 쉽게 만들어 주는 Make의 meta라고 생각할 수 있다. 리눅스, 윈도우 등 다양한 플랫폼에서 사용할 수 있으며 기존의 autotools 보다 훨씬 쉽게 사용할 수 있도록 만들어 졌다. 필자가 webOS 개발 프로젝트에 참여 중인데, 가장 많이 쓰는 component 빌드 스크립트 중에 하나이다.

 

아래는 공식 사이트의 Tutorial(http://www.cmake.org/cmake-tutorial/)을 보고 필자가 이해한 대로 정리한 것이다. 이 글에서는 Step 1에 대해서 정리하고 이후 Step 2 ~ 7 까지 정리하도록 한다.

Step 1: Tutorial 시작

line 1, 2 의 필요한 cmake 최소 버전과 project 이름은 반드시 적어야 하는 필수 사항이다.

1:  cmake_minimum_required (VERSION 2.6)  
2:  project (Tutorial)  
3:  add_executable(Tutorial tutorial.cxx)

아래는 테스트로 사용할 tutorial.cxx 예제이다.

1:  // A simple program that computes the square root of a number  
2:  #include <stdio.h>  
3:  #include <stdlib.h>  
4:  #include <math.h>  
5:  int main (int argc, char *argv[])  
6:  {  
7:   if (argc < 2)  
8:    {  
9:    fprintf(stdout,"Usage: %s number\n",argv[0]);  
10:    return 1;  
11:    }  
12:   double inputValue = atof(argv[1]);  
13:   double outputValue = sqrt(inputValue);  
14:   fprintf(stdout,"The square root of %g is %g\n",  
15:       inputValue, outputValue);  
16:   return 0;  
17:  }

버전 번호와 헤더 파일 추가 아래 CMakeLists.txt 파일은 기존 최조 내용에서 버전 번호와 헤더 파일 등이 추가 되었다.

1:  cmake_minimum_required (VERSION 2.6)  
2:  project (Tutorial)  
3:  # The version number.  
4:  set (Tutorial_VERSION_MAJOR 1)  
5:  set (Tutorial_VERSION_MINOR 0)  
6:  # configure a header file to pass some of the CMake settings  
7:  # to the source code  
8:  configure_file (  
9:   "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"  
10:   "${PROJECT_BINARY_DIR}/TutorialConfig.h"  
11:   )  
12:  # add the binary tree to the search path for include files  
13:  # so that we will find TutorialConfig.h  
14:  include_directories("${PROJECT_BINARY_DIR}")  
15:  # add the executable  
16:  add_executable(Tutorial tutorial.cxx)

아래는 TutorialConfig.h.in 파일이다. 위의 CMakeLists.txt의 선언된 변수(line 4,5)를 아래와 같이 헤더나 소스에서 사용할 수도 있다.

1:  // the configured options and settings for Tutorial  
2:  #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@  
3:  #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

실제 최종 소스코드인 tutorial.cxx 파일이다.

1:  // A simple program that computes the square root of a number  
2:  #include <stdio.h>  
3:  #include <stdlib.h>  
4:  #include <math.h>  
5:  #include "TutorialConfig.h"  
6:  int main (int argc, char *argv[])  
7:  {  
8:   if (argc < 2)  
9:    {  
10:    fprintf(stdout,"%s Version %d.%d\n",  
11:        argv[0],  
12:        Tutorial_VERSION_MAJOR,  
13:        Tutorial_VERSION_MINOR);  
14:    fprintf(stdout,"Usage: %s number\n",argv[0]);  
15:    return 1;  
16:    }  
17:   double inputValue = atof(argv[1]);  
18:   double outputValue = sqrt(inputValue);  
19:   fprintf(stdout,"The square root of %g is %g\n",  
20:       inputValue, outputValue);  
21:   return 0;

빌드는 CMakeLists.txt 파일이 있는 디렉토리에서 cmake . 을 하면 Makefile이 생성 되고 이후 make를 하면 최종 Tutorial 실행 파일이 생성된다. 더 세부적인 내용은 아래 Reference의 두번째 link인 cmake-tutorial 부분을 따라가면 더 알 수 있다.

Reference

728x90

'Development' 카테고리의 다른 글

CMake - 3부 (설치와 테스트)  (0) 2023.04.18
CMake - 2부 (라이브러리 추가)  (0) 2023.04.18
ack (ack-grep)  (0) 2023.04.18
git (깃)  (0) 2023.04.18
(터미널 프로그램) mobaxterm  (0) 2023.04.18

관련글 더보기