[CMake Tutorial] CMake Documentation

CMake 명령어

  • cmake_minimum_required()

Cmake 최소버전을 명시해준다. 보통 CMakeLists.txt의 최상단에 작성해준다.

ex) cmake_minimum_required(VERSION 3.25)
  • project()

project 이름을 설정하기 위해 사용된다.

ex) project(pcl_test)
  • set()

변수 정의를 위해 사용된다. set(변수명 값)과 같은 형태로 명시해주면 된다.

set(CMAKE_CXX_STANDARD 14)
  • find_package()

설치되어있는 package를 찾아준다. 또한, REQUIRED를 추가하게되면 라이브러리를 찾지못한 경우, 에러를 발생시킨다.

ex1) find_package(PCL 1.2) # find PCL 1.2 version

ex2) find_package(PCL 1.2 REQUIRED)

성공적으로 라이브러리를 찾으면 아래 변수들에 라이브러리 탐색성공여부, 라이브러리 경로와 같은 값이 할당된다.

    • name_FOUND
    • name_INCLUDE_DIRS (or name_INCLUDES)
    • name_LIBRARIES (or name_LIBS)
    • name_DEFINITIONS
if(PCL_FOUND)
endif()

예를들어 PCL_FOUND 형태의 변수를 통해 라이브러리를 제대로 찾았는지 확인할 수 있다.

  • include_directories(), target_link_libraries()

find_package를 통해 지정된 라이브러리를 탐색한 후, include_directories와 target_link_libraries를 통해 실질적인 추가하는 작업이 필요하다.

include_directories(${PCL_INCLUDE_DIRS})
target_link_libraries (${PCL_LIBRARIES})
  • add_executable()

add_executable을 통해서 실행파일을 생성할 수 있다. 이때, 명시된 소스코드는 상대경로로 찾아들어간다.

ex) add_executable(test
    test.cpp
    test.h
)

test라는 이름의 실행파일 즉, test.exe를 test.cpp, test.h와 같은 소스코드를 가지고 생성한다.

  • add_library()

라이브러리 생성시 사용된다. STATIC의 경우 정적 링킹 라이브러리(.a, .lib)를 SHARED는 동적 링킹 라이브러리를 생성할 수 있게 해준다.

ex1) add_library(test_lib STATIC
    test.cpp
    test.h
)

ex2) add_library(test_lib SHARED
    test.cpp
    test.h
)

라이브러리 링킹 형태를 따로 명시하지 않고 라이브러리를 생성할 수도 있다. 이때, 라이브러리 링킹 형태는 BUILD_SHARED_LIBS 변수에 할당해주면 된다.

ex) add_library(test_lib
    test.cpp
    test.h
)
  • add_definitions()

전처리기에 전달할 macro 정의를 위해 사용하며, 컴파일러 옵션의 경우 -D에 해당된다.

Leave a Reply

Your email address will not be published. Required fields are marked *