CMake is a build configuration tool. In other words, CMake generates Makefile
s. This post gives an example on how to configure CMake (the actual content of CMakeLists.txt) to build and run C++ code in Windows as well as in Linux. Here it goes:
cmake_minimum_required(VERSION 3.22.1)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
message(STATUS "Checking target system...")
if(WIN32 OR MSVC)
message(STATUS "Target system is Windows")
set(BOOST_ROOT <Path to your boost directory in Windows>)
set(Boost_INCLUDE_DIR ${BOOST_DIR})
endif()
if(UNIX)
message(STATUS "Target system is Linux")
set(BOOST_ROOT <Path to your boost directory in Linux>)
endif()
set(Boost_USE_STATIC_LIBS_ON)
find_package(Boost REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
project(<Project name>)
add_executable(<Project name>
main.cc
)
target_compile_features(<Project name>PUBLIC cxx_std_20)
target_link_libraries(<Project name> ${Boost_SYSTEM_LIBRARY})
The example above links only the headers and not the binaries. How to link binaries will come in the next part.