개발/개발 환경

[개발 환경] Make는 이제 그만. CMake 에 대해서 알아보자

growing-dev 2023. 1. 18. 21:55
반응형

CMake(Cross Platform Make)는 멀티플랫폼으로 사용할 수 있는 Make의 빌드관리시스템을 만들기 위한 오픈소스 프로젝트이다.

즉 기존의 Make 방식을 보완하는 오픈소스 프로젝트로서 조금 더 사용자 친화적으로 빌드시스템을 구축할 수 있는 것이 특징이다.

https://cmake.org/

 

CMake

“…CMake, CTest, and CDash have proven to be invaluable tools for us to build multiplatform code, track changes, run tests, and improve code quality by performing code coverage and memory leak analysis.”

cmake.org

기능

  • 소프트웨어 빌드에 특화된 언어로 독자적 설정 스크립트를 사용한다.
  • C, C++, 자바 등의 언어를 지원한다.
  • 각종 IDE에서 자체적으로 CMake를 분석할 수 있다.
  • 다양한 플랫폼(리눅스, 윈도)에서 크로스 컴파일을 지원한다.

 

장점

  • 익숙해지면 Make에 비해 복잡하지 않고 단순하다.
  • 코드를 모듈화해서 관리하기 쉽다. CMakeList.txt 혹은 xx.cmake 파일을 통해 구조적으로 모듈화 하기 쉽다.
  • 모듈화 된 라이브러리들의 의존성 관리가 쉽다. PUBLIC, PRIVATE 등의 속성을 통해 라이브러리의 캡슐화가 가능하다.
  • 각종 환경에 이미 적용되어 있어서 호환성에 좋다.
  • 한 레파지토리 안에서 시스템이나 플랫폼(OS 환경, 컴파일러)에 따른 빌드 설정이 간편하고, 여러 과제(H/W 버전에 따른 One repo 적용 등)를 한 번에 관리하여 선택적으로 빌드하는 데 있어서도 매우 편리하다.

 

CMake 파일 예제

cmake_minimum_required, project, add_library, target_include_directories, target_link_libraries 정도가 필수적으로 필요한 문법이다.

아래 예시에는 unit test 까지 포함할 수 있도록 구현한 예제이고 이 파일을 CMakeList.txt로 프로젝트에 포함시킨 다음

cmake를 명령어로 실행하던지, IDE에서 CMake 프로젝트로 Load 하면 빌드할 수 있다.

cmake_minimum_required(VERSION 3.16)

# Set the project name
project (unit_test)


# Add an library for the example classes
add_library(example_unit_test
    test.cpp
)

target_include_directories(example_unit_test
    PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
)

target_link_libraries(example_unit_test
    PUBLIC
       TEST
)

#############################################
# Unit tests

# enable CTest testing
enable_testing()

# Add a testing executable
add_executable(unit_tests unit_tests.cpp)

target_link_libraries(unit_tests
    example_unit_test
)

target_compile_definitions(unit_tests
    PRIVATE
        TEST_DYN_LINK
)

add_test(test_all unit_tests)

 

참고 링크

아래는 github에서 CMake에 대해서 공부하면 좋을 자료들이다. 이 자료들을 통해 좀 더 쉽게 이해할 수 있었다.

https://gist.github.com/luncliff/6e2d4eb7ca29a0afd5b592f72b80cb5c?permalink_comment_id=2831356 

 

CMake 할때 쪼오오금 도움이 되는 문서

CMake 할때 쪼오오금 도움이 되는 문서. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

반응형