mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
update some examples to require cmake v3
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -59,4 +59,5 @@ install_manifest.txt
|
||||
|
||||
/**/build
|
||||
/**/build.*
|
||||
/1
|
||||
|
||||
.tags
|
||||
|
||||
@@ -35,7 +35,7 @@ of CMake that is supported.
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
----
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,6 @@ project (compile_flags)
|
||||
# Add an executable
|
||||
add_executable(cmake_examples_compile_flags main.cpp)
|
||||
|
||||
target_compile_defintions(cmake_examples_compile_flags
|
||||
target_compile_definitions(cmake_examples_compile_flags
|
||||
PRIVATE EX3
|
||||
)
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
# Set the project name
|
||||
project (third_party_include)
|
||||
@@ -10,9 +10,6 @@ find_package(Boost 1.46.1 REQUIRED COMPONENTS filesystem system)
|
||||
# check if boost was found
|
||||
if(Boost_FOUND)
|
||||
message ("boost found")
|
||||
|
||||
# Include the boost headers
|
||||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
else()
|
||||
message (FATAL_ERROR "Cannot find Boost")
|
||||
endif()
|
||||
@@ -20,8 +17,14 @@ 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}
|
||||
)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
|
||||
project(subprojects)
|
||||
|
||||
|
||||
@@ -63,14 +63,12 @@ add_subdirectory(subbinary)
|
||||
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.
|
||||
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]
|
||||
----
|
||||
include_directories(
|
||||
${sublibrary1_SOURCE_DIR}/inc
|
||||
${sublibrary2_SOURCE_DIR}/inc
|
||||
)
|
||||
${sublibrary1_SOURCE_DIR}
|
||||
${sublibrary2_SOURCE_DIR}
|
||||
----
|
||||
|
||||
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
|
||||
|
||||
@@ -108,10 +128,42 @@ is added as a dependency.
|
||||
[source,cmake]
|
||||
----
|
||||
target_link_libraries(subbinary
|
||||
PUBLIC
|
||||
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
|
||||
|
||||
[source,bash]
|
||||
|
||||
@@ -1,11 +1,5 @@
|
||||
project(subbinary)
|
||||
|
||||
# Include the inc directories from the sub projects
|
||||
include_directories(
|
||||
${sublibrary1_SOURCE_DIR}/inc
|
||||
${sublibrary2_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
@@ -13,8 +7,16 @@ set(SOURCES
|
||||
# Create the executable
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
|
||||
# Link the static library from subproject1
|
||||
# This uses the project name to find out the library info
|
||||
# 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
|
||||
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
|
||||
)
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
# Set the project name
|
||||
project (sublibrary1)
|
||||
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
# Create a sources variable with a link to all cpp files to compile
|
||||
set(SOURCES
|
||||
src/sublib1.cpp
|
||||
@@ -12,3 +8,8 @@ set(SOURCES
|
||||
|
||||
# Add a library with the above sources
|
||||
add_library(${PROJECT_NAME} ${SOURCES})
|
||||
add_library(sub::lib1 ALIAS ${PROJECT_NAME})
|
||||
|
||||
include_directories( ${PROJECT_NAME}
|
||||
PUBLIC ${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
# Set the project name
|
||||
project (sublibrary2)
|
||||
|
||||
include_directories(
|
||||
add_library(${PROJECT_NAME} INTERFACE)
|
||||
add_library(sub::lib2 ALIAS ${PROJECT_NAME})
|
||||
|
||||
target_include_directories(${PROJECT_NAME}
|
||||
INTERFACE
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
# Set the project name
|
||||
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
|
||||
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_executable(cf_example
|
||||
main.cpp
|
||||
)
|
||||
|
||||
# include the directory with the new files
|
||||
target_include_directories( cf_example
|
||||
PUBLIC
|
||||
${CMAKE_BINARY_DIR}
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
# Set the project name
|
||||
project (protobuf_example)
|
||||
@@ -9,8 +9,6 @@ find_package(Protobuf REQUIRED)
|
||||
# check if protobuf was found
|
||||
if(PROTOBUF_FOUND)
|
||||
message ("protobuf found")
|
||||
include_directories(${PROTOBUF_INCLUDE_DIRS})
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
else()
|
||||
message (FATAL_ERROR "Cannot find Protobuf")
|
||||
endif()
|
||||
@@ -28,6 +26,14 @@ add_executable(protobuf_example
|
||||
${PROTO_SRCS}
|
||||
${PROTO_HDRS})
|
||||
|
||||
target_include_directories(protobuf_example
|
||||
PUBLIC
|
||||
${PROTOBUF_INCLUDE_DIRS}
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
# link the exe against the libraries
|
||||
target_link_libraries(protobuf_example
|
||||
${PROTOBUF_LIBRARIES})
|
||||
PUBLIC
|
||||
${PROTOBUF_LIBRARIES}
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
cmake_minimum_required (VERSION 3.0)
|
||||
|
||||
project(cppcheck_analysis)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
# Set the project name
|
||||
project (boost_unit_test)
|
||||
@@ -35,6 +35,9 @@ target_link_libraries(unit_tests
|
||||
${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)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
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
|
||||
target_link_libraries( cmake_examples_deb_bin
|
||||
PUBLIC
|
||||
cmake_examples_deb
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user