CMake를 이용한 C 언어 프로그래밍 #2 : 컴파일 정의(definition) 추가하기

반응형

 

CMake를 이용한 C 언어 프로그래밍 #2 : 컴파일 정의(definition) 추가하기

본 글에서는 CMake를 사용하여 컴파일할 때 컴파일 정의(definition)을 추가하는 방법을 설명한다.

 

다음 두 가지 cmake 커맨드를 통해 컴파일 정의를 추가할 수 있다.

  • add_compile_definitions()
  • target_compile_definitions()

 

add_compile_definitions()

add_compile_definitions() 커맨드는 현재 CMakeLists.txt 및 하위 CMakeLists.txt 내에서 전역으로 적용되는 정의를 추가하는데 사용된다.

즉, 본 커맨드를 통해 정의를 추가하면 CMakeLists.txt 상에 기술된 모든 타겟에 동일하게 적용된다.

 

다음은 add_compile_definitions() 커맨드를 통해 각 어플리케이션의 컴파일 정의를 추가하는 예제이다.

add_compile_definitions() 커맨드에 의해 app1 및 app2 컴파일 시에 "_DEBUG_"가 정의되어 컴파일된다.

 

CMakeLists.txt

set(TARGET1 app1)
set(TARGET2 app2)

add_compile_definitions(_DEBUG_)
add_executable(${TARGET1} app1.c)
add_executable(${TARGET2} app2.c)

 

app1.c

#include <stdio.h>

int main() 
{
#ifdef _DEBUG_
  printf("Running app1 on debug mode\n");
#else
  printf("Running app1\n");
#endif

  return 0;
}

 

app2.c

#include <stdio.h>

int main() 
{
#ifdef _DEBUG_
  printf("Running app2 on debug mode\n");
#else
  printf("Running app2\n");
#endif

  return 0;
}

 

두 어플리케이션 모두 _DEBUG_ 정의와 함께 컴파일 되었으므로, 각각을 실행하면 다음과 같이 실행된다.

root@7c65e9e5fcb3:/workspace/cmake/definitions# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /workspace/cmake/definitions
root@7c65e9e5fcb3:/workspace/cmake/definitions#
root@7c65e9e5fcb3:/workspace/cmake/definitions#
root@7c65e9e5fcb3:/workspace/cmake/definitions# make
[ 25%] Building C object CMakeFiles/app2.dir/app2.o
[ 50%] Linking C executable app2
[ 50%] Built target app2
[ 75%] Building C object CMakeFiles/app1.dir/app1.o
[100%] Linking C executable app1
[100%] Built target app1
root@7c65e9e5fcb3:/workspace/cmake/definitions# ./app1
Running app1 on debug mode
root@7c65e9e5fcb3:/workspace/cmake/definitions# ./app2
Running app2 on debug mode

 

 

target_compile_definitions()

본 커맨드를 사용하면 특정 타겟을 컴파일할 때에만 적용되는 항목을 정의할 수 있다.

사용법은 다음과 같다.

target_compile_definitions(<target>
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

 

PUBLIC을 사용하면, 해당 target에 의존성을 가지는 또 다른 타겟(예: 본 target이 라이브러리이고, 이 라이브러리를 링크하는 어플리케이션)에도 동일한 정의가 적용된다.

PRIVATE을 사용하면, 해당 target에만 적용된다.

 

다음은 target_compile_definitions() 커맨드를 통해 특정 타겟에 대한 컴파일 정의를 추가하는 예제이다.

target_compile_definitions() 커맨드에 의해 app1 컴파일 시에만 _DEBUG_가 정의되어 컴파일된다.

 

CMakeLists.txt

set(TARGET1 app1)
set(TARGET2 app2)

add_executable(${TARGET1} app1.c)
add_executable(${TARGET2} app2.c)
target_compile_definitions(${TARGET1} PRIVATE _DEBUG_)

 

app1 만이 _DEBUG_ 정의와 함께 컴파일되므로, 각 어플리케이션을 실행하면 다음과 같이 실행된다.

root@7c65e9e5fcb3:/workspace/cmake/definitions# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /workspace/cmake/definitions
root@7c65e9e5fcb3:/workspace/cmake/definitions#
root@7c65e9e5fcb3:/workspace/cmake/definitions#
root@7c65e9e5fcb3:/workspace/cmake/definitions# make
[ 25%] Building C object CMakeFiles/app2.dir/app2.o
[ 50%] Linking C executable app2
[ 50%] Built target app2
[ 75%] Building C object CMakeFiles/app1.dir/app1.o
[100%] Linking C executable app1
[100%] Built target app1
root@7c65e9e5fcb3:/workspace/cmake/definitions#
root@7c65e9e5fcb3:/workspace/cmake/definitions# ./app1
Running app1 on debug mode
root@7c65e9e5fcb3:/workspace/cmake/definitions# ./app2
Running app2
root@7c65e9e5fcb3:/workspace/cmake/definitions#
주의: target_compile_definitions() 커맨드(및 그외 모든 target_**() 커맨드)를 사용할 때에는 add_executable()과 같은 빌드 커맨드 아래 쪽에 커맨드를 작성해야 한다. 그렇지 않고 target_compile_definitions() 커맨드를 먼저 작성하면 cmake 실행 시 에러가 발생한다.

 

파트너스 활동을 통해 일정액의 수수료를 제공받을 수 있음

댓글

Designed by JB FACTORY