started updating examples for cmake v3 syntax

This commit is contained in:
ttroy50
2016-04-15 23:49:39 +01:00
parent e56847a5ef
commit 3335200e43
12 changed files with 96 additions and 48 deletions

View File

@@ -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)

View File

@@ -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})
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
)

View File

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

View File

@@ -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
)

View File

@@ -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
)
----

View File

@@ -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
)

View File

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

View File

@@ -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
)
############################################################

View File

@@ -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)

View File

@@ -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
)

View File

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