Skip to content

Commit 63ca686

Browse files
Add a second template for a header-only library
1 parent 7191e50 commit 63ca686

File tree

5 files changed

+122
-9
lines changed

5 files changed

+122
-9
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ cmake_minimum_required(VERSION 3.5)
1818
# listed here: https://cmake.org/cmake/help/latest/command/project.html.
1919
# We use this name to export all the files such that is then possible to use
2020
# find_package(LibTemplateCMake) in third party projects.
21-
# LANGUAGES specifies which languages your project supports. To have a broad
22-
# support with external libraries, it is good practice to list both CXX and C.
23-
# Infact, some legacy Find<package>.cmake files that require the C language to
24-
# be enabled and thus using only CXX may cause issues. Note that by default when
21+
# LANGUAGES specifies which languages your project supports. To have a broad
22+
# support with external libraries, it is good practice to list both CXX and C.
23+
# Infact, some legacy Find<package>.cmake files that require the C language to
24+
# be enabled and thus using only CXX may cause issues. Note that by default when
2525
# LANGUAGES is not specified CMake enables both CXX and C.
2626
project(LibTemplateCMake
2727
LANGUAGES CXX C
@@ -141,7 +141,6 @@ include(InstallBasicPackageFiles)
141141
install_basic_package_files(${PROJECT_NAME}
142142
VERSION ${${PROJECT_NAME}_VERSION}
143143
COMPATIBILITY AnyNewerVersion
144-
EXPORT ${PROJECT_NAME}
145144
VARS_PREFIX ${PROJECT_NAME}
146145
NO_CHECK_REQUIRED_COMPONENTS_MACRO)
147146
# Add the uninstall target

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(LibTemplateCMake)
2+
add_subdirectory(LibHeaderOnlyTemplateCMake)
23
add_subdirectory(ExeExample)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# This is the "physical" (i.e. real) name of the library.
2+
# The actual file name of the library built is constructed based on conventions
3+
# of the native platform, such as lib<LIBRARY_TARGET_NAME>.a,
4+
# lib<LIBRARY_TARGET_NAME>.dylib or <LIBRARY_TARGET_NAME>.lib.
5+
set(LIBRARY_TARGET_NAME ${PROJECT_NAME}HeaderOnly)
6+
7+
# List of HPP (header) library files.
8+
set(${LIBRARY_TARGET_NAME}_HDR
9+
include/LibHeaderOnlyTemplateCMake/LibHeaderOnlyTemplateCMake.h
10+
)
11+
12+
# You can add an external dependency using the find_package() function call
13+
# See: https://cmake.org/cmake/help/latest/command/find_package.html
14+
# Note that the imported objects resulting from the find_package() depends upon
15+
# the configuration files generated by the developer of the library.
16+
# These imported objects can be either:
17+
# - variables that must be used to include directories and/or link libraries
18+
# with function calls like target_include_directories() and/or
19+
# target_link_libraries();
20+
# - a CMake target that must only be linked using target_link_libraries() and
21+
# CMake will take care of including and/or linking the appropriate
22+
# directories and/or libraries.
23+
# See: https://cmake.org/cmake/help/latest/command/target_include_directories.html
24+
# See: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
25+
# For example:
26+
# find_package(FooPackage) may import either
27+
# - variables: FooPackage_INCLUDE_DIRS and FooPackage_LIBRARIES
28+
# - target: FooPackage::FooPackage
29+
30+
# Adds a library target called ${LIBRARY_TARGET_NAME} to be built from the
31+
# source and header files listed in the command invocation.
32+
add_library(${LIBRARY_TARGET_NAME} INTERFACE)
33+
34+
# Within this project, you can link to this library by just specifing the name
35+
# of the target, i.e. ${LIBRARY_TARGET_NAME} = LibTemplateCMakeHeaderOnly. It is
36+
# useful, however, to define an alias of this library with the scope of the
37+
# exported library itself because (1) you will link against it with the exact
38+
# same syntax of an imported library and (2) because names having a double-colon
39+
# (::) are always treated as the name of either an alias or imported target. Any
40+
# attempt to use such a name for a different target type will result in an error.
41+
add_library(${PROJECT_NAME}::${LIBRARY_TARGET_NAME} ALIAS ${LIBRARY_TARGET_NAME})
42+
43+
# Specify include directories for both compilation and installation process.
44+
# The $<INSTALL_PREFIX> generator expression is useful to ensure to create
45+
# relocatable configuration files, see https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-relocatable-packages
46+
target_include_directories(${LIBRARY_TARGET_NAME} INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
47+
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
48+
49+
# If you used find_package() you need to use target_include_directories() and/or
50+
# target_link_libraries(). As explained previously, depending on the imported
51+
# objects, you may need to call either or both:
52+
# - with imported variable:
53+
# target_include_directories(${LIBRARY_TARGET_NAME} ${FooPackage_INCLUDE_DIRS})
54+
# target_link_libraries(${LIBRARY_TARGET_NAME} ${FooPackage_LIBRARIES})
55+
# - with imported target:
56+
# target_link_libraries(${LIBRARY_TARGET_NAME} FooPackage_LIBRARIES::FooPackage_LIBRARIES)
57+
58+
# Specify installation targets, typology and destination folders.
59+
install(TARGETS ${LIBRARY_TARGET_NAME}
60+
EXPORT ${PROJECT_NAME})
61+
62+
install(FILES ${${LIBRARY_TARGET_NAME}_HDR}
63+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}")
64+
65+
66+
message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.")
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef LIB_TEMPLATE_CMAKE_H
2+
#define LIB_TEMPLATE_CMAKE_H
3+
4+
namespace LibTemplateCMake {
5+
6+
/**
7+
* \brief Computes the sum of two values.
8+
* \param op1 first input value.
9+
* \param op2 second input value.
10+
* \returns sum of op1 and op2.
11+
*/
12+
template<typename DatumType>
13+
DatumType sum(DatumType op1, DatumType op2)
14+
{
15+
return (op1 + op2);
16+
}
17+
18+
19+
/**
20+
* \brief Computes the difference of two values.
21+
* \param op1 first input value.
22+
* \param op2 second input value.
23+
* \returns difference of op1 and op2.
24+
*/
25+
template<typename DatumType>
26+
DatumType sub(DatumType op1, DatumType op2)
27+
{
28+
return (op1 - op2);
29+
}
30+
31+
} // namespace LibTemplateCMake
32+
33+
#endif /* LIB_TEMPLATE_CMAKE_H */

src/LibTemplateCMake/CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
# of the native platform, such as lib<LIBRARY_TARGET_NAME>.a,
44
# lib<LIBRARY_TARGET_NAME>.dylib or <LIBRARY_TARGET_NAME>.lib.
55
#
6-
# In order to use a library name that is not the same as the `EXPORT` (in the
7-
# `install` command), it is necessary to pass the `FIRST_TARGET` option to
8-
# `install_basic_package_files` in the main CMakeLists.txt file.
9-
# In this case the project name is used for both, therefore it is not necessary.
6+
# In order to use a library name LIBRARY_TARGET_NAME that is not the same as the
7+
# `EXPORT` used in the `install` command (see the end of this file), it is
8+
# required to use `FIRST_TARGET` option of the `install_basic_package_files`
9+
# command in the main CMakeLists.txt file with the name of one of the exported
10+
# targets. `FIRST_TARGET` option defaults to LIBRARY_TARGET_NAME.
11+
# By using setting LIBRARY_TARGET_NAME to ${PROJECT_NAME} it is not necessary to
12+
# use `FIRST_TARGET` option in the main CMakeLists.txt.
13+
# In general, withing a C++ project, there is at least one exported target whose
14+
# name is always know. Consequently, `FIRST_TARGET` option in the main
15+
# CMakeLists.txt can be statically set to such target name. In the context of
16+
# projects where users may choose what target to export, thus the name of a
17+
# target may change at configure time, you can use propagate back to parent
18+
# CMakeLists.txt, up to the main one, a variable containing the name of a
19+
# target. In order to do it, you can use `set` command with `PARENT_SCOPE`
20+
# option, see https://cmake.org/cmake/help/latest/command/set.html.
1021
set(LIBRARY_TARGET_NAME ${PROJECT_NAME})
1122

1223
# List of CPP (source) library files.
@@ -78,3 +89,6 @@ install(TARGETS ${LIBRARY_TARGET_NAME}
7889
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
7990
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin
8091
PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}" COMPONENT dev)
92+
93+
94+
message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.")

0 commit comments

Comments
 (0)