Let us build a corresponding CMakeLists.txt instance, which will enable us to conditionally compile the source code based on the target OS:
- We first set the minimum CMake version, project name, and supported language:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(recipe-02 LANGUAGES CXX)
- Then we define the executable and its corresponding source file:
add_executable(hello-world hello-world.cpp)
- Then we let the preprocessor know the system name by defining the following target compile definitions:
if(CMAKE_SYSTEM_NAME STREQUAL "Linux") target_compile_definitions(hello-world PUBLIC "IS_LINUX")endif()if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") target_compile_definitions(hello-world PUBLIC "IS_MACOS")endif()if(CMAKE_SYSTEM_NAME ...