From b81da6f68b1b5af82bd9e8e1751e1f01eed9c99f Mon Sep 17 00:00:00 2001 From: Thom Troy Date: Sun, 18 Mar 2018 17:23:57 +0000 Subject: [PATCH 1/4] Update to minimum CMake 3.5 And modernise some examples. --- .travis.yml | 4 +- 01-basic/A-hello-cmake/CMakeLists.txt | 4 +- 01-basic/A-hello-cmake/README.adoc | 2 +- 01-basic/B-hello-headers/CMakeLists.txt | 6 +-- 01-basic/B-hello-headers/README.adoc | 26 +++++++++- 01-basic/C-static-library/CMakeLists.txt | 22 ++++---- 01-basic/C-static-library/README.adoc | 49 ++++++++++++++---- .../include/{ => static}/Hello.h | 0 01-basic/C-static-library/src/Hello.cpp | 4 +- 01-basic/C-static-library/src/main.cpp | 4 +- 01-basic/D-shared-library/CMakeLists.txt | 22 ++++---- 01-basic/D-shared-library/README.adoc | 20 ++++---- .../include/{ => shared}/Hello.h | 0 01-basic/D-shared-library/src/Hello.cpp | 4 +- 01-basic/D-shared-library/src/main.cpp | 4 +- 01-basic/E-installing/CMakeLists.txt | 26 +++++----- 01-basic/E-installing/README.adoc | 15 +++++- .../include/{ => installing}/Hello.h | 0 01-basic/E-installing/src/Hello.cpp | 2 +- 01-basic/E-installing/src/main.cpp | 4 +- 01-basic/F-build-type/CMakeLists.txt | 2 +- 01-basic/G-compile-flags/CMakeLists.txt | 2 +- 01-basic/H-third-party-library/CMakeLists.txt | 10 +--- 01-basic/H-third-party-library/README.adoc | 50 +++++++++++++++---- .../I-compiling-with-clang/CMakeLists.txt | 2 +- 01-basic/J-building-with-ninja/CMakeLists.txt | 2 +- 01-basic/K-imported-targets/CMakeLists.txt | 2 +- 01-basic/K-imported-targets/README.adoc | 5 +- 02-sub-projects/A-basic/CMakeLists.txt | 2 +- 02-sub-projects/A-basic/README.adoc | 4 +- .../A-basic/subbinary/CMakeLists.txt | 6 +-- .../A-basic/sublibrary1/CMakeLists.txt | 7 +-- .../configure-files/CMakeLists.txt | 6 +-- 03-code-generation/protobuf/CMakeLists.txt | 2 +- .../clang-analyzer/CMakeLists.txt | 2 +- .../clang-analyzer/subproject1/CMakeLists.txt | 7 +-- .../clang-analyzer/subproject2/CMakeLists.txt | 7 +-- .../clang-format/CMakeLists.txt | 2 +- .../clang-format/subproject1/CMakeLists.txt | 7 +-- .../clang-format/subproject2/CMakeLists.txt | 7 +-- .../cppcheck-compile-commands/CMakeLists.txt | 2 +- .../subproject1/CMakeLists.txt | 7 +-- .../subproject2/CMakeLists.txt | 7 +-- 04-static-analysis/cppcheck/CMakeLists.txt | 2 +- 05-unit-testing/boost/CMakeLists.txt | 24 +++++---- 05-unit-testing/boost/README.adoc | 7 ++- 05-unit-testing/catch-vendored/CMakeLists.txt | 2 +- .../google-test-download/CMakeLists.txt | 2 +- 06-installer/deb/CMakeLists.txt | 26 +++------- README.adoc | 11 ++-- dockerfiles/README.adoc | 24 +++++---- dockerfiles/ubuntu16.04-cmake-3.10.3 | 49 ++++++++++++++++++ 52 files changed, 303 insertions(+), 212 deletions(-) rename 01-basic/C-static-library/include/{ => static}/Hello.h (100%) rename 01-basic/D-shared-library/include/{ => shared}/Hello.h (100%) rename 01-basic/E-installing/include/{ => installing}/Hello.h (100%) create mode 100644 dockerfiles/ubuntu16.04-cmake-3.10.3 diff --git a/.travis.yml b/.travis.yml index 5b84170..c862436 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,11 @@ sudo: required compiler: - gcc before_install: -- docker pull matrim/cmake-examples:3.4.3 - docker pull matrim/cmake-examples:3.5.1 +- docker pull matrim/cmake-examples:3.10.3 script: -- docker run --rm -v $PWD:/data/code -e DEV_UID=`id -u` -e DEV_GID=`id -g` -it matrim/cmake-examples:3.4.3 /data/code/test.sh - docker run --rm -v $PWD:/data/code -e DEV_UID=`id -u` -e DEV_GID=`id -g` -it matrim/cmake-examples:3.5.1 /data/code/test.sh +- docker run --rm -v $PWD:/data/code -e DEV_UID=`id -u` -e DEV_GID=`id -g` -it matrim/cmake-examples:3.10.3 /data/code/test.sh branches: except: - gh-pages diff --git a/01-basic/A-hello-cmake/CMakeLists.txt b/01-basic/A-hello-cmake/CMakeLists.txt index f43b8c7..0a43a8e 100644 --- a/01-basic/A-hello-cmake/CMakeLists.txt +++ b/01-basic/A-hello-cmake/CMakeLists.txt @@ -1,10 +1,10 @@ # Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (hello_cmake) # Add an executable -add_executable(hello_cmake main.cpp) \ No newline at end of file +add_executable(hello_cmake main.cpp) diff --git a/01-basic/A-hello-cmake/README.adoc b/01-basic/A-hello-cmake/README.adoc index cdb1ec0..9c5eb94 100644 --- a/01-basic/A-hello-cmake/README.adoc +++ b/01-basic/A-hello-cmake/README.adoc @@ -35,7 +35,7 @@ of CMake that is supported. [source,cmake] ---- -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) ---- diff --git a/01-basic/B-hello-headers/CMakeLists.txt b/01-basic/B-hello-headers/CMakeLists.txt index 236ad99..283a35a 100644 --- a/01-basic/B-hello-headers/CMakeLists.txt +++ b/01-basic/B-hello-headers/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 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (hello_headers) @@ -12,12 +12,12 @@ set(SOURCES src/main.cpp ) - # Add an executable with the above sources 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}/include + PRIVATE + ${PROJECT_SOURCE_DIR}/include ) diff --git a/01-basic/B-hello-headers/README.adoc b/01-basic/B-hello-headers/README.adoc index eb86fc5..1da9e91 100644 --- a/01-basic/B-hello-headers/README.adoc +++ b/01-basic/B-hello-headers/README.adoc @@ -83,6 +83,27 @@ file(GLOB SOURCES "src/*.cpp") ---- ==== + +[NOTE] +==== +An alternative to setting specific file names in the +SOURCES+ variable is +to use a GLOB command to find files using wildcard pattern matching. + +[source,cmake] +---- +file(GLOB SOURCES "src/*.cpp") +---- +==== + +[TIP] +==== +For modern CMake it is normally recommend to not use a variable for sources and to +directly declare the sources in the add_xxx function. + +This is particularly important for glob commands which may not always show you the +correct results if you add a new source file. +==== + ## Including Directories When you have different include folders, you can make your compiler aware of them using the @@ -91,8 +112,9 @@ When you have different include folders, you can make your compiler aware of the [source,cmake] ---- target_include_directories(target - PRIVATE ${PROJECT_SOURCE_DIR}/include - ) + PRIVATE + ${PROJECT_SOURCE_DIR}/include +) ---- 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] diff --git a/01-basic/C-static-library/CMakeLists.txt b/01-basic/C-static-library/CMakeLists.txt index 8cde8fc..3dc8210 100644 --- a/01-basic/C-static-library/CMakeLists.txt +++ b/01-basic/C-static-library/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) project(hello_library) @@ -6,16 +6,14 @@ project(hello_library) # Create a library ############################################################ -# Source files to be used in the library -set(library_SOURCES +#Generate the static library from the library sources +add_library(hello_library STATIC src/Hello.cpp ) -#Generate the static library from the library sources -add_library(hello_library STATIC ${library_SOURCES}) - target_include_directories(hello_library - PUBLIC ${PROJECT_SOURCE_DIR}/include + PUBLIC + ${PROJECT_SOURCE_DIR}/include ) @@ -23,15 +21,13 @@ target_include_directories(hello_library # Create an executable ############################################################ -# Source fles for the binary -set(binary_SOURCES +# Add an executable with the above sources +add_executable(hello_binary src/main.cpp ) -# Add an executable with the above sources -add_executable(hello_binary ${binary_SOURCES}) - # link the new hello_library target with the hello_binary target target_link_libraries( hello_binary - PRIVATE hello_library + PRIVATE + hello_library ) diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index 8754ce3..06b6f42 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -6,7 +6,9 @@ toc::[] # Introduction -Shows a hello world example which first creates and links a static library +Shows a hello world example which first creates and links a static library. This is a +simplified example showing the libray and binary in the same folder. Typically +these would be in sub-projects as described in section 02-sub-projects The files in this tutorial are below: @@ -15,14 +17,15 @@ $ tree . ├── CMakeLists.txt ├── include -│   └── Hello.h +│   └── static +│   └── Hello.h └── src ├── Hello.cpp └── main.cpp ``` * link:CMakeLists.txt[] - Contains the CMake commands you wish to run - * link:include/Hello.h[] - The header file to include + * link:include/static/Hello.h[] - The header file to include * link:src/Hello.cpp[] - A source file to compile * link:src/main.cpp[] - The source file with main @@ -36,15 +39,19 @@ This is called as follows: [source,cmake] ---- -set(library_SOURCES +add_library(hello_library STATIC src/Hello.cpp ) - -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. +the sources in the +add_library+ call. + +[NOTE] +==== +As mentioned in the prevoius example, we pass the source files directly to the ++add_library+ call, as recommended for modern CMake. +==== ## Populating Including Directories @@ -53,7 +60,8 @@ In this example, we include directories in the library using the +target_include [source,cmake] ---- target_include_directories(hello_library - PUBLIC ${PROJECT_SOURCE_DIR}/include + PUBLIC + ${PROJECT_SOURCE_DIR}/include ) ---- @@ -69,6 +77,24 @@ The meaning of scopes are: * +PUBLIC+ - As above, it is included int his library and also any targets that link this library. +[TIP] +==== +For public headers it is often a good idea to have your include folder be "namespaced" +with sub-directories. + +The directory passed to +target_include_directories+ will be the root of your +include directory and your C++ files should include the path from there to your header. + +For this example you can see that we do it as follows: +[source,cpp] +---- +#include "static/Hello.h" +---- + +Using this method means that there is less chance of header filename clashes when +you use multiple libraries in your project. +==== + ## Linking a Library When creating an executable that will use your library you must tell the compiler @@ -76,10 +102,13 @@ about the library. This can be done using the +target_link_library()+ function. [source,cmake] ---- -add_executable(hello_binary ${binary_SOURCES}) +add_executable(hello_binary + src/main.cpp +) target_link_libraries( hello_binary - PRIVATE hello_library + PRIVATE + hello_library ) ---- diff --git a/01-basic/C-static-library/include/Hello.h b/01-basic/C-static-library/include/static/Hello.h similarity index 100% rename from 01-basic/C-static-library/include/Hello.h rename to 01-basic/C-static-library/include/static/Hello.h diff --git a/01-basic/C-static-library/src/Hello.cpp b/01-basic/C-static-library/src/Hello.cpp index eee8667..3b15c03 100644 --- a/01-basic/C-static-library/src/Hello.cpp +++ b/01-basic/C-static-library/src/Hello.cpp @@ -1,8 +1,8 @@ #include -#include "Hello.h" +#include "static/Hello.h" void Hello::print() { std::cout << "Hello Static Library!" << std::endl; -} \ No newline at end of file +} diff --git a/01-basic/C-static-library/src/main.cpp b/01-basic/C-static-library/src/main.cpp index 62a1520..bec6b3e 100644 --- a/01-basic/C-static-library/src/main.cpp +++ b/01-basic/C-static-library/src/main.cpp @@ -1,8 +1,8 @@ -#include "Hello.h" +#include "static/Hello.h" int main(int argc, char *argv[]) { Hello hi; hi.print(); return 0; -} \ No newline at end of file +} diff --git a/01-basic/D-shared-library/CMakeLists.txt b/01-basic/D-shared-library/CMakeLists.txt index 50343a5..49bf0d3 100644 --- a/01-basic/D-shared-library/CMakeLists.txt +++ b/01-basic/D-shared-library/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) project(hello_library) @@ -6,32 +6,28 @@ project(hello_library) # Create a library ############################################################ -# Source files to be used in the library -set(library_SOURCES +#Generate the shared library from the library sources +add_library(hello_library SHARED src/Hello.cpp ) - -#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}/include + PUBLIC + ${PROJECT_SOURCE_DIR}/include ) ############################################################ # Create an executable ############################################################ -# Source fles for the binary -set(binary_SOURCES +# Add an executable with the above sources +add_executable(hello_binary src/main.cpp ) -# Add an executable with the above sources -add_executable(hello_binary ${binary_SOURCES}) - # link the new hello_library target with the hello_binary target target_link_libraries( hello_binary - PRIVATE hello::library + PRIVATE + hello::library ) diff --git a/01-basic/D-shared-library/README.adoc b/01-basic/D-shared-library/README.adoc index fc6a60b..e9f30c7 100644 --- a/01-basic/D-shared-library/README.adoc +++ b/01-basic/D-shared-library/README.adoc @@ -17,14 +17,15 @@ $ tree . ├── CMakeLists.txt ├── include -│   └── Hello.h +│   └── shared +│   └── Hello.h └── src ├── Hello.cpp └── main.cpp ``` * link:CMakeLists.txt[] - Contains the CMake commands you wish to run - * link:include/Hello.h[] - The header file to include + * link:include/shared/Hello.h[] - The header file to include * link:src/Hello.cpp[] - A source file to compile * link:src/main.cpp[] - The source file with main @@ -39,15 +40,13 @@ This is called as follows: [source,cmake] ---- -set(library_SOURCES +add_library(hello_library SHARED src/Hello.cpp ) - -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. +the sources passed to teh +add_library()+ function. ## Alias Target @@ -67,10 +66,13 @@ executable use the the +target_link_library()+ function to point to your library [source,cmake] ---- -add_executable(hello_binary ${binary_SOURCES}) +add_executable(hello_binary + src/main.cpp +) -target_link_libraries( hello_binary - hello::library +target_link_libraries(hello_binary + PRIVATE + hello::library ) ---- diff --git a/01-basic/D-shared-library/include/Hello.h b/01-basic/D-shared-library/include/shared/Hello.h similarity index 100% rename from 01-basic/D-shared-library/include/Hello.h rename to 01-basic/D-shared-library/include/shared/Hello.h diff --git a/01-basic/D-shared-library/src/Hello.cpp b/01-basic/D-shared-library/src/Hello.cpp index 083c482..865e58f 100644 --- a/01-basic/D-shared-library/src/Hello.cpp +++ b/01-basic/D-shared-library/src/Hello.cpp @@ -1,8 +1,8 @@ #include -#include "Hello.h" +#include "shared/Hello.h" void Hello::print() { std::cout << "Hello Shared Library!" << std::endl; -} \ No newline at end of file +} diff --git a/01-basic/D-shared-library/src/main.cpp b/01-basic/D-shared-library/src/main.cpp index 62a1520..a57a9e9 100644 --- a/01-basic/D-shared-library/src/main.cpp +++ b/01-basic/D-shared-library/src/main.cpp @@ -1,8 +1,8 @@ -#include "Hello.h" +#include "shared/Hello.h" int main(int argc, char *argv[]) { Hello hi; hi.print(); return 0; -} \ No newline at end of file +} diff --git a/01-basic/E-installing/CMakeLists.txt b/01-basic/E-installing/CMakeLists.txt index f73c73f..5d03c5c 100644 --- a/01-basic/E-installing/CMakeLists.txt +++ b/01-basic/E-installing/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) project(cmake_examples_install) @@ -6,34 +6,29 @@ project(cmake_examples_install) # Create a library ############################################################ -# Source files to be used in the library -set(library_SOURCES +#Generate the shared library from the library sources +add_library(cmake_examples_inst SHARED src/Hello.cpp ) -#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}/include + PUBLIC + ${PROJECT_SOURCE_DIR}/include ) ############################################################ # Create an executable ############################################################ -# Source fles for the binary -set(binary_SOURCES +# Add an executable with the above sources +add_executable(cmake_examples_inst_bin src/main.cpp ) -# Add an executable with the above sources -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 - PRIVATE cmake_examples_inst + PRIVATE + cmake_examples_inst ) ############################################################ @@ -49,6 +44,9 @@ install (TARGETS cmake_examples_inst_bin install (TARGETS cmake_examples_inst LIBRARY DESTINATION lib) +# Header files +install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ + DESTINATION include) # Config install (FILES cmake-examples.conf diff --git a/01-basic/E-installing/README.adoc b/01-basic/E-installing/README.adoc index f4d3f54..a692bd2 100644 --- a/01-basic/E-installing/README.adoc +++ b/01-basic/E-installing/README.adoc @@ -17,7 +17,8 @@ $ tree ├── cmake-examples.conf ├── CMakeLists.txt ├── include -│   └── Hello.h +│   └── installing +│   └── Hello.h ├── README.adoc └── src ├── Hello.cpp @@ -26,7 +27,7 @@ $ tree * link:CMakeLists.txt[] - Contains the CMake commands you wish to run * link:cmake-examples.conf[] - An example configuration file - * link:include/Hello.h[] - The header file to include + * link:include/installing/Hello.h[] - The header file to include * link:src/Hello.cpp[] - A source file to compile * link:src/main.cpp[] - The source file with main @@ -72,6 +73,16 @@ install (TARGETS cmake_examples_inst ---- ==== + +[source,cmake] +---- +install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ + DESTINATION include) +---- + +Install the header files for developing against the +cmake_examples_inst+ library +into the +${CMAKE_INSTALL_PREFIX}/include+ directory. + [source,cmake] ---- install (FILES cmake-examples.conf diff --git a/01-basic/E-installing/include/Hello.h b/01-basic/E-installing/include/installing/Hello.h similarity index 100% rename from 01-basic/E-installing/include/Hello.h rename to 01-basic/E-installing/include/installing/Hello.h diff --git a/01-basic/E-installing/src/Hello.cpp b/01-basic/E-installing/src/Hello.cpp index 75c152d..13350ed 100644 --- a/01-basic/E-installing/src/Hello.cpp +++ b/01-basic/E-installing/src/Hello.cpp @@ -1,6 +1,6 @@ #include -#include "Hello.h" +#include "installing/Hello.h" void Hello::print() { diff --git a/01-basic/E-installing/src/main.cpp b/01-basic/E-installing/src/main.cpp index 62a1520..960f3a0 100644 --- a/01-basic/E-installing/src/main.cpp +++ b/01-basic/E-installing/src/main.cpp @@ -1,8 +1,8 @@ -#include "Hello.h" +#include "installing/Hello.h" int main(int argc, char *argv[]) { Hello hi; hi.print(); return 0; -} \ No newline at end of file +} diff --git a/01-basic/F-build-type/CMakeLists.txt b/01-basic/F-build-type/CMakeLists.txt index 775ba4d..cdbfd18 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 3.0) +cmake_minimum_required(VERSION 3.5) # 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 2ddca7d..56e2261 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 3.0) +cmake_minimum_required(VERSION 3.5) # Set a default C++ compile flag set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE) diff --git a/01-basic/H-third-party-library/CMakeLists.txt b/01-basic/H-third-party-library/CMakeLists.txt index 2bb5a89..85128ef 100644 --- a/01-basic/H-third-party-library/CMakeLists.txt +++ b/01-basic/H-third-party-library/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (third_party_include) @@ -17,14 +17,8 @@ endif() # Add an executable add_executable(third_party_include main.cpp) -# Include the boost headers -target_include_directories( third_party_include - PRIVATE ${Boost_INCLUDE_DIRS} -) - # link against the boost libraries target_link_libraries( third_party_include PRIVATE - ${Boost_SYSTEM_LIBRARY} - ${Boost_FILESYSTEM_LIBRARY} + Boost::filesystem ) diff --git a/01-basic/H-third-party-library/README.adoc b/01-basic/H-third-party-library/README.adoc index d5a8914..3442a4b 100644 --- a/01-basic/H-third-party-library/README.adoc +++ b/01-basic/H-third-party-library/README.adoc @@ -85,33 +85,63 @@ variable, these are package specific and are typically documented at the top of The variables exported in this example include: * `Boost_INCLUDE_DIRS` - The path to the boost header files. - * `Boost_SYSTEM_LIBRARY` - The path to the boost system library. - * `Boost_FILESYSTEM_LIBRARY` - The path to the boost filesystem library. In some cases you can also check these variables by examining the cache using ccmake or cmake-gui. -## Alias variables +## Alias / Imported targets -Some modern CMake libraries export +ALIAS+ targets in their module files. For example, starting from v3.5+ of CMake, the +Most modern CMake libraries link:https://cmake.org/cmake/help/v3.6/prop_tgt/IMPORTED.html#prop_tgt:IMPORTED[export] +ALIAS+ targets in their module files. +The benefit of imported targets are that they can also populate include directories and linked libraries. + +For example, starting from v3.5+ of CMake, the Boost module supports this. Similar to using your own ALIAS target for libraires, an +ALIAS+ in a module can make referencing found targets eaiser. -In the case of Boost, you could replace the following from this example: +In the case of Boost, all targets are exported using the `Boost::` identifier and then the name +of the subsystem. For example you can use: - * `Boost_INCLUDE_DIRS` with `Boost::boost` for header only libraries - * `Boost_FILESYSTEM_LIBRARY` with `Boost::filesystem` - * `Boost_SYSTEM_LIBRARY` with `Boost::system`. If you include `Boost::filesystem` it automatically includes `Boost::system` + * `Boost::boost` for header only libraries + * `Boost::system` for the boost system library. + * `Boost::filesystem` for filesystem library. -Using the new alias sytem, to replicate this example you only have to link the following: +As with your own targets, these targets include their dependencies, so linking against +`Boost::filesystem` will automatically add `Boost::boost` and `Boost::system` dependencies. + +To link against an imported target you can use the following: [source,cmake] ---- target_link_libraries( third_party_include PRIVATE - Boost::filesystem + Boost::filesystem ) ---- +## Non-alias targets + +While most modern libraries use imported targets, not all modules have been updated. In the +case where a library hasn't been updated you will often find the following variables available: + + * xxx_INCLUDE_DIRS - A varialble pointing to the include directory for the library. + * xxx_LIBRARY - A variable pointing to the library path. + +These can then be added to your +target_include_directories+ and +target_link_libraries+ as: + +[source,cmake] +---- +# Include the boost headers +target_include_directories( third_party_include + PRIVATE ${Boost_INCLUDE_DIRS} +) + +# link against the boost libraries +target_link_libraries( third_party_include + PRIVATE + ${Boost_SYSTEM_LIBRARY} + ${Boost_FILESYSTEM_LIBRARY} +) +---- + # Building the Example [source,bash] diff --git a/01-basic/I-compiling-with-clang/CMakeLists.txt b/01-basic/I-compiling-with-clang/CMakeLists.txt index 6471847..0a43a8e 100644 --- a/01-basic/I-compiling-with-clang/CMakeLists.txt +++ b/01-basic/I-compiling-with-clang/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 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (hello_cmake) diff --git a/01-basic/J-building-with-ninja/CMakeLists.txt b/01-basic/J-building-with-ninja/CMakeLists.txt index 6471847..0a43a8e 100644 --- a/01-basic/J-building-with-ninja/CMakeLists.txt +++ b/01-basic/J-building-with-ninja/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 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (hello_cmake) diff --git a/01-basic/K-imported-targets/CMakeLists.txt b/01-basic/K-imported-targets/CMakeLists.txt index 0e23d07..46eac96 100644 --- a/01-basic/K-imported-targets/CMakeLists.txt +++ b/01-basic/K-imported-targets/CMakeLists.txt @@ -20,5 +20,5 @@ add_executable(imported_targets main.cpp) # link against the boost libraries target_link_libraries( imported_targets PRIVATE - Boost::filesystem + Boost::filesystem ) diff --git a/01-basic/K-imported-targets/README.adoc b/01-basic/K-imported-targets/README.adoc index 84bf40f..4c8b434 100644 --- a/01-basic/K-imported-targets/README.adoc +++ b/01-basic/K-imported-targets/README.adoc @@ -32,8 +32,7 @@ This example requires the following: ## Imported Target -Imported targets are read-only targets that are exported by FindXXX modules. The benefit of imported -targets are that they can also populate include directories and linked libraries. +Imported targets are read-only targets that are exported by FindXXX modules. To include boost filesystem you can do the following: @@ -41,7 +40,7 @@ To include boost filesystem you can do the following: ---- target_link_libraries( imported_targets PRIVATE - Boost::filesystem + Boost::filesystem ) ---- diff --git a/02-sub-projects/A-basic/CMakeLists.txt b/02-sub-projects/A-basic/CMakeLists.txt index 524caf2..7e2a6c2 100644 --- a/02-sub-projects/A-basic/CMakeLists.txt +++ b/02-sub-projects/A-basic/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.0) +cmake_minimum_required (VERSION 3.5) project(subprojects) diff --git a/02-sub-projects/A-basic/README.adoc b/02-sub-projects/A-basic/README.adoc index 3031891..c13cf9a 100644 --- a/02-sub-projects/A-basic/README.adoc +++ b/02-sub-projects/A-basic/README.adoc @@ -145,7 +145,7 @@ is added as a dependency. ---- target_link_libraries(subbinary PUBLIC - sublibrary1 + sublibrary1 ) ---- @@ -164,7 +164,7 @@ To reference the alias, just it as follows: [source,cmake] ---- target_link_libraries(subbinary - sublibrary1 + sub::lib2 ) ---- diff --git a/02-sub-projects/A-basic/subbinary/CMakeLists.txt b/02-sub-projects/A-basic/subbinary/CMakeLists.txt index 1269b9b..7f74e5b 100644 --- a/02-sub-projects/A-basic/subbinary/CMakeLists.txt +++ b/02-sub-projects/A-basic/subbinary/CMakeLists.txt @@ -1,11 +1,7 @@ project(subbinary) -set(SOURCES - main.cpp -) - # Create the executable -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main.cpp) # Link the static library from subproject1 using it's alias sub::lib1 # Link the header only library from subproject2 using it's alias sub::lib2 diff --git a/02-sub-projects/A-basic/sublibrary1/CMakeLists.txt b/02-sub-projects/A-basic/sublibrary1/CMakeLists.txt index 82b06d9..97dcb59 100644 --- a/02-sub-projects/A-basic/sublibrary1/CMakeLists.txt +++ b/02-sub-projects/A-basic/sublibrary1/CMakeLists.txt @@ -1,13 +1,8 @@ # Set the project name project (sublibrary1) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - src/sublib1.cpp -) - # Add a library with the above sources -add_library(${PROJECT_NAME} ${SOURCES}) +add_library(${PROJECT_NAME} src/sublib1.cpp) add_library(sub::lib1 ALIAS ${PROJECT_NAME}) target_include_directories( ${PROJECT_NAME} diff --git a/03-code-generation/configure-files/CMakeLists.txt b/03-code-generation/configure-files/CMakeLists.txt index df324a7..1dfaf80 100644 --- a/03-code-generation/configure-files/CMakeLists.txt +++ b/03-code-generation/configure-files/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (cf_example) @@ -20,10 +20,10 @@ configure_file(path.h.in ${PROJECT_BINARY_DIR}/path.h @ONLY) # Add an executable add_executable(cf_example main.cpp - ) +) # include the directory with the new files target_include_directories( cf_example PUBLIC - ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR} ) diff --git a/03-code-generation/protobuf/CMakeLists.txt b/03-code-generation/protobuf/CMakeLists.txt index 34b8577..8fd31db 100644 --- a/03-code-generation/protobuf/CMakeLists.txt +++ b/03-code-generation/protobuf/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (protobuf_example) diff --git a/04-static-analysis/clang-analyzer/CMakeLists.txt b/04-static-analysis/clang-analyzer/CMakeLists.txt index e953049..0a94a19 100644 --- a/04-static-analysis/clang-analyzer/CMakeLists.txt +++ b/04-static-analysis/clang-analyzer/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.0) +cmake_minimum_required (VERSION 3.5) project(cppcheck_analysis) diff --git a/04-static-analysis/clang-analyzer/subproject1/CMakeLists.txt b/04-static-analysis/clang-analyzer/subproject1/CMakeLists.txt index 2554853..e846c31 100644 --- a/04-static-analysis/clang-analyzer/subproject1/CMakeLists.txt +++ b/04-static-analysis/clang-analyzer/subproject1/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject1) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main1.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main1.cpp) diff --git a/04-static-analysis/clang-analyzer/subproject2/CMakeLists.txt b/04-static-analysis/clang-analyzer/subproject2/CMakeLists.txt index bb05ae3..0c882a4 100644 --- a/04-static-analysis/clang-analyzer/subproject2/CMakeLists.txt +++ b/04-static-analysis/clang-analyzer/subproject2/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject2) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main2.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main2.cpp) diff --git a/04-static-analysis/clang-format/CMakeLists.txt b/04-static-analysis/clang-format/CMakeLists.txt index a83015d..b6c4674 100644 --- a/04-static-analysis/clang-format/CMakeLists.txt +++ b/04-static-analysis/clang-format/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.0) +cmake_minimum_required (VERSION 3.5) project(cppcheck_analysis) diff --git a/04-static-analysis/clang-format/subproject1/CMakeLists.txt b/04-static-analysis/clang-format/subproject1/CMakeLists.txt index 2554853..e846c31 100644 --- a/04-static-analysis/clang-format/subproject1/CMakeLists.txt +++ b/04-static-analysis/clang-format/subproject1/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject1) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main1.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main1.cpp) diff --git a/04-static-analysis/clang-format/subproject2/CMakeLists.txt b/04-static-analysis/clang-format/subproject2/CMakeLists.txt index bb05ae3..0c882a4 100644 --- a/04-static-analysis/clang-format/subproject2/CMakeLists.txt +++ b/04-static-analysis/clang-format/subproject2/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject2) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main2.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main2.cpp) diff --git a/04-static-analysis/cppcheck-compile-commands/CMakeLists.txt b/04-static-analysis/cppcheck-compile-commands/CMakeLists.txt index 591aa15..f1d572a 100644 --- a/04-static-analysis/cppcheck-compile-commands/CMakeLists.txt +++ b/04-static-analysis/cppcheck-compile-commands/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.0) +cmake_minimum_required (VERSION 3.5) project(cppcheck_analysis) diff --git a/04-static-analysis/cppcheck-compile-commands/subproject1/CMakeLists.txt b/04-static-analysis/cppcheck-compile-commands/subproject1/CMakeLists.txt index 2554853..e846c31 100644 --- a/04-static-analysis/cppcheck-compile-commands/subproject1/CMakeLists.txt +++ b/04-static-analysis/cppcheck-compile-commands/subproject1/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject1) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main1.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main1.cpp) diff --git a/04-static-analysis/cppcheck-compile-commands/subproject2/CMakeLists.txt b/04-static-analysis/cppcheck-compile-commands/subproject2/CMakeLists.txt index bb05ae3..0c882a4 100644 --- a/04-static-analysis/cppcheck-compile-commands/subproject2/CMakeLists.txt +++ b/04-static-analysis/cppcheck-compile-commands/subproject2/CMakeLists.txt @@ -1,10 +1,5 @@ # Set the project name project (subproject2) -# Create a sources variable with a link to all cpp files to compile -set(SOURCES - main2.cpp -) - # Add an executable with the above sources -add_executable(${PROJECT_NAME} ${SOURCES}) +add_executable(${PROJECT_NAME} main2.cpp) diff --git a/04-static-analysis/cppcheck/CMakeLists.txt b/04-static-analysis/cppcheck/CMakeLists.txt index aae99ce..b76205b 100644 --- a/04-static-analysis/cppcheck/CMakeLists.txt +++ b/04-static-analysis/cppcheck/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required (VERSION 3.0) +cmake_minimum_required (VERSION 3.5) project(cppcheck_analysis) diff --git a/05-unit-testing/boost/CMakeLists.txt b/05-unit-testing/boost/CMakeLists.txt index f0bd1e9..58c1b66 100644 --- a/05-unit-testing/boost/CMakeLists.txt +++ b/05-unit-testing/boost/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (boost_unit_test) @@ -7,19 +7,21 @@ project (boost_unit_test) # find a boost install with the libraries unit_test_framework find_package(Boost 1.46.1 REQUIRED COMPONENTS unit_test_framework) -set (SOURCES +# Add an library for the example classes +add_library(example_boost_unit_test Reverse.cpp Palindrome.cpp - ) +) -include_directories( - ${Boost_INCLUDE_DIRS} - ${CMAKE_CURRENT_SOURCE_DIR} - ) - -# Add an library for the example classes -add_library(example_boost_unit_test ${SOURCES}) +target_include_directories(example_boost_unit_test + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} +) +target_link_libraries(example_boost_unit_test + PUBLIC + Boost::boost +) ############################################# # Unit tests @@ -32,7 +34,7 @@ add_executable(unit_tests unit_tests.cpp) target_link_libraries(unit_tests example_boost_unit_test - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} + Boost::unit_test_framework ) target_compile_definitions(unit_tests diff --git a/05-unit-testing/boost/README.adoc b/05-unit-testing/boost/README.adoc index 3f253ad..93fbd0c 100644 --- a/05-unit-testing/boost/README.adoc +++ b/05-unit-testing/boost/README.adoc @@ -59,10 +59,13 @@ add_executable(unit_tests unit_tests.cpp) target_link_libraries(unit_tests example_boost_unit_test - ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} + Boost::unit_test_framework ) -add_definitions (-DBOOST_TEST_DYN_LINK) +target_compile_definitions(unit_tests + PRIVATE + BOOST_TEST_DYN_LINK +) ---- In the above code, a unit test binary is added, which links against the boost unit-test-framework diff --git a/05-unit-testing/catch-vendored/CMakeLists.txt b/05-unit-testing/catch-vendored/CMakeLists.txt index 25f5592..f4a1d7f 100644 --- a/05-unit-testing/catch-vendored/CMakeLists.txt +++ b/05-unit-testing/catch-vendored/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (catch_unit_test) diff --git a/05-unit-testing/google-test-download/CMakeLists.txt b/05-unit-testing/google-test-download/CMakeLists.txt index bef7df7..f433b17 100644 --- a/05-unit-testing/google-test-download/CMakeLists.txt +++ b/05-unit-testing/google-test-download/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) # Set the project name project (google_test_example) diff --git a/06-installer/deb/CMakeLists.txt b/06-installer/deb/CMakeLists.txt index 373de63..a42bf23 100644 --- a/06-installer/deb/CMakeLists.txt +++ b/06-installer/deb/CMakeLists.txt @@ -1,11 +1,7 @@ -cmake_minimum_required(VERSION 3.0) +cmake_minimum_required(VERSION 3.5) project(cmake_examples_deb) -include_directories( - ${PROJECT_SOURCE_DIR}/include - ) - # set a project version set (deb_example_VERSION_MAJOR 0) set (deb_example_VERSION_MINOR 2) @@ -17,30 +13,24 @@ set (deb_example_VERSION "${deb_example_VERSION_MAJOR}.${deb_example_VERSION_MIN # Create a library ############################################################ -# Source files to be used in the library -set(library_SOURCES - src/Hello.cpp -) - #Generate the shared library from the library sources -add_library(cmake_examples_deb SHARED ${library_SOURCES}) +add_library(cmake_examples_deb SHARED src/Hello.cpp) +target_include_directories(cmake_examples_deb + PUBLIC + ${PROJECT_SOURCE_DIR}/include +) ############################################################ # Create an executable ############################################################ -# Source fles for the binary -set(binary_SOURCES - src/main.cpp -) - # Add an executable with the above sources -add_executable(cmake_examples_deb_bin ${binary_SOURCES}) +add_executable(cmake_examples_deb_bin src/main.cpp) # link the new hello_library target with the hello_binary target target_link_libraries( cmake_examples_deb_bin PUBLIC - cmake_examples_deb + cmake_examples_deb ) ############################################################ diff --git a/README.adoc b/README.adoc index f0bebb5..1481e18 100644 --- a/README.adoc +++ b/README.adoc @@ -16,9 +16,12 @@ when exploring it's usage for various projects. The examples are laid out in a t The first examples are very basic and slowly increase in complexity drawing on previous examples to show more complex use cases. -These examples have been tested on Ubuntu 14.04 but should work under any Linux system that supports CMake. +These examples have been tested on Ubuntu 16.04 but should work under any Linux system that supports CMake v3.5+. -This branch works with the CMake version 3.x onwards. For examples that use CMake version 2.x see the branch link:https://github.com/ttroy50/cmake-examples/tree/v2-style-includes[v2-style-includes]. +This branch works with the CMake version 3.5 onwards. + +* For examples that use CMake version 2.x see the branch link:https://github.com/ttroy50/cmake-examples/tree/v2-style-includes[v2-style-includes]. +* For examples that use CMake version 3.0 see the branch link:https://github.com/ttroy50/cmake-examples/tree/v3.0-minimum[v3.0-minimum] image:https://travis-ci.org/ttroy50/cmake-examples.svg?branch=master["Build Status", link="https://travis-ci.org/ttroy50/cmake-examples"] @@ -26,7 +29,7 @@ image:https://travis-ci.org/ttroy50/cmake-examples.svg?branch=master["Build Stat The basic requirements for most examples are: -* CMake v3.x +* CMake v3.5+ * A c++ compiler (defaults to gcc) * make @@ -71,7 +74,7 @@ To build the full set of cmake-examples test cases you can run: [source,bash] ---- -docker run -it matrim/cmake-examples:3.4.3 +docker run -it matrim/cmake-examples:3.5.1 git clone https://github.com/ttroy50/cmake-examples.git cd cmake-examples ./test.sh diff --git a/dockerfiles/README.adoc b/dockerfiles/README.adoc index 17aaed6..aa3e57b 100644 --- a/dockerfiles/README.adoc +++ b/dockerfiles/README.adoc @@ -22,7 +22,7 @@ For example: [source,bash] ---- -$ docker build --rm -f ubuntu14.04-cmake-3.4.3 -t matrim/cmake-examples:3.4.3 . +$ docker build --rm -f ubuntu14.04-cmake-3.5.1 -t matrim/cmake-examples:3.5.1 . ---- In this example the tag is created as follows @@ -35,7 +35,17 @@ This will tag the container as belong to my repositry with the label of the cont I have pre-build images and pushed them to the https://hub.docker.com[docker hub] in the repository https://hub.docker.com/r/matrim/cmake-examples/[matrim/cmake-examples]. -The images available include the following versions of cmake: +The current usable images include the following versions of cmake: + +* Ubuntu 16.04 with CMake 3.5.1 + + $ docker pull docker pull matrim/cmake-examples:3.5.1 + +* Ubuntu 16.04 with CMake 3.10.3 + + $ docker pull docker pull matrim/cmake-examples:3.10.3 + +Some old images which work with older version of the repository include: * Ubuntu 14.04 with CMake 2.8.12.2 @@ -45,10 +55,6 @@ The images available include the following versions of cmake: $ docker pull docker pull matrim/cmake-examples:3.4.3 -* Ubuntu 16.04 with CMake 3.5.1 - - $ docker pull docker pull matrim/cmake-examples:3.5.1 - # Running When run the images will automatically create a non root user called devuser, with a default command to launch a bash shell in the users home directory. @@ -59,7 +65,7 @@ For example [source,bash] ---- -docker run -e DEV_UID=`id -u` -e DEV_GID=`id -u` -it matrim/cmake-examples:3.4.3 +docker run -e DEV_UID=`id -u` -e DEV_GID=`id -u` -it matrim/cmake-examples:3.5.1 ---- @@ -67,7 +73,7 @@ To build the full set of cmake-examples test cases you can run: [source,bash] ---- -docker run -it matrim/cmake-examples:3.4.3 +docker run -it matrim/cmake-examples:3.5.1 git clone https://github.com/ttroy50/cmake-examples.git cd cmake-examples ./test.sh @@ -80,5 +86,5 @@ Below is an example of loading a volume and automatically running all cmake-exam [source,bash] ---- -docker run --rm -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.4.3 /data/code/test.sh +docker run --rm -e DEV_UID=`id -u` -e DEV_GID=`id -u` -v /checkout/directory:/data/code -it matrim/cmake-examples:3.5.1 /data/code/test.sh ---- diff --git a/dockerfiles/ubuntu16.04-cmake-3.10.3 b/dockerfiles/ubuntu16.04-cmake-3.10.3 new file mode 100644 index 0000000..f094b95 --- /dev/null +++ b/dockerfiles/ubuntu16.04-cmake-3.10.3 @@ -0,0 +1,49 @@ +# Container for building and testing cmake-examples with CMake 3.10.3 +FROM ubuntu:16.04 +MAINTAINER Thom Troy + +RUN apt-get update && apt-get install -y build-essential \ + sudo \ + cmake \ + libboost-all-dev \ + libprotobuf-dev \ + protobuf-compiler \ + clang-3.6 \ + clang-format-3.6 \ + ninja-build \ + wget \ + git \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* + +RUN cd /usr/local/src \ + && wget https://cmake.org/files/v3.10/cmake-3.10.3.tar.gz \ + && tar xvf cmake-3.10.3.tar.gz \ + && cd cmake-3.10.3 \ + && ./bootstrap \ + && make \ + && make install \ + && cd .. \ + && rm -rf cmake* + +# cppcheck +RUN cd /usr/local/src \ + && wget https://github.com/danmar/cppcheck/archive/1.79.tar.gz \ + && tar xvf 1.79.tar.gz \ + && cd cppcheck-1.79 \ + && mkdir build \ + && cd build \ + && cmake .. \ + && make install \ + && cd ../.. && rm -rf cppcheck* + +RUN cd /usr/local/src \ + && wget https://github.com/tianon/gosu/releases/download/1.10/gosu-amd64 \ + && mv gosu-amd64 /usr/local/bin/gosu \ + && chmod +x /usr/local/bin/gosu + +ADD setup.sh /setup.sh +RUN chmod +x /setup.sh + +CMD ["/bin/bash"] +ENTRYPOINT ["/setup.sh"] From 33b3ea5683c2ecd49420651bf3fa1e7968d09139 Mon Sep 17 00:00:00 2001 From: Thom Troy Date: Sun, 18 Mar 2018 17:38:09 +0000 Subject: [PATCH 2/4] fix some typos --- 01-basic/B-hello-headers/README.adoc | 15 ++------------- 01-basic/C-static-library/README.adoc | 4 ++-- README.adoc | 2 +- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/01-basic/B-hello-headers/README.adoc b/01-basic/B-hello-headers/README.adoc index 1da9e91..b96cb52 100644 --- a/01-basic/B-hello-headers/README.adoc +++ b/01-basic/B-hello-headers/README.adoc @@ -84,21 +84,10 @@ file(GLOB SOURCES "src/*.cpp") ==== -[NOTE] -==== -An alternative to setting specific file names in the +SOURCES+ variable is -to use a GLOB command to find files using wildcard pattern matching. - -[source,cmake] ----- -file(GLOB SOURCES "src/*.cpp") ----- -==== - [TIP] ==== -For modern CMake it is normally recommend to not use a variable for sources and to -directly declare the sources in the add_xxx function. +For modern CMake it is NOT recommended to use a variable for sources. Insead it is +typical to directly declare the sources in the add_xxx function. This is particularly important for glob commands which may not always show you the correct results if you add a new source file. diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index 06b6f42..ec4ce71 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -7,8 +7,8 @@ toc::[] # Introduction Shows a hello world example which first creates and links a static library. This is a -simplified example showing the libray and binary in the same folder. Typically -these would be in sub-projects as described in section 02-sub-projects +simplified example showing the library and binary in the same folder. Typically +these would be in sub-projects as described in section link:../../02-sub-projects[02-sub-projects] The files in this tutorial are below: diff --git a/README.adoc b/README.adoc index 1481e18..4f1f5d0 100644 --- a/README.adoc +++ b/README.adoc @@ -11,7 +11,7 @@ https://cmake.org/[CMake] is a cross-platform open-source meta-build system whic can build, test and package software. It can be used to support multiple native build environments including make, Apple's xcode and Microsoft Visual Studio. -This repository includes some example CMake configurations which I have picked up +This repository includes some example modern CMake configurations which I have picked up when exploring it's usage for various projects. The examples are laid out in a tutorial like format. The first examples are very basic and slowly increase in complexity drawing on previous examples to show more complex use cases. From 3b1e2aaed6714520d7cfd92ab7b24bf1bb505ddc Mon Sep 17 00:00:00 2001 From: Thom Troy Date: Sun, 18 Mar 2018 17:39:14 +0000 Subject: [PATCH 3/4] fix missing + --- 01-basic/C-static-library/README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index ec4ce71..3adbf85 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -72,7 +72,7 @@ This will cause the included directory used in the following places: The meaning of scopes are: -* +PRIVATE_ - the directory is added to this target's include directories +* +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. From 58b9147298d343b9f74dc0f2036939e46406e9c3 Mon Sep 17 00:00:00 2001 From: Thom Troy Date: Sun, 18 Mar 2018 17:40:27 +0000 Subject: [PATCH 4/4] add missing word --- 01-basic/C-static-library/README.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index 3adbf85..e35b0b7 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -83,7 +83,7 @@ For public headers it is often a good idea to have your include folder be "names with sub-directories. The directory passed to +target_include_directories+ will be the root of your -include directory and your C++ files should include the path from there to your header. +include directory tree and your C++ files should include the path from there to your header. For this example you can see that we do it as follows: [source,cpp]