DEV Community

tinhnguyentrung
tinhnguyentrung

Posted on

CMake Basic 2

The first line

cmake_minimum_required(VERSION 3.25)

# range supported

cmake_minimum_required(VERSION 3.7...3.26)

if(${CMAKE_VERSION} VERSION_LESS 3.12)
    cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
endif()
Enter fullscreen mode Exit fullscreen mode

If the CMake versions is less than 3.12, the if block will be true, and the policy will be set to the current CMake version. if CMake is 3.12 or higher, the if block is false but the new syntax in cmake_minimum_required will be respected.

Setting a project

project(MyProject VERSION 1.0
                  DESCRIPTION "Very nice project"
                  LANGUAGES CXX)
Enter fullscreen mode Exit fullscreen mode

Making an executable

add_executable(one two.cpp three.h)
Enter fullscreen mode Exit fullscreen mode
  • one: is the name of executable file which is generated
  • one: is the name of the CMake target created
  • two.cpp three.h : is the source files

Making a library

add_library(one STATIC two.cpp three.h)
Enter fullscreen mode Exit fullscreen mode

Library type: STATIC, SHARED and MODULE.

Targets are your friend

# include directory

target_include_directories(one PUBLIC include)
                           target name
                                 policy?
                                       directory
Enter fullscreen mode Exit fullscreen mode

PUBLIC -> for a library, it lets CMake know that any targets that link to this target must also need that include directory.

PRIVATE -> only affect the current target

INTERFACE -> only needed for dependencies ?!

We can then chain targets

add_library(another STATIC another.cpp another.h)
target_link_libraries(another PUBLIC one)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)