The following are some of the valid targets for this Makefile: ... all (the default if no target is provided) ... clean ... depend ... edit_cache ... rebuild_cache ... hello-world ... hello-world.o ... hello-world.i ... hello-world.s
2. Switching generators
Choose Ninja:
1 2 3 4
$ mkdir -p build $ cd build $ cmake -G Ninja .. # must install Ninja first $ cmake --build .
Single command:
1
cmake -H. -Bbuild -GNinja
3. Building and linking static and shared libraries
CmakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-03 LANGUAGES CXX)
# generate a library from sources add_library(message STATIC Message.hpp Message.cpp )
add_executable(hello-world hello-world.cpp)
target_link_libraries(hello-world message)
add_library(message STATIC Message.hpp Message.cpp): add_library’s first param is target name, available for entire CMakeLists.txt to be refered. Actually name will be prefixed with lib and suffixed withbash proper extension by cmake, determined by (STATIC or SHARED) and OS
target_link_libraries(hello-world message): link library to executable and make sure hello-world depends correctly on library. Must after add_library.
After build, we get libmessage.a together with executable hello-world.
# set minimum cmake version cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# project name and language project(recipe-05 LANGUAGES CXX)
# expose options to the user option(USE_LIBRARY "Compile sources into a library" OFF)
message(STATUS "Compile sources into a library? ${USE_LIBRARY}")
include(CMakeDependentOption)
# second option depends on the value of the first cmake_dependent_option( MAKE_STATIC_LIBRARY "Compile sources into a static library" OFF "USE_LIBRARY" ON )
# third option depends on the value of the first cmake_dependent_option( MAKE_SHARED_LIBRARY "Compile sources into a shared library" ON "USE_LIBRARY" ON )
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
# list sources list(APPEND _sources Message.hpp Message.cpp)
if(USE_LIBRARY) message(STATUS "Compile sources into a STATIC library? ${MAKE_STATIC_LIBRARY}") message(STATUS "Compile sources into a SHARED library? ${MAKE_SHARED_LIBRARY}")
# set minimum cmake version cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# project name and language project(recipe-06 LANGUAGES C CXX)
message(STATUS "Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}") if(CMAKE_CXX_COMPILER_LOADED) message(STATUS "The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}") message(STATUS "Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCXX}") message(STATUS "The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}") endif()
message(STATUS "Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}") if(CMAKE_C_COMPILER_LOADED) message(STATUS "The C compiler ID is: ${CMAKE_C_COMPILER_ID}") message(STATUS "Is the C from GNU? ${CMAKE_COMPILER_IS_GNUCC}") message(STATUS "The C compiler version is: ${CMAKE_C_COMPILER_VERSION}") endif()
# we use the IN LISTS foreach syntax to set source properties message(STATUS "Setting source properties using IN LISTS syntax:") foreach(_source IN LISTS sources_with_lower_optimization) set_source_files_properties(${_source} PROPERTIES COMPILE_FLAGS -O2) message(STATUS "Appending -O2 flag for ${_source}") endforeach()
# we demonstrate the plain foreach syntax to query source properties # which requires to expand the contents of the variable message(STATUS "Querying sources properties using plain syntax:") foreach(_source ${sources_with_lower_optimization}) get_source_file_property(_flags ${_source} COMPILE_FLAGS) message(STATUS "Source ${_source} has the following extra COMPILE_FLAGS: ${_flags}") endforeach()