

I am trying to understand how to add COMPONENTs to a CMake library so that I can list them in the find_package command of a consuming application and I can’t seem to find a clear idiomatic way of how to do this. I’ll include those as-is for now in case it’s useful. You can change the default component name just before you pull in that dependency though using the CMAKE_INSTALL_DEFAULT_COMPONENT_NAME variable (mentioned on page 325 of my book).ĭeniz Bahadir’s response is harder to quote as he provided updated commands of an example CMakeLists.txt file I provided. with FetchContent) and that dependency doesn’t define install components. It often occurs when a third party dependency is brought into your build directly (e.g. That usually means you’ve got something being installed without any component specified. Where possible, you should try to avoid having your project put things into the “Unspecified” component. I don’t recall if you can provide the -component option more than once or give it a list of components. I rarely find a need to use cmake -install. Sorry if that wasn’t clear and it seems like I was suggesting that cmake -install should somehow be used within the project itself. Define your package contents and install components within your project and let CPack take care of turning all that into a final package for you. You don’t need it for producing a package and shouldn’t try to do so.
#CMAKE INSTALL MULTIPLE DESTINATIONS MANUAL#
The cmake -install command is meant only if you want to do a manual install of the project or individual components locally. I used fairly poor wording in my initial reply (the result of various edits before sending the final version). It would certainly make it easier to do so (for the person maintaining the project). Yes they are different things, but it would not be unusual for the package components to line up fairly closely to the install components. Maybe there’s a workaround for this I’m not sure, or you just have to run the command multiple times. One hitch I found was you can only install one component at a time and the Unspecified component can’t be installed at the same time as a more specific one (a common component as it were). In the example I linked in my first email I do now have things working and have locally experimented with the adding the COMPONENT keyword and using cmake -install build -component.

There’s another interesting talk called ‘Oh No! More Modern CMake’ that recommends this but unfortunately doesn’t show exactly how to set it up. The thing I was interested in doing was what Qt and Boost do by allowing users to list the components they want in their find_package command.

These appear to be entirely orthogonal concepts but I’d assumed they were tightly related. I think my confusion stemmed from the COMPONENT keyword in the install command and the COMPONENTS keyword in the find_package command. My website has a link to that talk and accompanying slides here: The middle section talks a fair bit about install components and builds up an example with motivation for why you might put parts of a library in different components. In the next chapter on packaging, Section 26.2 " Components " covers how components in much more detail, covering how relationships between components can be expressed and how to use that for package generation.įor a more arm-chair coverage of this area, you might also want to watch my CppCon 2019 talk “Deep CMake For Library Authors”. Referring to the 5th edition of my book, Section 25.2 " Installing Targets " explains what components are and gives an example of a typical usage (starting near the bottom of page 324). Some package generators also support a hierarchy of components through grouping, which is how you get things like optional install components in some GUI installers. with the "cmake -install -component " command), or to create multiple packages based around those components. These can be used to selectively pull in just a subset of available things into a package (e.g. The COMPONENT keyword allows you to group installed things into separate “bags” that you can then use to refer to them later. I wanted to use if for specifying components in the CMake find_package command, but I found it actually isn’t necessary at all and you can simply use careful naming of -config.cmake files. I did a bunch of digging but can’t seem to find what it does. I am trying to understand what the COMPONENT keyword is in the CMake install command. In case anyone else might find this useful I’m including the conversations here so it’s not just for my benefit. After watching excellent talks by both Craig Scott and Deniz Bahadir I reached out to them directly via e-mail and got some very helpful responses. I recently had a number of questions around the CMake COMPONENT keyword used in the CMake install command and using COMPONENTS in the CMake find_package command.
