You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,7 +190,7 @@ The library will be automatically fetched and built as part of your project.
190
190
191
191
#### Installation (optional)
192
192
193
-
Installation is optional and typically not required when using CPM. If you need to install your library (e.g., for system-wide deployment or use with a package manager) use:
193
+
Installation is optional and typically not required when using CPM. If you need to install your library (e.g., for system-wide deployment or use with a package manager):
The `install` preset enables `CPM_USE_LOCAL_PACKAGES`, which makes CPM call `find_package()` first before fetching. This verifies your generated Config.cmake works correctly.
220
+
205
221
For information about using installed packages with `find_package()`, see the [CPM.cmake documentation](https://github.com/cpm-cmake/CPM.cmake) about [controlling how dependencies are found](https://github.com/cpm-cmake/CPM.cmake#cpm_use_local_packages).
206
222
223
+
**Re-exporting dependencies from external packages:**
224
+
225
+
When your library re-exports dependencies (via `INTERFACE` linkage) that come from CPMAddPackage, you must wrap them in `BUILD_INTERFACE` to avoid CMake export errors:
226
+
227
+
```cmake
228
+
# Correct: Wrap CPM-fetched dependencies in BUILD_INTERFACE
**Why this is needed:** CPMAddPackage creates regular (non-IMPORTED) targets in your build tree. When CMake exports your library, it sees these dependencies but cannot include them in your export set (they belong to a different package). The `BUILD_INTERFACE` wrapper tells CMake:
234
+
- Use this dependency during build
235
+
- Don't include it in the export
236
+
- Let consumers find it themselves via `find_dependency()` in the generated Config.cmake
237
+
238
+
cpp-library automatically extracts dependencies from `BUILD_INTERFACE` expressions and generates the appropriate `find_dependency()` calls in your package configuration.
239
+
240
+
**When BUILD_INTERFACE is NOT needed:**
241
+
- Dependencies from `find_package()` (already IMPORTED targets)
242
+
- System libraries like `Threads::Threads`
243
+
- Internal (PRIVATE) dependencies (not applicable to INTERFACE libraries)
244
+
245
+
This is a standard CMake pattern for re-exporting external package dependencies, not specific to cpp-library.
246
+
207
247
#### Dependency Handling in Installed Packages
208
248
209
249
cpp-library automatically generates `find_dependency()` calls in the installed CMake package configuration. Call `cpp_library_enable_dependency_tracking()` before `project()`:
@@ -470,6 +510,8 @@ cpp-library generates a `CMakePresets.json` file with the following configuratio
470
510
471
511
All presets automatically configure `CPM_SOURCE_CACHE` to `${sourceDir}/.cache/cpm` for faster dependency resolution. You can override this by setting the `CPM_SOURCE_CACHE` environment variable.
472
512
513
+
**Best Practice:** Set `CPM_SOURCE_CACHE` in presets or via environment variable/command line, not in CMakeLists.txt. Setting it in CMakeLists.txt with `FORCE` can override parent project settings when used as a subdirectory, and prevents users from configuring it themselves.
0 commit comments