
上QQ阅读APP看书,第一时间看更新
How to do it
Now that we have the sources in place, our goal will be to configure the project and experiment with compiler flags:
- We set the minimum required version of CMake:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
- We declare the name of the project and the language:
project(recipe-08 LANGUAGES CXX)
- Then, we print the current set of compiler flags. CMake will use these for all C++ targets:
message("C++ compiler flags: ${CMAKE_CXX_FLAGS}")
- We prepare a list of flags for our targets. Some of these will not be available on Windows and we make sure to account for that case:
list(APPEND flags "-fPIC" "-Wall")
if(NOT WIN32)
list(APPEND flags "-Wextra" "-Wpedantic")
endif()
- We add a new target, the geometry library and list its source dependencies:
add_library(geometry
STATIC
geometry_circle.cpp
geometry_circle.hpp
geometry_polygon.cpp
geometry_polygon.hpp
geometry_rhombus.cpp
geometry_rhombus.hpp
geometry_square.cpp
geometry_square.hpp
)
- We set compile options for this library target:
target_compile_options(geometry
PRIVATE
${flags}
)
- We then add a target for the compute-areas executable:
add_executable(compute-areas compute-areas.cpp)
- We also set compile options for the executable target:
target_compile_options(compute-areas
PRIVATE
"-fPIC"
)
- Finally, we link the executable to the geometry library:
target_link_libraries(compute-areas geometry)