-
Notifications
You must be signed in to change notification settings - Fork 2.2k
CMake contribution rules
-
Don't make any assumption about the platform or compiler!
CMake takes care that it works on all compilers and platforms.
Compiler and platform test results are cached in CMakeCache.txt -
Don't modify global compile/link flags.
-
Don't assume that this project root is the build root.
-
Don't make any global changes!
-
Always add Poco namespace alias for libraries.
Like:add_library(Poco::Foundation ALIAS foundation)
-
Don't pass STATIC/SHARED on
add_library()
, unless they cannot be built otherwise!
Leave the control ofBUILD_SHARED_LIBS
to the clients -
Link against namespaced targets.
Like:target_link_libraries(crypto Poco::Foundation OpenSSL::Crypto)
-
Specify the dependencies are PRIVATE or PUBLIC.
-
PRIVATE Only used for this target
-
PUBLIC Used for this target and all targets that link against it.
This will also export into thexxxConfig.cmake
forfind_package()
.
-
INTERFACE Only used for targets that link against this library.
-
PRIVATE Only used for this target
target_<usage_requirement>(<target> <PRIVATE|PUBLIC|INTERFACE] <lib> ... [<PRIVATE|PUBLIC|INTERFACE] <lib> ...] ... )
-
Don't use
link_libraries()
command.
Use:target_link_libraries()
-
Don't use
link_directories()
command.
Use:target_link_libraries()
-
Don't use
include_directories()
command.
Use:target_include_directories()
-
Don't use
add_definitons()
command.
Use:target_compile_definitons()
-
Don't use
add_compile_options()
command.
Use:target_compile_options()
-
Don't add definitons or options to
CMAKE_<LANG>_FLAGS
.
Use:target_compile_definitons()
-
Wrap compiler specific options in an appropriate condition.
Like:
if(CMAKE_COMPILER_IS_GNUCXX) target_compile_options(foo PUBLIC -fno-elide-constructors ) endif()
-
Don't add or pass
-std=c++11
or-std=c++yy
toCMAKE_<LANG>_FLAGS
ortarget_compile_options()
.
Use:target_compile_features()
-
No need to call
add_dependencies()
. -
Don't use
file(GLOB ..)
in configure mode. (List all sources explicitly!)
Like:
file(GLOB sources "*.cpp") add_library(mylib ${sources})
-
Avoid custom variables, variables are fragile. Use targets and properties!
If it is not possilbe to avoid, addPOCO_
as prefix! -
Modern cmake is about targets and properties!
Here an overview howto use:
Constructors for targets:
Target member variables:add_executable()
add_library()
See properties on targets in cmake documentation.
Target member functions:
get_target_property()
set_target_properties()
get_property(TARGET)
set_property(TARGET)
target_compile_definitions()
target_compile_features()
target_compile_options()
target_include_directories()
target_link_libraries()
target_sources()
This best practices are from Daniel Pfeifer. He has a good experience about cmake.
It is recommended to look at his presentation: C++Now 2017: Daniel Pfeifer “Effective CMake"
Link: