CMake - 1 (Basic)
1. 개요
CMake는 크로스 플랫폼 빌드 시스템을 위한 오픈 소스 도구이다. C, C++ 등의 프로젝트를 보다 효율적으로 관리하고, 다양한 플랫폼에서 빌드할 수 있도록 지원한다. CMake 가 프로젝트 자체를 빌드하는 것은 아니고, 빌드 파일(Makefile)을 생성하는 것이다. Fig 1. 은 CMake의 컴파일 과정을 나타낸 그림이다.

다양한 예제를 작성해 보고, 나중에 필요 시 참고하여 사용할 수 있도록 하자.
2. 실행 파일을 만드는 기본 방법
CMake를 이용해서 기본적인 실행파일을 만드는 방법에 대해서 알아보자. 예제 1에서 소스 코드로 main.cpp만 사용해보고, 예제 2에서는 추가적인 소스코드를 어떻게 CMakeLists에 추가할 수 있는지 알아보자.
2.1 예제 1) 실행 파일을 만드는 기본적인 방법 without custom header file
CMake를 이용해서 간단히 실행파일을 빌드하는 방법을 알아보자.
main 문에서 Hello World를 출력하는 코드를 작성하였고, 이를 실행파일로 만들고자 한다.
main.cpp
1 2 3 4 5 #include <iostream> void main(int argc, char* argv[]) { std::cout << "Hello World" << std::endl; }
CMakeLists에서 minimum cmake version, project name, executable file name을 작성해야 한다.
CMakeLists
1 2 3 4 5 6 7 8 # Required CMake version cmake_minimum_required(VERSION 3.0.0) # Project name project(hello_world) # make executable file add_executable(hello_world_runfile src/main.cpp)
전체 코드 트리는 아래와 같다.
code tree
1 2 3 4 5 my_project/ ├── CMakeLists.txt ├── src/ │ ├── main.cpp └── build/
2.2 예제 2) 실행 파일을 만드는 기본적인 방법 with custom header file
이번에는 헤더파일과 추가 소스코드가 있는 상황에 대해서 어떻게 CMakeLists를 작성할 수 있는지 알아보자. main 문에서 Hello World를 출력하는 코드를 작성하였고, source1.cpp에 추가로 함수를 작성하였다.
main.cpp
1 2 3 4 5 6 #include <iostream> #include "header1.h" void main(int argc, char* argv[]) { std::cout << "Hello World" << std::endl; source1_function(); }
source1.cpp에 source1_function()
을 작성하였다.
source1.cpp
1 2 3 void source1_function() { std::cout << "source1 function call" << std::endl; }
header1.h에 source1_function()
을 선언하였다.
header1.h
1 void source1_function();
CMakeLists 작성 시, 추가적으로 add_library
, target_include_directories
, target_link_libraries
를 작성해야 한다.
각각의 명령어의 의미는 아래와 같다. 각 명령어에 대해 옵션들도 다양하게 있지만, 한번에 작성하면 복잡하니까 이후에 정리하도록 하자.
반드시 알아야 할 것은 library와 include 파일을 추가하고, 링킹하는 방법이다.
- add_library: 라이브러리를 생성하는 명령어다.
- target_include_directories: 타겟(라이브러리 또는 실행 파일)에 대해 헤더 파일 위치를 지정하는 명령어다.
- target_link_libraries: 타겟을 라이브러리와 연결하는 명령어다.
CMakeLists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 # Required CMake version cmake_minimum_required(VERSION 3.0.0) # Project name project(hello_world) # add source files add_library(LIBRARY_NAME src/source1.cpp) # make executable file add_executable(hello_world_runfile src/main.cpp) # include directories target_include_directories(hello_world_runfile PUBLIC include) # link library target_link_libraries(hello_world_runfile LIBRARY_NAME)
전체 코드 트리는 아래와 같다.
code tree
1 2 3 4 5 6 7 my_project/ ├── CMakeLists.txt │── include/header.h ├── src/ │ ├── source1.cpp │ ├── main.cpp └── build/
2.3 예제 3) 실행 파일을 만드는 일반적인 방법 with custom header file
이번에는 예제 2를 확장하여, 조금 더 일반적으로 CMakeLists를 작성하는 방법에 대해서 알아보자. CMakeLists에서도 다른 프로그래밍 언어와 마찬가지로 변수를 추가할 수 있다. 실행파일의 이름을 변수로 바꾸고, 상대경로를 이용해서 CMakeLists를 수정 해보자.
예제 2 대비하여 나머지 코드는 동일하고, CMakeLists만 아래와 같이 수정되었다.
CMakeLists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # Required CMake version cmake_minimum_required(VERSION 3.0.0) # Project name # Language C CXX to support both C and C++ files project(hello_world VERSION 3.0.0 LANGUAGES C CXX) set(EXECUTABLE_NAME Executable) set(LIBRARY_NAME Library) # add source files add_library(${LIBRARY_NAME} STATIC src/source1.cpp) # make executable file add_executable(${EXECUTABLE_NAME} src/main.cpp) # include directories target_include_directories(${EXECUTABLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) # link library target_link_libraries(${EXECUTABLE_NAME} ${LIBRARY_NAME})
전체 코드 트리는 아래와 같다.
code tree
1 2 3 4 5 6 7 my_project/ ├── CMakeLists.txt │── include/header.h ├── src/ │ ├── source1.cpp │ ├── main.cpp └── build/