Skip to content

Commit 5f8ed4b

Browse files
authored
A lecture about installing and reusing packages with CMake (#109)
1 parent 6077d9a commit 5f8ed4b

File tree

12 files changed

+878
-0
lines changed

12 files changed

+878
-0
lines changed

lectures/cmake_install.md

Lines changed: 733 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
cmake_minimum_required(VERSION 3.16..3.24)
2+
project(core_project
3+
VERSION 0.0.1
4+
DESCRIPTION "Sample project with a library target"
5+
LANGUAGES CXX)
6+
7+
if(NOT CMAKE_BUILD_TYPE)
8+
set(CMAKE_BUILD_TYPE Release CACHE INTERNAL "Build type")
9+
endif()
10+
11+
message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
12+
13+
add_subdirectory(core_project)
14+
add_subdirectory(examples)
15+
16+
include(CMakePackageConfigHelpers)
17+
18+
# Create a config file that CMake looks for when we call FindPackage(core_project)
19+
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
20+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
21+
INSTALL_DESTINATION "share/cmake/${PROJECT_NAME}"
22+
)
23+
24+
# Create a versioned config file that CMake uses to compare version of the package.
25+
write_basic_package_version_file(
26+
${PROJECT_NAME}ConfigVersion.cmake
27+
VERSION ${PACKAGE_VERSION}
28+
COMPATIBILITY AnyNewerVersion
29+
)
30+
31+
# Copy these files into the install directory.
32+
install(
33+
FILES
34+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
35+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
36+
DESTINATION
37+
share/cmake/${PROJECT_NAME}
38+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@PACKAGE_INIT@
2+
3+
# Automatically include all exported library files
4+
file(GLOB EXPORT_FILES "${CMAKE_CURRENT_LIST_DIR}/*_export.cmake")
5+
foreach(EXPORT_FILE ${EXPORT_FILES})
6+
include(${EXPORT_FILE})
7+
endforeach()
8+
9+
check_required_components(core_project)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
set(LIBRARY_NAME some_library)
2+
add_library(${LIBRARY_NAME} ${LIBRARY_NAME}.cpp)
3+
target_compile_features(${LIBRARY_NAME} PUBLIC cxx_std_17)
4+
target_include_directories(${LIBRARY_NAME}
5+
PUBLIC
6+
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
7+
$<INSTALL_INTERFACE:include/>
8+
)
9+
add_library(${PROJECT_NAME}::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME})
10+
11+
install(
12+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
13+
DESTINATION include
14+
FILES_MATCHING PATTERN "*.hpp"
15+
)
16+
17+
install(
18+
TARGETS ${LIBRARY_NAME}
19+
EXPORT ${LIBRARY_NAME}_export
20+
)
21+
22+
install(EXPORT ${LIBRARY_NAME}_export
23+
FILE ${LIBRARY_NAME}_export.cmake
24+
NAMESPACE ${PROJECT_NAME}::
25+
DESTINATION share/cmake/${PROJECT_NAME}
26+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "core_project/some_library.hpp"
2+
3+
#include <iostream>
4+
5+
namespace core_project {
6+
7+
void PrintHello() noexcept {
8+
std::cout << "Hello world!" << std::endl;
9+
}
10+
11+
} // namespace core_project
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace core_project {
4+
5+
void PrintHello() noexcept;
6+
7+
} // namespace core_project
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
add_executable(print_hello print_hello.cpp)
2+
target_link_libraries(print_hello PRIVATE core_project::some_library)
3+
4+
install(TARGETS print_hello)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "core_project/some_library.hpp"
2+
3+
int main() {
4+
core_project::PrintHello();
5+
return 0;
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.16..3.24)
2+
project(dependent_project
3+
VERSION 0.0.1
4+
DESCRIPTION "Sample project that uses the core project"
5+
LANGUAGES CXX)
6+
7+
add_subdirectory(examples)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
find_package(core_project REQUIRED CONFIG)
2+
3+
add_executable(print_hello print_hello.cpp)
4+
target_link_libraries(print_hello PRIVATE core_project::some_library)

0 commit comments

Comments
 (0)