# Code

**URL:** https://discourse.cmake.org/c/code/11.md?page=47

[Latest](https://discourse.cmake.org/latest.md) · [Categories](https://discourse.cmake.org/categories.md) · [Tags](https://discourse.cmake.org/tags.md)

**Page:** 48

---

## [How to get an LC\_RPATH and @rpath/ prefix on a dylib on macos](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540)

<div class="topic-metadata">

**Author:** [@chris-miner](https://discourse.cmake.org/u/chris-miner)\
**Replies:** 8\
**Last updated:** [April 27, 2022, 5:36pm UTC](https://discourse.cmake.org/t/how-to-get-an-lc-rpath-and-rpath-prefix-on-a-dylib-on-macos/5540 "2022-04-27T17:36:09Z")

</div>

Hi All, I’m trying to solve the apparently common problem on macos of getting cmake to do what I’m doing with install\_name\_tool. The common problem is that I link against 3rd party dylib, and at run-time the dylib is n…

---

## [Can't build 32bit DLL files with Cmake](https://discourse.cmake.org/t/cant-build-32bit-dll-files-with-cmake/5535)

<div class="topic-metadata">

**Author:** [@DeVaughn\_BB](https://discourse.cmake.org/u/DeVaughn_BB)\
**Replies:** 5\
**Last updated:** [April 25, 2022, 11:20pm UTC](https://discourse.cmake.org/t/cant-build-32bit-dll-files-with-cmake/5535 "2022-04-25T23:20:32Z")

</div>

Hi currently I am attempting to build 32bit DLL files using cmake to execute a program however when I execute I only get a new solution file not new 32bit DLL files cmake command ran in command prompt: cmake -H"OptickCo…

---

## [Correct usage of find\_package\_handle\_standard\_args full signature to check REQUIRED VARS](https://discourse.cmake.org/t/correct-usage-of-find-package-handle-standard-args-full-signature-to-check-required-vars/5530)

<div class="topic-metadata">

**Author:** [@Joel](https://discourse.cmake.org/u/Joel)\
**Replies:** 1\
**Last updated:** [April 25, 2022, 11:19pm UTC](https://discourse.cmake.org/t/correct-usage-of-find-package-handle-standard-args-full-signature-to-check-required-vars/5530 "2022-04-25T23:19:20Z")

</div>

Hi, I’m confused by the correct usage of find\_package\_handle\_standard\_args: Lets say I want to write a simple Findmypackage.cmake to find a certain lib that is usually placed in e.g. /opt/mypackage, and has include dir…

---

## [CMAKE\_UNITY\_BUILD and compiled QML files - anonymous namespace collisions](https://discourse.cmake.org/t/cmake-unity-build-and-compiled-qml-files-anonymous-namespace-collisions/5471)

<div class="topic-metadata">

**Author:** [@Rich\_von\_Lehe](https://discourse.cmake.org/u/Rich_von_Lehe)\
**Replies:** 5\
**Last updated:** [April 25, 2022, 4:02pm UTC](https://discourse.cmake.org/t/cmake-unity-build-and-compiled-qml-files-anonymous-namespace-collisions/5471 "2022-04-25T16:02:54Z")

</div>

I found some discussion around problems with anonymous namespaces and using CMAKE\_UNITY\_BUILD from about a year ago \[1\]. I’m using CMake 3.21 and the project runs Qt’s QtQuick compiler over all of the QML source files t…

---

## [Installing/exporting projects that conditionally use optional dependencies](https://discourse.cmake.org/t/installing-exporting-projects-that-conditionally-use-optional-dependencies/5194)

<div class="topic-metadata">

**Author:** [@benthevining](https://discourse.cmake.org/u/benthevining)\
**Replies:** 6\
**Last updated:** [April 25, 2022, 10:12am UTC](https://discourse.cmake.org/t/installing-exporting-projects-that-conditionally-use-optional-dependencies/5194 "2022-04-25T10:12:31Z")

</div>

I’m starting to get the hang of writing install and export rules for my CMake projects, but I’ve got a couple of questions about using external dependencies in projects: First – If I use a custom find module for an exte…

---

## [Inconvenience caused by packages calling 'cmake\_minimum\_required()' when using \`find\_package()\`?](https://discourse.cmake.org/t/inconvenience-caused-by-packages-calling-cmake-minimum-required-when-using-find-package/5498)

<div class="topic-metadata">

**Author:** [@oblivioncth](https://discourse.cmake.org/u/oblivioncth)\
**Replies:** 3\
**Last updated:** [April 22, 2022, 12:02pm UTC](https://discourse.cmake.org/t/inconvenience-caused-by-packages-calling-cmake-minimum-required-when-using-find-package/5498 "2022-04-22T12:02:52Z")

</div>

I tried looking around for discussion related to this topic and was surprised to find none, so I figured I’d post this here. So my initial confusion was caused because I have a script like this: cmake\_minimum\_required(…

---

## [Including SDL2 as a Framework (macOS) help](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481)

<div class="topic-metadata">

**Author:** [@fry](https://discourse.cmake.org/u/fry)\
**Replies:** 5\
**Last updated:** [April 18, 2022, 3:05pm UTC](https://discourse.cmake.org/t/including-sdl2-as-a-framework-macos-help/5481 "2022-04-18T15:05:50Z")

</div>

I have installed the latest version of SDL2 as a framework (downloaded from the sdl2 website). So it appears alongside other frameworks (/Library/Frameworks/SDL2.framework). I have been following the “Linking Frameworks…

---

## [Explicitly link against public/interface dependencies?](https://discourse.cmake.org/t/explicitly-link-against-public-interface-dependencies/5484)

<div class="topic-metadata">

**Author:** [@silverqx](https://discourse.cmake.org/u/silverqx)\
**Replies:** 2\
**Last updated:** [April 18, 2022, 2:05pm UTC](https://discourse.cmake.org/t/explicitly-link-against-public-interface-dependencies/5484 "2022-04-18T14:05:56Z")

</div>

Hi, I want to ask if I should explicitly call the find\_package() and target\_link\_libraries() if they are provided by a library through INTERFACE\_LINK\_LIBRARIES. A short explanation of what I mean :thinking:, I have a sh…

---

## [Header-only library](https://discourse.cmake.org/t/header-only-library/5476)

<div class="topic-metadata">

**Author:** [@exldna](https://discourse.cmake.org/u/exldna)\
**Replies:** 1\
**Last updated:** [April 17, 2022, 11:07pm UTC](https://discourse.cmake.org/t/header-only-library/5476 "2022-04-17T23:07:46Z")

</div>

I have an h-o library named utils. I’m trying to include this library in my project. This is my folder structure: + my\_proj | + source | | + ext | | | + utils | | | | + include | | | | | + utils | | | | | | patterns.hx…

---

## [Is \`CMAKE\_ENABLE\_EXPORTS\` modified when \`WINDOWS\_EXPORT\_ALL\_SYMBOLS\` is set?](https://discourse.cmake.org/t/is-cmake-enable-exports-modified-when-windows-export-all-symbols-is-set/5470)

<div class="topic-metadata">

**Author:** [@ScottB](https://discourse.cmake.org/u/ScottB)\
**Replies:** 3\
**Last updated:** [April 15, 2022, 2:03pm UTC](https://discourse.cmake.org/t/is-cmake-enable-exports-modified-when-windows-export-all-symbols-is-set/5470 "2022-04-15T14:03:26Z")

</div>

When compiling Snappy v1.1.9 as a shared lib, I noticed the implementation library snappy\_test\_support.lib was absent. The main library sets WINDOWS\_EXPORT\_ALL\_SYMBOLS to ON here. But a test support lib has no such mod…

---

## [How to find the problem with an "Invalid preset" cmake message?](https://discourse.cmake.org/t/how-to-find-the-problem-with-an-invalid-preset-cmake-message/5390)

<div class="topic-metadata">

**Author:** [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Replies:** 5\
**Last updated:** [April 13, 2022, 6:21pm UTC](https://discourse.cmake.org/t/how-to-find-the-problem-with-an-invalid-preset-cmake-message/5390 "2022-04-13T18:21:38Z")

</div>

bash-3.2$ jsonschema -i CMakePresets.json -o pretty schemaCMakePrests.json ===\[SUCCESS\]===(CMakePresets.json)=== bash-3.2$ cmake . --list-presets=all CMake Error: Could not read presets from /Users/clausklein/Workspace/…

---

## [Compiler not including /usr/include of QNX sysroot](https://discourse.cmake.org/t/compiler-not-including-usr-include-of-qnx-sysroot/5456)

<div class="topic-metadata">

**Author:** [@Rich\_von\_Lehe](https://discourse.cmake.org/u/Rich_von_Lehe)\
**Replies:** 2\
**Last updated:** [April 12, 2022, 8:58pm UTC](https://discourse.cmake.org/t/compiler-not-including-usr-include-of-qnx-sysroot/5456 "2022-04-12T20:58:59Z")

</div>

I’m wondering if anyone else has had this problem. My compile comands get a very small subset of system include folders added from the QNX sysroot path (i.e. C:/qnx710/target/qnx7). You can see below that only the usr/…

---

## [How to run different add\_custom\_command for each specific target?](https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438)

<div class="topic-metadata">

**Author:** [@Leo\_Abalckin](https://discourse.cmake.org/u/Leo_Abalckin)\
**Replies:** 1\
**Last updated:** [April 12, 2022, 11:50am UTC](https://discourse.cmake.org/t/how-to-run-different-add-custom-command-for-each-specific-target/5438 "2022-04-12T11:50:42Z")

</div>

I work with Visual Studio and have different paths to work for different configurations (Debug, Release): if(WIN32 ) set(static\_release ${my\_lib}\_release/SomeLibrary.lib) set(static\_debug ${my\_lib}\_debug/SomeLib…

---

## [Find package without myLib.cmake](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420)

<div class="topic-metadata">

**Author:** [@Mat\_55](https://discourse.cmake.org/u/Mat_55)\
**Replies:** 2\
**Last updated:** [April 12, 2022, 8:05am UTC](https://discourse.cmake.org/t/find-package-without-mylib-cmake/5420 "2022-04-12T08:05:28Z")

</div>

Hi, I am trying to use libgit2 in my C++ project. I installed the libgit2 library through brew (macOS). The CMake configuration of my project fails with the following message: CMake Warning at CMakeLists.txt:6 (find…

---

## [FindCUDAToolkit unable to find cudart library](https://discourse.cmake.org/t/findcudatoolkit-unable-to-find-cudart-library/5406)

<div class="topic-metadata">

**Author:** [@naglemi](https://discourse.cmake.org/u/naglemi)\
**Replies:** 2\
**Last updated:** [April 11, 2022, 1:29pm UTC](https://discourse.cmake.org/t/findcudatoolkit-unable-to-find-cudart-library/5406 "2022-04-11T13:29:19Z")

</div>

First, a big thank you to the developers and community. My understanding of the fundamentals of cmake is lacking, so I greatly appreciate your time and patience. I am trying to compile a program that comes with a CMakeL…

---

## [install(IMPORTED\_RUNTIME\_ARTIFACTS ...) with a conditionally static/shared dependency](https://discourse.cmake.org/t/install-imported-runtime-artifacts-with-a-conditionally-static-shared-dependency/5384)

<div class="topic-metadata">

**Author:** [@NekkoDroid](https://discourse.cmake.org/u/NekkoDroid)\
**Replies:** 2\
**Last updated:** [April 10, 2022, 4:16pm UTC](https://discourse.cmake.org/t/install-imported-runtime-artifacts-with-a-conditionally-static-shared-dependency/5384 "2022-04-10T16:16:54Z")

</div>

Hey, I was setting up a project with CMake 3.21 (with CMakePresets.json) with one preset that uses system packages and one that uses the vcpkg toolchain (if installed) to make development easy to get in (specifically fo…

---

## [Help / Feature Request: TARGET\_BUNDLE\_DIR\_NAME](https://discourse.cmake.org/t/help-feature-request-target-bundle-dir-name/5374)

<div class="topic-metadata">

**Author:** [@CrouchEndTiger](https://discourse.cmake.org/u/CrouchEndTiger)\
**Replies:** 3\
**Last updated:** [April 10, 2022, 7:43am UTC](https://discourse.cmake.org/t/help-feature-request-target-bundle-dir-name/5374 "2022-04-10T07:43:04Z")

</div>

I’m struggling to find a solution to my problem. It seems like it should be a pretty basic use case of generator expressions, but I can’t find the feature, or any workaround. I’d like to have a generator expression whic…

---

## [How to detect whether the current compiler has mpi support builtin?](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416)

<div class="topic-metadata">

**Author:** [@hzhangxyz](https://discourse.cmake.org/u/hzhangxyz)\
**Replies:** 5\
**Last updated:** [April 9, 2022, 9:19pm UTC](https://discourse.cmake.org/t/how-to-detect-whether-the-current-compiler-has-mpi-support-builtin/5416 "2022-04-09T21:19:08Z")

</div>

I want to check if the given CMAKE\_CXX\_COMPILER has mpi support builtin, such as mpicc, with which there is no need to add include path or link library manually. I used if(CMAKE\_CXX\_COMPILER MATCHES "mpi") before. Howev…

---

## [How to prevent that cmake . --list-presets=all hide presets not usable on my host?](https://discourse.cmake.org/t/how-to-prevent-that-cmake-list-presets-all-hide-presets-not-usable-on-my-host/5395)

<div class="topic-metadata">

**Author:** [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Replies:** 1\
**Last updated:** [April 9, 2022, 12:48pm UTC](https://discourse.cmake.org/t/how-to-prevent-that-cmake-list-presets-all-hide-presets-not-usable-on-my-host/5395 "2022-04-09T12:48:19Z")

</div>

This seems the painfully solution, or not? and cmake presets are not usable (at leased on OSX) · Issue #31 · cpp-best-practices/cpp\_boilerplate\_project · GitHub

---

## [How to do both efficient and correct System Inspection?](https://discourse.cmake.org/t/how-to-do-both-efficient-and-correct-system-inspection/5380)

<div class="topic-metadata">

**Author:** [@frankj](https://discourse.cmake.org/u/frankj)\
**Replies:** 1\
**Last updated:** [April 9, 2022, 12:40pm UTC](https://discourse.cmake.org/t/how-to-do-both-efficient-and-correct-system-inspection/5380 "2022-04-09T12:40:39Z")

</div>

I’m trying to use CMake to do System Inspection as described in “Mastering CMake” (https://cmake.org/cmake/help/book/mastering-cmake/chapter/System%20Inspection.html), and I can’t figure out how to get it to happen if an…

---

## [CMake List of all Project Targets](https://discourse.cmake.org/t/cmake-list-of-all-project-targets/1077)

<div class="topic-metadata">

**Author:** [@developer](https://discourse.cmake.org/u/developer)\
**Replies:** 16\
**Last updated:** [April 7, 2022, 9:18am UTC](https://discourse.cmake.org/t/cmake-list-of-all-project-targets/1077 "2022-04-07T09:18:40Z")

</div>

I have a top level project CMakeLists.txt file with multiple CMakeLists.txt below added with add\_subdirectory that define targets. Is there a way to list all of the targets from my top level project?

---

## [Can I tell what sources / include dirs / targets are PUBLIC vs PRIVATE in target properties?](https://discourse.cmake.org/t/can-i-tell-what-sources-include-dirs-targets-are-public-vs-private-in-target-properties/5393)

<div class="topic-metadata">

**Author:** [@dwarwick](https://discourse.cmake.org/u/dwarwick)\
**Replies:** 1\
**Last updated:** [April 7, 2022, 4:10am UTC](https://discourse.cmake.org/t/can-i-tell-what-sources-include-dirs-targets-are-public-vs-private-in-target-properties/5393 "2022-04-07T04:10:49Z")

</div>

It’s possible to read the sources / include dirs / target dependencies of a target via target properties like SOURCES and LINK\_LIIBRARIES. This doesn’t tell me whether these are PUBLIC or PRIVATE though. Is there a way t…

---

## ["add\_subdirectory twice", with a different environment variable value for each target](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376)

<div class="topic-metadata">

**Author:** [@Prosperoh](https://discourse.cmake.org/u/Prosperoh)\
**Replies:** 7\
**Last updated:** [April 6, 2022, 5:33pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376 "2022-04-06T17:33:49Z")

</div>

Hi all! I have an external submodule that I want to include in my project. In its own CMakeLists.txt it uses the value of an environment variable to define some macros and include some modules. The user is expected to d…

---

## [Figure out the build type on Windows](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295)

<div class="topic-metadata">

**Author:** [@hungptit](https://discourse.cmake.org/u/hungptit)\
**Replies:** 5\
**Last updated:** [April 6, 2022, 4:45pm UTC](https://discourse.cmake.org/t/figure-out-the-build-type-on-windows/5295 "2022-04-06T16:45:49Z")

</div>

I assume this question has been asked somewhere on the Web, but I could not find anything usable for my use cases. What is the proper way to handle this logic on all platforms? if (CMAKE\_BUILD\_TYPE STREQUAL "Debug") s…

---

## [try\_compile does not honor own variables](https://discourse.cmake.org/t/try-compile-does-not-honor-own-variables/5386)

<div class="topic-metadata">

**Author:** [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Replies:** 3\
**Last updated:** [April 6, 2022, 2:09pm UTC](https://discourse.cmake.org/t/try-compile-does-not-honor-own-variables/5386 "2022-04-06T14:09:56Z")

</div>

Hi, I have a toolchain file that sets a custom CMAKE\_SYSTEM\_NAME. I also have the Platform/-Files that I need. The problem is that try\_compile() cannot see those. I tried: cmake\_minimum\_required(VERSION 3.22) set ( C…

---

## [target\_include\_directories(mylib SYSTEM ...) is exported without \_IMPORT\_PREFIX?](https://discourse.cmake.org/t/target-include-directories-mylib-system-is-exported-without-import-prefix/5359)

<div class="topic-metadata">

**Author:** [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Replies:** 3\
**Last updated:** [April 5, 2022, 6:30pm UTC](https://discourse.cmake.org/t/target-include-directories-mylib-system-is-exported-without-import-prefix/5359 "2022-04-05T18:30:33Z")

</div>

This CMakeLists.txt snipped: # to get CMAKE\_INSTALL\_INCLUDEDIR include(GNUInstallDirs) # target\_include\_directories with the SYSTEM modifier will request the compiler # to omit warnings from the provided paths, if the …

---

## [ctest --preset=ninja-multi --config Release does not work?](https://discourse.cmake.org/t/ctest-preset-ninja-multi-config-release-does-not-work/5378)

<div class="topic-metadata">

**Author:** [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Replies:** 4\
**Last updated:** [April 5, 2022, 5:57pm UTC](https://discourse.cmake.org/t/ctest-preset-ninja-multi-config-release-does-not-work/5378 "2022-04-05T17:57:20Z")

</div>

bash-3.2$ ctest --preset=ninja-multi --config Release Test project /Users/clausklein/Workspace/cpp/ModernCmakeStarter/build/ninja-multi Start 1: GreeterTests Test not available without configuration. (Missing "-C \<…

---

## [How to compile same file in same target with different macro?](https://discourse.cmake.org/t/how-to-compile-same-file-in-same-target-with-different-macro/5372)

<div class="topic-metadata">

**Author:** [@hzhangxyz](https://discourse.cmake.org/u/hzhangxyz)\
**Replies:** 1\
**Last updated:** [April 5, 2022, 1:19pm UTC](https://discourse.cmake.org/t/how-to-compile-same-file-in-same-target-with-different-macro/5372 "2022-04-05T13:19:11Z")

</div>

I have a c++ library, to build it, I need to compile the same file several time with different macro. How to do it? It seems i can create object library several times with different name to do it. But i need manually ad…

---

## [How to make a subproject to be inside a project?](https://discourse.cmake.org/t/how-to-make-a-subproject-to-be-inside-a-project/5365)

<div class="topic-metadata">

**Author:** [@kvpfs](https://discourse.cmake.org/u/kvpfs)\
**Replies:** 1\
**Last updated:** [April 4, 2022, 3:22pm UTC](https://discourse.cmake.org/t/how-to-make-a-subproject-to-be-inside-a-project/5365 "2022-04-04T15:22:17Z")

</div>

Hello. Let’s imagine a situation - there is a main project MainPrj and a subroject SubPrj, which is fetched as git submodule. SubPrj is self-sufficient cmake project with its own install target. MainPrj is mine, SubPrj i…

---

## [Escape \`\\\` for use in \`STREQUAL\`?](https://discourse.cmake.org/t/escape-for-use-in-strequal/5362)

<div class="topic-metadata">

**Author:** [@SamuelMarks](https://discourse.cmake.org/u/SamuelMarks)\
**Replies:** 7\
**Last updated:** [April 4, 2022, 1:25am UTC](https://discourse.cmake.org/t/escape-for-use-in-strequal/5362 "2022-04-04T01:25:19Z")

</div>

I am trying to create a header/source from a header-only C library. To do this I need to parse C code to figure out what parts to extract out. CMake as a language seems incredibly verbose, but I am making progress: W…

[Previous page](https://discourse.cmake.org/c/code/11.md?page=46)

[Next page](https://discourse.cmake.org/c/code/11.md?page=48)
