Merge pull request #15 from ttroy50/update_cmake_35

Update cmake 35
This commit is contained in:
Thom Troy
2018-03-18 18:48:17 +00:00
committed by GitHub
52 changed files with 294 additions and 214 deletions

View File

@@ -5,11 +5,11 @@ sudo: required
compiler: compiler:
- gcc - gcc
before_install: before_install:
- docker pull matrim/cmake-examples:3.4.3
- docker pull matrim/cmake-examples:3.5.1 - docker pull matrim/cmake-examples:3.5.1
- docker pull matrim/cmake-examples:3.10.3
script: 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.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: branches:
except: except:
- gh-pages - gh-pages

View File

@@ -1,10 +1,10 @@
# Set the minimum version of CMake that can be used # Set the minimum version of CMake that can be used
# To find the cmake version run # To find the cmake version run
# $ cmake --version # $ cmake --version
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (hello_cmake) project (hello_cmake)
# Add an executable # Add an executable
add_executable(hello_cmake main.cpp) add_executable(hello_cmake main.cpp)

View File

@@ -35,7 +35,7 @@ of CMake that is supported.
[source,cmake] [source,cmake]
---- ----
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
---- ----

View File

@@ -1,7 +1,7 @@
# Set the minimum version of CMake that can be used # Set the minimum version of CMake that can be used
# To find the cmake version run # To find the cmake version run
# $ cmake --version # $ cmake --version
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (hello_headers) project (hello_headers)
@@ -12,12 +12,12 @@ set(SOURCES
src/main.cpp src/main.cpp
) )
# Add an executable with the above sources # Add an executable with the above sources
add_executable(hello_headers ${SOURCES}) add_executable(hello_headers ${SOURCES})
# Set the direcoties that should be included in the build command for this target # 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/ # when running g++ these will be included as -I/directory/path/
target_include_directories(hello_headers target_include_directories(hello_headers
PRIVATE ${PROJECT_SOURCE_DIR}/include PRIVATE
${PROJECT_SOURCE_DIR}/include
) )

View File

@@ -83,6 +83,16 @@ file(GLOB SOURCES "src/*.cpp")
---- ----
==== ====
[TIP]
====
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.
====
## Including Directories ## Including Directories
When you have different include folders, you can make your compiler aware of them using the When you have different include folders, you can make your compiler aware of them using the
@@ -91,8 +101,9 @@ When you have different include folders, you can make your compiler aware of the
[source,cmake] [source,cmake]
---- ----
target_include_directories(target 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] 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]

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
project(hello_library) project(hello_library)
@@ -6,16 +6,14 @@ project(hello_library)
# Create a library # Create a library
############################################################ ############################################################
# Source files to be used in the library #Generate the static library from the library sources
set(library_SOURCES add_library(hello_library STATIC
src/Hello.cpp src/Hello.cpp
) )
#Generate the static library from the library sources
add_library(hello_library STATIC ${library_SOURCES})
target_include_directories(hello_library 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 # Create an executable
############################################################ ############################################################
# Source fles for the binary # Add an executable with the above sources
set(binary_SOURCES add_executable(hello_binary
src/main.cpp 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 # link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary target_link_libraries( hello_binary
PRIVATE hello_library PRIVATE
hello_library
) )

View File

@@ -6,7 +6,9 @@ toc::[]
# Introduction # 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 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: The files in this tutorial are below:
@@ -15,14 +17,15 @@ $ tree
. .
├── CMakeLists.txt ├── CMakeLists.txt
├── include ├── include
│   └── Hello.h │   └── static
│   └── Hello.h
└── src └── src
├── Hello.cpp ├── Hello.cpp
└── main.cpp └── main.cpp
``` ```
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run * 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/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main * link:src/main.cpp[] - The source file with main
@@ -36,15 +39,19 @@ This is called as follows:
[source,cmake] [source,cmake]
---- ----
set(library_SOURCES add_library(hello_library STATIC
src/Hello.cpp 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 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 ## Populating Including Directories
@@ -53,7 +60,8 @@ In this example, we include directories in the library using the +target_include
[source,cmake] [source,cmake]
---- ----
target_include_directories(hello_library target_include_directories(hello_library
PUBLIC ${PROJECT_SOURCE_DIR}/include PUBLIC
${PROJECT_SOURCE_DIR}/include
) )
---- ----
@@ -64,11 +72,29 @@ This will cause the included directory used in the following places:
The meaning of scopes are: 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. * +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. * +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 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]
----
#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 ## Linking a Library
When creating an executable that will use your library you must tell the compiler 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] [source,cmake]
---- ----
add_executable(hello_binary ${binary_SOURCES}) add_executable(hello_binary
src/main.cpp
)
target_link_libraries( hello_binary target_link_libraries( hello_binary
PRIVATE hello_library PRIVATE
hello_library
) )
---- ----

View File

@@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include "Hello.h" #include "static/Hello.h"
void Hello::print() void Hello::print()
{ {
std::cout << "Hello Static Library!" << std::endl; std::cout << "Hello Static Library!" << std::endl;
} }

View File

@@ -1,8 +1,8 @@
#include "Hello.h" #include "static/Hello.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Hello hi; Hello hi;
hi.print(); hi.print();
return 0; return 0;
} }

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
project(hello_library) project(hello_library)
@@ -6,32 +6,28 @@ project(hello_library)
# Create a library # Create a library
############################################################ ############################################################
# Source files to be used in the library #Generate the shared library from the library sources
set(library_SOURCES add_library(hello_library SHARED
src/Hello.cpp 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) add_library(hello::library ALIAS hello_library)
target_include_directories(hello_library target_include_directories(hello_library
PUBLIC ${PROJECT_SOURCE_DIR}/include PUBLIC
${PROJECT_SOURCE_DIR}/include
) )
############################################################ ############################################################
# Create an executable # Create an executable
############################################################ ############################################################
# Source fles for the binary # Add an executable with the above sources
set(binary_SOURCES add_executable(hello_binary
src/main.cpp 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 # link the new hello_library target with the hello_binary target
target_link_libraries( hello_binary target_link_libraries( hello_binary
PRIVATE hello::library PRIVATE
hello::library
) )

View File

@@ -17,14 +17,15 @@ $ tree
. .
├── CMakeLists.txt ├── CMakeLists.txt
├── include ├── include
│   └── Hello.h │   └── shared
│   └── Hello.h
└── src └── src
├── Hello.cpp ├── Hello.cpp
└── main.cpp └── main.cpp
``` ```
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run * 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/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main * link:src/main.cpp[] - The source file with main
@@ -39,15 +40,13 @@ This is called as follows:
[source,cmake] [source,cmake]
---- ----
set(library_SOURCES add_library(hello_library SHARED
src/Hello.cpp 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 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 ## Alias Target
@@ -67,10 +66,13 @@ executable use the the +target_link_library()+ function to point to your library
[source,cmake] [source,cmake]
---- ----
add_executable(hello_binary ${binary_SOURCES}) add_executable(hello_binary
src/main.cpp
)
target_link_libraries( hello_binary target_link_libraries(hello_binary
hello::library PRIVATE
hello::library
) )
---- ----

View File

@@ -1,8 +1,8 @@
#include <iostream> #include <iostream>
#include "Hello.h" #include "shared/Hello.h"
void Hello::print() void Hello::print()
{ {
std::cout << "Hello Shared Library!" << std::endl; std::cout << "Hello Shared Library!" << std::endl;
} }

View File

@@ -1,8 +1,8 @@
#include "Hello.h" #include "shared/Hello.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Hello hi; Hello hi;
hi.print(); hi.print();
return 0; return 0;
} }

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
project(cmake_examples_install) project(cmake_examples_install)
@@ -6,34 +6,29 @@ project(cmake_examples_install)
# Create a library # Create a library
############################################################ ############################################################
# Source files to be used in the library #Generate the shared library from the library sources
set(library_SOURCES add_library(cmake_examples_inst SHARED
src/Hello.cpp 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 target_include_directories(cmake_examples_inst
PUBLIC ${PROJECT_SOURCE_DIR}/include PUBLIC
${PROJECT_SOURCE_DIR}/include
) )
############################################################ ############################################################
# Create an executable # Create an executable
############################################################ ############################################################
# Source fles for the binary # Add an executable with the above sources
set(binary_SOURCES add_executable(cmake_examples_inst_bin
src/main.cpp 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 # link the new hello_library target with the hello_binary target
target_link_libraries( cmake_examples_inst_bin 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 install (TARGETS cmake_examples_inst
LIBRARY DESTINATION lib) LIBRARY DESTINATION lib)
# Header files
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/
DESTINATION include)
# Config # Config
install (FILES cmake-examples.conf install (FILES cmake-examples.conf

View File

@@ -17,7 +17,8 @@ $ tree
├── cmake-examples.conf ├── cmake-examples.conf
├── CMakeLists.txt ├── CMakeLists.txt
├── include ├── include
│   └── Hello.h │   └── installing
│   └── Hello.h
├── README.adoc ├── README.adoc
└── src └── src
├── Hello.cpp ├── Hello.cpp
@@ -26,7 +27,7 @@ $ tree
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run * link:CMakeLists.txt[] - Contains the CMake commands you wish to run
* link:cmake-examples.conf[] - An example configuration file * 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/Hello.cpp[] - A source file to compile
* link:src/main.cpp[] - The source file with main * 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] [source,cmake]
---- ----
install (FILES cmake-examples.conf install (FILES cmake-examples.conf

View File

@@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include "Hello.h" #include "installing/Hello.h"
void Hello::print() void Hello::print()
{ {

View File

@@ -1,8 +1,8 @@
#include "Hello.h" #include "installing/Hello.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
Hello hi; Hello hi;
hi.print(); hi.print();
return 0; return 0;
} }

View File

@@ -1,7 +1,7 @@
# Set the minimum version of CMake that can be used # Set the minimum version of CMake that can be used
# To find the cmake version run # To find the cmake version run
# $ cmake --version # $ cmake --version
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set a default build type if none was specified # Set a default build type if none was specified
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set a default C++ compile flag # Set a default C++ compile flag
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE) set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (third_party_include) project (third_party_include)
@@ -17,14 +17,8 @@ endif()
# Add an executable # Add an executable
add_executable(third_party_include main.cpp) 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 # link against the boost libraries
target_link_libraries( third_party_include target_link_libraries( third_party_include
PRIVATE PRIVATE
${Boost_SYSTEM_LIBRARY} Boost::filesystem
${Boost_FILESYSTEM_LIBRARY}
) )

View File

@@ -85,33 +85,63 @@ variable, these are package specific and are typically documented at the top of
The variables exported in this example include: The variables exported in this example include:
* `Boost_INCLUDE_DIRS` - The path to the boost header files. * `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 In some cases you can also check these variables by examining the cache using
ccmake or cmake-gui. 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. 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::boost` for header only libraries
* `Boost_FILESYSTEM_LIBRARY` with `Boost::filesystem` * `Boost::system` for the boost system library.
* `Boost_SYSTEM_LIBRARY` with `Boost::system`. If you include `Boost::filesystem` it automatically includes `Boost::system` * `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] [source,cmake]
---- ----
target_link_libraries( third_party_include target_link_libraries( third_party_include
PRIVATE 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 # Building the Example
[source,bash] [source,bash]

View File

@@ -1,7 +1,7 @@
# Set the minimum version of CMake that can be used # Set the minimum version of CMake that can be used
# To find the cmake version run # To find the cmake version run
# $ cmake --version # $ cmake --version
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (hello_cmake) project (hello_cmake)

View File

@@ -1,7 +1,7 @@
# Set the minimum version of CMake that can be used # Set the minimum version of CMake that can be used
# To find the cmake version run # To find the cmake version run
# $ cmake --version # $ cmake --version
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (hello_cmake) project (hello_cmake)

View File

@@ -20,5 +20,5 @@ add_executable(imported_targets main.cpp)
# link against the boost libraries # link against the boost libraries
target_link_libraries( imported_targets target_link_libraries( imported_targets
PRIVATE PRIVATE
Boost::filesystem Boost::filesystem
) )

View File

@@ -32,8 +32,7 @@ This example requires the following:
## Imported Target ## Imported Target
Imported targets are read-only targets that are exported by FindXXX modules. The benefit of imported Imported targets are read-only targets that are exported by FindXXX modules.
targets are that they can also populate include directories and linked libraries.
To include boost filesystem you can do the following: 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 target_link_libraries( imported_targets
PRIVATE PRIVATE
Boost::filesystem Boost::filesystem
) )
---- ----

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.5)
project(subprojects) project(subprojects)

View File

@@ -145,7 +145,7 @@ is added as a dependency.
---- ----
target_link_libraries(subbinary target_link_libraries(subbinary
PUBLIC PUBLIC
sublibrary1 sublibrary1
) )
---- ----
@@ -164,7 +164,7 @@ To reference the alias, just it as follows:
[source,cmake] [source,cmake]
---- ----
target_link_libraries(subbinary target_link_libraries(subbinary
sublibrary1 sub::lib2
) )
---- ----

View File

@@ -1,11 +1,7 @@
project(subbinary) project(subbinary)
set(SOURCES
main.cpp
)
# Create the executable # 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 static library from subproject1 using it's alias sub::lib1
# Link the header only library from subproject2 using it's alias sub::lib2 # Link the header only library from subproject2 using it's alias sub::lib2

View File

@@ -1,13 +1,8 @@
# Set the project name # Set the project name
project (sublibrary1) 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 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}) add_library(sub::lib1 ALIAS ${PROJECT_NAME})
target_include_directories( ${PROJECT_NAME} target_include_directories( ${PROJECT_NAME}

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (cf_example) project (cf_example)
@@ -20,10 +20,10 @@ configure_file(path.h.in ${PROJECT_BINARY_DIR}/path.h @ONLY)
# Add an executable # Add an executable
add_executable(cf_example add_executable(cf_example
main.cpp main.cpp
) )
# include the directory with the new files # include the directory with the new files
target_include_directories( cf_example target_include_directories( cf_example
PUBLIC PUBLIC
${CMAKE_BINARY_DIR} ${CMAKE_BINARY_DIR}
) )

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (protobuf_example) project (protobuf_example)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.5)
project(cppcheck_analysis) project(cppcheck_analysis)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject1) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main1.cpp)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject2) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main2.cpp)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.5)
project(cppcheck_analysis) project(cppcheck_analysis)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject1) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main1.cpp)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject2) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main2.cpp)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.5)
project(cppcheck_analysis) project(cppcheck_analysis)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject1) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main1.cpp)

View File

@@ -1,10 +1,5 @@
# Set the project name # Set the project name
project (subproject2) 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 an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} main2.cpp)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.0) cmake_minimum_required (VERSION 3.5)
project(cppcheck_analysis) project(cppcheck_analysis)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (boost_unit_test) project (boost_unit_test)
@@ -7,19 +7,21 @@ project (boost_unit_test)
# find a boost install with the libraries unit_test_framework # find a boost install with the libraries unit_test_framework
find_package(Boost 1.46.1 REQUIRED COMPONENTS 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 Reverse.cpp
Palindrome.cpp Palindrome.cpp
) )
include_directories( target_include_directories(example_boost_unit_test
${Boost_INCLUDE_DIRS} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
) )
# Add an library for the example classes
add_library(example_boost_unit_test ${SOURCES})
target_link_libraries(example_boost_unit_test
PUBLIC
Boost::boost
)
############################################# #############################################
# Unit tests # Unit tests
@@ -32,7 +34,7 @@ add_executable(unit_tests unit_tests.cpp)
target_link_libraries(unit_tests target_link_libraries(unit_tests
example_boost_unit_test example_boost_unit_test
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} Boost::unit_test_framework
) )
target_compile_definitions(unit_tests target_compile_definitions(unit_tests

View File

@@ -59,10 +59,13 @@ add_executable(unit_tests unit_tests.cpp)
target_link_libraries(unit_tests target_link_libraries(unit_tests
example_boost_unit_test 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 In the above code, a unit test binary is added, which links against the boost unit-test-framework

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (catch_unit_test) project (catch_unit_test)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
# Set the project name # Set the project name
project (google_test_example) project (google_test_example)

View File

@@ -1,11 +1,7 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.5)
project(cmake_examples_deb) project(cmake_examples_deb)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
# set a project version # set a project version
set (deb_example_VERSION_MAJOR 0) set (deb_example_VERSION_MAJOR 0)
set (deb_example_VERSION_MINOR 2) 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 # 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 #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 # Create an executable
############################################################ ############################################################
# Source fles for the binary
set(binary_SOURCES
src/main.cpp
)
# Add an executable with the above sources # 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 # link the new hello_library target with the hello_binary target
target_link_libraries( cmake_examples_deb_bin target_link_libraries( cmake_examples_deb_bin
PUBLIC PUBLIC
cmake_examples_deb cmake_examples_deb
) )
############################################################ ############################################################

View File

@@ -11,14 +11,17 @@ 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 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. 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. 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 The first examples are very basic and slowly increase in complexity drawing on previous examples to show
more complex use cases. 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"] 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: The basic requirements for most examples are:
* CMake v3.x * CMake v3.5+
* A c++ compiler (defaults to gcc) * A c++ compiler (defaults to gcc)
* make * make
@@ -71,7 +74,7 @@ To build the full set of cmake-examples test cases you can run:
[source,bash] [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 git clone https://github.com/ttroy50/cmake-examples.git
cd cmake-examples cd cmake-examples
./test.sh ./test.sh

View File

@@ -22,7 +22,7 @@ For example:
[source,bash] [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 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]. 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 * 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 $ 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 # 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. 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] [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] [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 git clone https://github.com/ttroy50/cmake-examples.git
cd cmake-examples cd cmake-examples
./test.sh ./test.sh
@@ -80,5 +86,5 @@ Below is an example of loading a volume and automatically running all cmake-exam
[source,bash] [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
---- ----

View File

@@ -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"]