CMake 설치
>> brew install cmake
CMake 버전 확인
>> cmake --version
cmake version 3.23.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
기본 프로젝트 세팅하기
1단계 예제를 수행할 Step01_source 폴더를 생성합니다.
>> mkdir Step01_source
Step01_source 폴더에 다음 두 파일을 추가합니다.
CMakeLists.txt
# cmake 버전을 지정
cmake_minimum_required(VERSION 3.23.2)
# 프로젝트 이름 설정
project(hello_world)
# 실행 파일 추가
add_executable(hello_world main.cpp)
main.cpp
#include <iostream>
int main() {
std::cout << "Hello World!\\n";
return 0;
}
Step01_source에 대한 빌드 디렉토리 Step01_build 폴더를 생성합니다.
>> mkdir Step01_build
빌드 디렉토리로 이동 후. cmake로 Step01_source의 프로젝트를 구성하고 기본 빌드 시스템을 생성합니다.
>> cd Step01_build
>> cmake ../Step01_source
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhjang/Dev/cmake_tutorial/step01_build
프로젝트를 컴파일, 링크 합니다.
>> cmake --build .
[ 50%] Building CXX object CMakeFiles/hello_world.dir/main.cpp.o
[100%] Linking CXX executable hello_world
[100%] Built target hello_world
실행해봅니다.
>> ./hello_world
Hello World!
2단계. 버전 번호 및 구성된 헤더 파일 추가
HelloWorldConfig.h.in
CMake로 헤더가 구성될때 HelloWorldConfig.h.in는 HelloWorldConfig.h 파일로 재생성될 것입니다.
HelloWorldConfig.h는 프로그램 버전 정보를 정의하는 헤더입니다.
// HelloWorld에 대해 구성된 옵션과 설정
#define HelloWorld_VERSION_MAJOR @HelloWorld_VERSION_MAJOR@
#define HelloWorld_VERSION_MINOR @HelloWorld_VERSION_MINOR@
프로젝트명이 HelloWorld가 아닐 경우, [프로젝트명]_VERSION_MAJOR 등으로 고쳐야 합니다.
main.cpp
프로그램의 버전 정보를 출력하는 코드를 작성합니다.
#include <iostream>
#include "HelloWorldConfig.h"
int main() {
std::cout << "Version : " << HelloWorld_VERSION_MAJOR << "." << HelloWorld_VERSION_MINOR << std::endl;
return 0;
}
CMakeLists.txt
# cmake 버전을 지정
cmake_minimum_required(VERSION 3.23.2)
# 프로젝트 이름과 버전을 설정
project(HelloWorld VERSION 1.0)
# 버전 정보를 소스코드에 전달
configure_file(HelloWorldConfig.h.in HelloWorldConfig.h)
# 실행 파일 추가
add_executable(HelloWorld main.cpp)
# 구성된 파일은 이진 트리에 기록됩니다.
# 파일을 include 하기 위한 이진 트리를 추가합니다.
# HelloWorldConfig.h를 찾게 될 것입니다.
target_include_directories(HelloWorld PUBLIC
"${PROJECT_BINARY_DIR}"
)
빌드 시스템 생성 및 실행
>> cmake ../Step02_source
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhjang/Dev/cmake_tutorial/Step02_build
~/Dev/cmake_tutorial/Step02_build
cmake ../Step02_source
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhjang/Dev/cmake_tutorial/Step02_build
~/Dev/cmake_tutorial/Step02_build
cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhjang/Dev/cmake_tutorial/Step02_build
>> cmake --build .
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o
[100%] Linking CXX executable HelloWorld
[100%] Built target HelloWorld
>> ./HelloWorld
Version : 1.0
3단계. C++ 표준 지정
C++ 11 표준의 기능을 사용하는 예제를 만들어봅니다.
main.cpp
#include <iostream>
int main() {
int a[]{1,2,3,4};
for(auto i : a)
std::cout << i << std::endl;
return 0;
}
CMakeLists.txt
C++ 표준을 지정하는 구문을 add_executable 위에 선언해주어야 합니다.
# cmake 버전을 지정
cmake_minimum_required(VERSION 3.23.2)
# 프로젝트 이름 설정
project(HelloWorld)
# C++ 표준 지정 (C++ 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# 실행 파일 추가
add_executable(HelloWorld main.cpp)
빌드 시스템 생성 및 실행
>> cmake ../Step03_source
-- The C compiler identification is AppleClang 13.1.6.13160021
-- The CXX compiler identification is AppleClang 13.1.6.13160021
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yhjang/Dev/cmake_tutorial/Step03_build
>> cmake --build .
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o
[100%] Linking CXX executable HelloWorld
[100%] Built target HelloWorld
>> ./HelloWorld
1
2
3
4
레퍼런스
https://cmake.org/cmake/help/latest/guide/tutorial/index.html
CMake Tutorial — CMake 3.24.0 Documentation
Introduction The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address. Seeing how various topics all work together in an example project can be very helpful.
cmake.org
댓글