cmake - step 1

O.S/Linux 2015. 10. 22. 17:19
기본 설정

간단한 프로젝트를 위해서는 아래와 같이 CMakeLists 파일에 두줄만 추가하면된다. 

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

위의 예제에서는 cmake 명령어를 소문자를 썼지만, cmake 명령어는 대소문자를 가리지 않고 동작한다.

아래의 소스코드는 튜토리얼 위한 tutorial.cxx 파일이다. 이 코드는 입력한 수의 제곱근을 구하는 간단한 코드이다.

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


버전 넘버 추가와 헤더 파일 구성하기


처음으로 추가할 기능은 프로젝트의 버전 넘버 추가이다.
소스코드에서 독점적으로 이 기능을 수행할 수 있지만 CMakeLists 파일에서 하는 것이 더 많은 유연성을 제공한다.
버전 넘버를 추가하기 위해서 아래와 같이 작성한다.

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


구성 파일이 이전 트리에 기록되기 때문에 파일을 검색하기 위한 경록 목록에 해당 디렉토리를 추가해야한다.
아래와 같은 TutorialConfig.h.in 파일을 생성한다.

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

cmake 가 위의 헤더파일을 구성할때 @Tutorial_VERSION_MAJOR@ 및 @Tutorial_VERSION_MINOR@ 값은 CMakeLists 에 있는 값으로 변환된다. 다음으로 tutorial.cxx 파일에 구성된 헤더파일을 포함하도록 한다.

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




WRITTEN BY
Profile
- 선빈 -


,