Skip to content

Commit

Permalink
Update Readme and Debug preset
Browse files Browse the repository at this point in the history
  • Loading branch information
ClausKlein committed Oct 12, 2024
1 parent 6d6508e commit 9d446d1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
5 changes: 2 additions & 3 deletions CMakeGenericPresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
"generator": "Ninja",
"hidden": true,
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": true,
"CMAKE_MESSAGE_LOG_LEVEL": "WARNING",
"MYLIB_SHARED_LIBS": true,
"MYLIB_BUILD_TESTS": true,
"MYLIB_BUILD_EXAMPLES": true
}
Expand Down Expand Up @@ -47,6 +45,7 @@
"type": "path",
"value": "${sourceDir}/stagedir"
},
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_MESSAGE_LOG_LEVEL": "STATUS",
"BUILD_SHARED_LIBS": false,
"BUILD_TESTING": true
Expand All @@ -63,7 +62,7 @@
"name": "clang-tidy",
"hidden": true,
"cacheVariables": {
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--header-filter=^${sourceDir}/"
"CMAKE_CXX_CLANG_TIDY": "clang-tidy;--header-filter=^${sourceDir}/include"
}
},
{
Expand Down
12 changes: 0 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ target_sources(
)
target_compile_definitions(mylib PUBLIC "$<$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>:MYLIB_STATIC_DEFINE>")

# target_include_directories(mylib
# PUBLIC
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
# "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>")

set_target_properties(mylib PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR} VERSION ${PROJECT_VERSION})

if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
Expand All @@ -116,13 +111,6 @@ if(MYLIB_INSTALL AND NOT CMAKE_SKIP_INSTALL_RULES)
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
)

# install(DIRECTORY include/
# TYPE INCLUDE
# COMPONENT mylib-dev)
# install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/mylib/${export_file_name}"
# COMPONENT mylib-dev
# DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/mylib")

set(targets_file "mylib-shared-targets.cmake")

if(NOT BUILD_SHARED_LIBS)
Expand Down
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
# Overview

Template repository to be used as a base for creating C++ libraries built with CMake. This repository provides:
* Commonly accepted directory layout: `cmake` for CMake utilities and package config, `include/<libname>` for public headers, `src` for library sources and private headers, `examples` and `tests` for library examples and tests correspondingly.
Template repository to be used as a base for creating C++ libraries built with CMake.

## This repository provides:

* Commonly accepted directory layout:

- `cmake` for CMake utilities and package config
- `include/<libname>` for public headers
- `src` for library sources and private headers
- `examples`
- `tests` for library examples and tests correspondingly

* `CMakeLists.txt` files thoroughly implemented with the following ideas in mind:
* user should be able to build library both as subproject (probably fetched with CMake's [FetchContent_MakeAvailable](https://cmake.org/cmake/help/latest/module/FetchContent.html)) and as a stand-alone project
* user should be able to build examples/tests as part of the library build or as a stand-alone projects
* multi-configuration CMake generators should be supported
* package systems (conan, vcpkg and others) should be respected, which means that library `CMakeLists.txt` file should contain only **build requirements**, i.e. should not hardcode any compiler/linker flags unless they are absolutely required to build the library
* Basic [preset](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) file `CMakePresets.json` which is a modern way (since CMake 3.19) for specifying build options (instead of hardcoding them in the `CMakeLists.txt` or setting on the command line)

- user should be able to build library both as subproject (probably fetched with CMake's
[FetchContent_MakeAvailable](https://cmake.org/cmake/help/latest/module/FetchContent.html)) and as a stand-alone project
- user should be able to build examples/tests as part of the library build or as a stand-alone projects
- multi-configuration CMake generators should be supported
- package systems (conan, vcpkg and others) should be respected, which means that library `CMakeLists.txt` file should contain
only **build requirements**, i.e. should not hardcode any compiler/linker flags unless they are absolutely required to build
the library

* [CMake Workflow preset](https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html) file `CMakePresets.json` which is a
modern way (since CMake 3.25) for specifying build options (instead of hardcoding them in the `CMakeLists.txt` or setting
on the command line)

* My personal `.clang-format` for code-style worth trying

Note, that I prefer [googletest](https://github.com/google/googletest) as a testing framework so it's the one used in this repository. Still it's not a big deal to replace it with any other of your choice.
* And too `.cmake-format.yml` to format the CMake files

Note, that I prefer [googletest](https://github.com/google/googletest) as a testing framework so it's the one used in this
repository. Still it's not a big deal to replace it with any other of your choice.


## Links

* Anton Pantyukhin - [Modern CMake tutorial for C++ library developers](https://medium.com/@pananton/modern-cmake-tutorial-for-c-library-developers-12f3bf57dc87)
* Dominik Berner - [Organizing CMake presets](https://dominikberner.ch/cmake-presets-best-practices/)
* Craig Scott -[C++20 Modules, CMake, And Shared Libraries](https://crascit.com/category/cmake/)
* [cmake-init - CMake project generator](https://github.com/friendlyanon/cmake-init) that creates good `CMakeLists.tx` files

0 comments on commit 9d446d1

Please sign in to comment.