From 3335200e432b0dd51979907a493c49dc8c46de3f Mon Sep 17 00:00:00 2001 From: ttroy50 Date: Fri, 15 Apr 2016 23:49:39 +0100 Subject: [PATCH] started updating examples for cmake v3 syntax --- .gitignore | 1 + 01-basic/A-hello-cmake/CMakeLists.txt | 2 +- 01-basic/B-hello-headers/CMakeLists.txt | 16 +++++++------- 01-basic/B-hello-headers/README.adoc | 27 ++++++++++++------------ 01-basic/C-static-library/CMakeLists.txt | 13 ++++++------ 01-basic/C-static-library/README.adoc | 16 +++++++++++++- 01-basic/D-shared-library/CMakeLists.txt | 13 ++++++------ 01-basic/D-shared-library/README.adoc | 20 ++++++++++++++---- 01-basic/E-installing/CMakeLists.txt | 13 ++++++------ 01-basic/F-build-type/CMakeLists.txt | 2 +- 01-basic/G-compile-flags/CMakeLists.txt | 6 +++++- 01-basic/G-compile-flags/README.adoc | 15 ++++++++++++- 12 files changed, 96 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index be7ebf6..2896b54 100644 --- a/.gitignore +++ b/.gitignore @@ -59,3 +59,4 @@ install_manifest.txt /**/build /**/build.* +/1 diff --git a/01-basic/A-hello-cmake/CMakeLists.txt b/01-basic/A-hello-cmake/CMakeLists.txt index ec649c0..f43b8c7 100644 --- a/01-basic/A-hello-cmake/CMakeLists.txt +++ b/01-basic/A-hello-cmake/CMakeLists.txt @@ -1,7 +1,7 @@ # Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) # Set the project name project (hello_cmake) diff --git a/01-basic/B-hello-headers/CMakeLists.txt b/01-basic/B-hello-headers/CMakeLists.txt index 6476a26..d326979 100644 --- a/01-basic/B-hello-headers/CMakeLists.txt +++ b/01-basic/B-hello-headers/CMakeLists.txt @@ -1,17 +1,11 @@ # Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) # Set the project name project (hello_headers) -# Set the direcoties that should be included in the build command -# when running g++ these will be included as -I/directory/path/ -include_directories( - ${PROJECT_SOURCE_DIR}/inc - ) - # Create a sources variable with a link to all cpp files to compile set(SOURCES src/Hello.cpp @@ -20,4 +14,10 @@ set(SOURCES # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) \ No newline at end of file +add_executable(hello_headers ${SOURCES}) + +# Set the direcoties that should be included in the build command for this target +# when running g++ these will be included as -I/directory/path/ +target_include_directories(hello_headers + PRIVATE ${PROJECT_SOURCE_DIR}/inc +) \ No newline at end of file diff --git a/01-basic/B-hello-headers/README.adoc b/01-basic/B-hello-headers/README.adoc index b875aa4..e7176ca 100644 --- a/01-basic/B-hello-headers/README.adoc +++ b/01-basic/B-hello-headers/README.adoc @@ -55,19 +55,6 @@ directory where you ran the cmake command. |PROJECT_BINARY_DIR |The build directory for the current project. |======================================================================= -## Including Directories - -When you have different include folders, you can make your compiler aware of them using the -+include_directories()+ function. This will add these directories to the -compiler with the -I flag e.g. `-I/directory/path` - -[source,cmake] ----- -include_directories( - ${PROJECT_SOURCE_DIR}/inc - ) ----- - ## Source Files Variable Creating a variable which includes the source files allows you to be @@ -96,6 +83,20 @@ file(GLOB SOURCES "src/*.cpp") ---- ==== +## Including Directories + +When you have different include folders, you can make your compiler aware of them using the ++target_include_directories()+ function. When compiling this target this will add these directories to the compiler with the -I flag e.g. `-I/directory/path` + +[source,cmake] +---- +target_include_directories(target + PRIVATE ${PROJECT_SOURCE_DIR}/inc + ) +---- + +The +PRIVATE+ identifier specifies the scope of the include. This is important for libraries and is exlpained in the next example. More details on the function is available link:https://cmake.org/cmake/help/v3.0/command/target_include_directories.html[here] + # Building the Example ## Standard Output diff --git a/01-basic/C-static-library/CMakeLists.txt b/01-basic/C-static-library/CMakeLists.txt index 3caef11..7624d23 100644 --- a/01-basic/C-static-library/CMakeLists.txt +++ b/01-basic/C-static-library/CMakeLists.txt @@ -1,11 +1,7 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) project(hello_library) -include_directories( - ${PROJECT_SOURCE_DIR}/inc - ) - ############################################################ # Create a library ############################################################ @@ -18,6 +14,11 @@ set(library_SOURCES #Generate the static library from the library sources add_library(hello_library STATIC ${library_SOURCES}) +target_include_directories(hello_library + PUBLIC ${PROJECT_SOURCE_DIR}/inc +) + + ############################################################ # Create an executable ############################################################ @@ -32,5 +33,5 @@ add_executable(hello_binary ${binary_SOURCES}) # link the new hello_library target with the hello_binary target target_link_libraries( hello_binary - hello_library + PRIVATE hello_library ) diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index fc418e4..46cbcd5 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -46,6 +46,20 @@ add_library(hello_library STATIC ${library_SOURCES}) This will be used to create a static library with the name libhello_library.a with the sources from the +library_SOURCES+ variable. +## Populating Including Directories + +In this example, we include directories in the library using the +target_include_directories()+ function with the scope set to +PUBLIC+. This will cause the included directory used in the following places: + +* When compiling the library +* When compiling any additional target that links the library. + +The meaning of scopes are: + +* +PRIVATE_ - the directory is added to this target's include directories +* +INTERFACE+ - the directory is added to the include directores for any targets that link this library. +* +PUBLIC+ - As above, it is included int his library and also any targets that link this library. + + ## Linking a Library When creating an executable that will use your library you must tell the compiler @@ -56,7 +70,7 @@ about the library. This can be done using the +target_link_library()+ function. add_executable(hello_binary ${binary_SOURCES}) target_link_libraries( hello_binary - hello_library + PRIVATE hello_library ) ---- diff --git a/01-basic/D-shared-library/CMakeLists.txt b/01-basic/D-shared-library/CMakeLists.txt index 4fde1d0..b2a2973 100644 --- a/01-basic/D-shared-library/CMakeLists.txt +++ b/01-basic/D-shared-library/CMakeLists.txt @@ -1,11 +1,7 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) project(hello_library) -include_directories( - ${PROJECT_SOURCE_DIR}/inc - ) - ############################################################ # Create a library ############################################################ @@ -17,6 +13,11 @@ set(library_SOURCES #Generate the shared library from the library sources add_library(hello_library SHARED ${library_SOURCES}) +add_library(hello::library ALIAS hello_library) + +target_include_directories(hello_library + PUBLIC ${PROJECT_SOURCE_DIR}/inc +) ############################################################ # Create an executable @@ -32,5 +33,5 @@ add_executable(hello_binary ${binary_SOURCES}) # link the new hello_library target with the hello_binary target target_link_libraries( hello_binary - hello_library + PRIVATE hello::library ) \ No newline at end of file diff --git a/01-basic/D-shared-library/README.adoc b/01-basic/D-shared-library/README.adoc index d2382c2..1d8c8f0 100644 --- a/01-basic/D-shared-library/README.adoc +++ b/01-basic/D-shared-library/README.adoc @@ -6,7 +6,9 @@ toc::[] # Introduction -Shows a hello world example which first creates and links a shared library +Shows a hello world example which first creates and links a shared library. + +This also shows how to create an link:https://cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#alias-targets[alias target] The files in this tutorial are below: @@ -48,6 +50,17 @@ add_library(hello_library SHARED ${library_SOURCES}) This will be used to create a shared library with the name libhello_library.so with the sources from the +library_SOURCES+ variable. +## Alias Target + +As the name suggests an alias target is an alternative name for a target that can be used instead of the real target name in read-only contexts. + +[source,cmake] +---- +add_library(hello::library ALIAS hello_library) +---- + +As show below, this allows you to reference the target using the alias name when linking it against other targets. + ## Linking a Shared Library Linking a shared library is the same as linking a static library. When creating your @@ -58,12 +71,11 @@ executable use the the +target_link_library()+ function to point to your library add_executable(hello_binary ${binary_SOURCES}) target_link_libraries( hello_binary - hello_library + hello::library ) ---- -This tells CMake to link the hello_library against the hello_binary executable -during link time. +This tells CMake to link the hello_library against the hello_binary executable using the alias target name. An example of this being called by the linker is diff --git a/01-basic/E-installing/CMakeLists.txt b/01-basic/E-installing/CMakeLists.txt index 35d41d4..3e2606c 100644 --- a/01-basic/E-installing/CMakeLists.txt +++ b/01-basic/E-installing/CMakeLists.txt @@ -1,11 +1,7 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) project(cmake_examples_install) -include_directories( - ${PROJECT_SOURCE_DIR}/inc - ) - ############################################################ # Create a library ############################################################ @@ -18,6 +14,11 @@ set(library_SOURCES #Generate the shared library from the library sources add_library(cmake_examples_inst SHARED ${library_SOURCES}) + +target_include_directories(cmake_examples_inst + PUBLIC ${PROJECT_SOURCE_DIR}/inc +) + ############################################################ # Create an executable ############################################################ @@ -32,7 +33,7 @@ add_executable(cmake_examples_inst_bin ${binary_SOURCES}) # link the new hello_library target with the hello_binary target target_link_libraries( cmake_examples_inst_bin - cmake_examples_inst + PRIVATE cmake_examples_inst ) ############################################################ diff --git a/01-basic/F-build-type/CMakeLists.txt b/01-basic/F-build-type/CMakeLists.txt index 3aa4ef6..775ba4d 100644 --- a/01-basic/F-build-type/CMakeLists.txt +++ b/01-basic/F-build-type/CMakeLists.txt @@ -1,7 +1,7 @@ # Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) # Set a default build type if none was specified if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) diff --git a/01-basic/G-compile-flags/CMakeLists.txt b/01-basic/G-compile-flags/CMakeLists.txt index 4ff996b..c9bc48f 100644 --- a/01-basic/G-compile-flags/CMakeLists.txt +++ b/01-basic/G-compile-flags/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.6) +cmake_minimum_required(VERSION 3.0) # Set a default C++ compile flag set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE) @@ -8,3 +8,7 @@ project (compile_flags) # Add an executable add_executable(cmake_examples_compile_flags main.cpp) + +target_compile_defintions(cmake_examples_compile_flags + PRIVATE EX3 +) \ No newline at end of file diff --git a/01-basic/G-compile-flags/README.adoc b/01-basic/G-compile-flags/README.adoc index d194472..26c1652 100644 --- a/01-basic/G-compile-flags/README.adoc +++ b/01-basic/G-compile-flags/README.adoc @@ -26,7 +26,7 @@ $ tree ## Set C++ Flag -Similar to the build type a C++ compiler flag can be set using the following methods. +Similar to the build type a global C++ compiler flag can be set using the following methods. - Using a gui tool such as ccmake / cmake-gui @@ -60,6 +60,19 @@ are used to force this variable to be set in the CMakeCache.txt file. For more details, see https://cmake.org/cmake/help/v3.0/command/set.html[here] ==== +## Set Per-Target C++ Flags + +Once set the +CMAKE_C_FLAGS+ and +CMAKE_CXX_FLAGS+ will set a compler flag / definiton globally for all targets in this directory or any included sub-directories. To set a compiler definitons for a specific target you can use the +target_compile_defintions()+ link:https://cmake.org/cmake/help/v3.0/command/target_compile_definitions.html[function]. This will cause the compiler to add the definition +-DEX3+ when compiling the target. + +[source,cmake] +---- +target_compile_defintions(cmake_examples_compile_flags + PRIVATE EX3 +) +---- + +Depending on the scope this defintion may also be included in any targets that link this target. For compiler options you can also use the +target_compile_options()+ link:https://cmake.org/cmake/help/v3.0/command/target_compile_options.html[function]. + # Building the Example [source,bash]