update some examples to require cmake v3

This commit is contained in:
Thom Troy
2016-08-17 00:09:36 +01:00
parent 3335200e43
commit 90013579f9
16 changed files with 123 additions and 47 deletions

3
.gitignore vendored
View File

@@ -59,4 +59,5 @@ install_manifest.txt
/**/build /**/build
/**/build.* /**/build.*
/1
.tags

View File

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

View File

@@ -9,6 +9,6 @@ project (compile_flags)
# Add an executable # Add an executable
add_executable(cmake_examples_compile_flags main.cpp) add_executable(cmake_examples_compile_flags main.cpp)
target_compile_defintions(cmake_examples_compile_flags target_compile_definitions(cmake_examples_compile_flags
PRIVATE EX3 PRIVATE EX3
) )

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 3.0)
# Set the project name # Set the project name
project (third_party_include) project (third_party_include)
@@ -10,9 +10,6 @@ find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
# check if boost was found # check if boost was found
if(Boost_FOUND) if(Boost_FOUND)
message ("boost found") message ("boost found")
# Include the boost headers
include_directories(${Boost_INCLUDE_DIRS})
else() else()
message (FATAL_ERROR "Cannot find Boost") message (FATAL_ERROR "Cannot find Boost")
endif() endif()
@@ -20,8 +17,14 @@ 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
${Boost_SYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY}
) )

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 2.6) cmake_minimum_required(VERSION 3.0)
# 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

@@ -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 2.6) cmake_minimum_required(VERSION 3.0)
# 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

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

View File

@@ -63,14 +63,12 @@ add_subdirectory(subbinary)
When a project is created using the `project()` command, CMake will automatically When a project is created using the `project()` command, CMake will automatically
create a number of variables which can be used to reference details about the project. create a number of variables which can be used to reference details about the project.
These variables can then be used by other sub-projects or the main project. For exampe, These variables can then be used by other sub-projects or the main project. For exampe,
to include header files between projects. to reference the source directory for a different project you can use.
[source,cmake] [source,cmake]
---- ----
include_directories( ${sublibrary1_SOURCE_DIR}
${sublibrary1_SOURCE_DIR}/inc ${sublibrary2_SOURCE_DIR}
${sublibrary2_SOURCE_DIR}/inc
)
---- ----
The variables created by CMake include: The variables created by CMake include:
@@ -97,6 +95,28 @@ In this example the binary directories created would be `sublibrary1_BINARY_DIR`
|======================================================================= |=======================================================================
## Header only Libraries
If you have a library that is created as a header only library, cmake supports the +INTERFACE+
target to allow creating a target without any build output. More details can be found from
link:https://cmake.org/cmake/help/v3.4/command/add_library.html#interface-libraries[here]
[source,cmake]
----
add_library(${PROJECT_NAME} INTERFACE)
----
When creating the target you can also include directories for that target using
the +INTERFACE+ scope. The +INTERFACE+ scope is use to make target requirements that are used in any Libraries
that link this target but not in the complation of the target itself.
[source,cmake]
----
target_include_directories(${PROJECT_NAME}
INTERFACE
${PROJECT_SOURCE_DIR}/inc
)
----
## Referencing Libraries from Sub-Projects ## Referencing Libraries from Sub-Projects
@@ -108,10 +128,42 @@ is added as a dependency.
[source,cmake] [source,cmake]
---- ----
target_link_libraries(subbinary target_link_libraries(subbinary
PUBLIC
sublibrary1 sublibrary1
) )
---- ----
Alternatively, you can create an alias target which allows you to reference the
target in read only contexts.
To create an alias target run:
[source,cmake]
----
add_library(sublibrary2)
add_library(sub::lib2 ALIAS sublibrary2)
----
To reference the alias, just it as follows:
[source,cmake]
----
target_link_libraries(subbinary
sublibrary1
)
----
## Include directories from sub-projects
When adding the libraries from the sub-projects, starting from cmake v3, there is
no need to add the projects include directores in the include directories of the
binary using them.
This is controlled by the scope in the `target_include_directories()` command when creating
the libraries. In this example because the subbinary executable links the sublibrary1
and sublibrary2 libraries it will automatically include the `${sublibrary1_SOURCE_DIR}/inc`
and `${sublibrary2_SOURCE_DIR}/inc` folders as they are exported with the
+PUBLIC+ and +INTERFACE+ scopes of the libraries.
# Building the example # Building the example
[source,bash] [source,bash]

View File

@@ -1,11 +1,5 @@
project(subbinary) project(subbinary)
# Include the inc directories from the sub projects
include_directories(
${sublibrary1_SOURCE_DIR}/inc
${sublibrary2_SOURCE_DIR}/inc
)
set(SOURCES set(SOURCES
main.cpp main.cpp
) )
@@ -13,8 +7,16 @@ set(SOURCES
# Create the executable # Create the executable
add_executable(${PROJECT_NAME} ${SOURCES}) add_executable(${PROJECT_NAME} ${SOURCES})
# Link the static library from subproject1 # Link the static library from subproject1 using it's alias sub::lib1
# This uses the project name to find out the library info # Link the header only library from subproject2 using it's alias sub::lib2
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
sublibrary1 sub::lib1
sub::lib2
)
# Include the inc directories from the sub projects
include_directories( ${PROJECT_NAME}
PRIVATE
${sublibrary1_SOURCE_DIR}/inc
${sublibrary2_SOURCE_DIR}/inc
) )

View File

@@ -1,10 +1,6 @@
# Set the project name # Set the project name
project (sublibrary1) project (sublibrary1)
include_directories(
${PROJECT_SOURCE_DIR}/inc
)
# Create a sources variable with a link to all cpp files to compile # Create a sources variable with a link to all cpp files to compile
set(SOURCES set(SOURCES
src/sublib1.cpp src/sublib1.cpp
@@ -12,3 +8,8 @@ set(SOURCES
# Add a library with the above sources # Add a library with the above sources
add_library(${PROJECT_NAME} ${SOURCES}) add_library(${PROJECT_NAME} ${SOURCES})
add_library(sub::lib1 ALIAS ${PROJECT_NAME})
include_directories( ${PROJECT_NAME}
PUBLIC ${PROJECT_SOURCE_DIR}/inc
)

View File

@@ -1,6 +1,10 @@
# Set the project name # Set the project name
project (sublibrary2) project (sublibrary2)
include_directories( add_library(${PROJECT_NAME} INTERFACE)
${PROJECT_SOURCE_DIR}/inc add_library(sub::lib2 ALIAS ${PROJECT_NAME})
)
target_include_directories(${PROJECT_NAME}
INTERFACE
${PROJECT_SOURCE_DIR}/inc
)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 3.0)
# Set the project name # Set the project name
project (cf_example) project (cf_example)
@@ -17,10 +17,13 @@ configure_file(ver.h.in ${PROJECT_BINARY_DIR}/ver.h)
# This file can only use the @VARIABLE@ syntax in the file # This file can only use the @VARIABLE@ syntax in the file
configure_file(path.h.in ${PROJECT_BINARY_DIR}/path.h @ONLY) configure_file(path.h.in ${PROJECT_BINARY_DIR}/path.h @ONLY)
# include the directory with the new files
include_directories(${CMAKE_BINARY_DIR})
# 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
target_include_directories( cf_example
PUBLIC
${CMAKE_BINARY_DIR}
)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 3.0)
# Set the project name # Set the project name
project (protobuf_example) project (protobuf_example)
@@ -9,8 +9,6 @@ find_package(Protobuf REQUIRED)
# check if protobuf was found # check if protobuf was found
if(PROTOBUF_FOUND) if(PROTOBUF_FOUND)
message ("protobuf found") message ("protobuf found")
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
else() else()
message (FATAL_ERROR "Cannot find Protobuf") message (FATAL_ERROR "Cannot find Protobuf")
endif() endif()
@@ -28,6 +26,14 @@ add_executable(protobuf_example
${PROTO_SRCS} ${PROTO_SRCS}
${PROTO_HDRS}) ${PROTO_HDRS})
target_include_directories(protobuf_example
PUBLIC
${PROTOBUF_INCLUDE_DIRS}
${CMAKE_CURRENT_BINARY_DIR}
)
# link the exe against the libraries # link the exe against the libraries
target_link_libraries(protobuf_example target_link_libraries(protobuf_example
${PROTOBUF_LIBRARIES}) PUBLIC
${PROTOBUF_LIBRARIES}
)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 2.6) cmake_minimum_required (VERSION 3.0)
project(cppcheck_analysis) project(cppcheck_analysis)
@@ -23,4 +23,4 @@ if( CPPCHECK_FOUND )
ADD_DEPENDENCIES(analysis ${ALL_ANALYSIS_TARGETS}) ADD_DEPENDENCIES(analysis ${ALL_ANALYSIS_TARGETS})
set_target_properties(analysis PROPERTIES EXCLUDE_FROM_ALL TRUE) set_target_properties(analysis PROPERTIES EXCLUDE_FROM_ALL TRUE)
message("analysis analysis targets are ${ALL_ANALYSIS_TARGETS}") message("analysis analysis targets are ${ALL_ANALYSIS_TARGETS}")
endif() endif()

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 3.0)
# Set the project name # Set the project name
project (boost_unit_test) project (boost_unit_test)
@@ -35,6 +35,9 @@ target_link_libraries(unit_tests
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
) )
add_definitions (-DBOOST_TEST_DYN_LINK) target_compile_definitions(unit_tests
PRIVATE
BOOST_TEST_DYN_LINK
)
add_test(test_all unit_tests) add_test(test_all unit_tests)

View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 3.0)
project(cmake_examples_deb) project(cmake_examples_deb)
@@ -39,6 +39,7 @@ add_executable(cmake_examples_deb_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_deb_bin target_link_libraries( cmake_examples_deb_bin
PUBLIC
cmake_examples_deb cmake_examples_deb
) )