# How to create pkgconfig file

**URL:** https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337
**Category:** Code
**Created:** [March 9, 2024, 4:27pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337 "2024-03-09T16:27:05Z")
**Posts on this page:** 15
**Page:** 2

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 10, 2024, 6:25pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/21 "2024-03-10T18:25:19Z")

</div>

My actual library has something like 10 optional dependencies depending on cmake options and the return values of find\_package.

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 10, 2024, 6:26pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/22 "2024-03-10T18:26:25Z")

</div>

Basically my first solution works provided you never use generator expressions or imported targets. But that’s not modern cmake

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [March 10, 2024, 6:29pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/23 "2024-03-10T18:29:58Z")

</div>

> [@Peter\_Featherstone](#):
>
> What if libpng is an optional dependency in my library controlled by a cmake option ? That’s my use case.

OK, that should also a CMake variable:  
[PNG\_LIBRARIES](https://cmake.org/cmake/help/latest/module/FindPNG.html#result-variables)

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 10, 2024, 6:43pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/24 "2024-03-10T18:43:06Z")

</div>

Yep. That’s the only solution I can find. Not very modern cmake though

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [March 10, 2024, 7:09pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/25 "2024-03-10T19:09:48Z")

</div>

> [@Peter\_Featherstone](#):
>
> Not very modern cmake though

You will have the same problems when exporting a `CMake config package`:

you have to write the `find_dependency()` calls.

see [Installed module library can't find imported headers [Ubuntu 22.04 LTS, CMake 3.28.1, Ninja 1.11.1 and Clang 17.0.6] - #18 by ben.boeckel](https://discourse.cmake.org/t/installed-module-library-cant-find-imported-headers-ubuntu-22-04-lts-cmake-3-28-1-ninja-1-11-1-and-clang-17-0-6/9819/18)

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 10, 2024, 8:00pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/26 "2024-03-10T20:00:24Z")

</div>

Yep it’s pants.  
But at least it’s doable.  
I’m starting to believe cmake doesn’t solve anything. As in, it doesn’t actually make you productive. But anyway, it’s the best we got unfortunately

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [March 11, 2024, 6:16am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/27 "2024-03-11T06:16:16Z")

</div>

Everything is possible.

Perhaps you should try the [project\_options](https://github.com/ClausKlein/project_options/pull/6)  
You may write a patch to export a `pgk-config` files too?

## Es ist nicht genug zu wissen, man muß auch anwenden; es ist nicht genug zu wollen, man muß auch tun.

[Johann Wolfgang von Goethe](https://www.aphorismen.de/autoren/person/1461/Johann%20Wolfgang%20von%20Goethe) (1749 - 1832)

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 11, 2024, 7:13am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/28 "2024-03-11T07:13:22Z")

</div>

To be honest, the amount of time I have to spend writing and debugging cmake it’s barely worth it. New languages put a lot of effort in their build tools to minimise the amounting of time spent writing build code.  
I might have a look at Meson this weekend and see how they tackle dependencies and install scripts.  
But anyway, thank you for all your help

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [March 11, 2024, 8:27am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/29 "2024-03-11T08:27:05Z")

</div>

- [GitHub - boost-cmake/bcm: Boost cmake modules](https://github.com/boost-cmake/bcm)
- [GitHub - robotology/ycm: YCM (YCM CMake modules) is a collection of various useful CMake modules.](https://github.com/robotology/ycm)

provide some wrappers that automatically generate pkgConfig files. I haven’t tried them myself but could be a good resource.

---

<div class="post-metadata">

### Author: ![mmomtchev](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmomtchev/32/4465_2.png) [@mmomtchev](https://discourse.cmake.org/u/mmomtchev)
#### Post date: [April 4, 2024, 1:29pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/30 "2024-04-04T13:29:38Z")

</div>

Peter, did you find a solution, I have exactly the same problem as you - a huge number of dependencies, could be shared, could be static, could be coming from conan, could be coming form the system

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [April 8, 2024, 2:45pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/31 "2024-04-08T14:45:07Z")

</div>

No I didn’t sorry. I haven’t tried [GitHub - boost-cmake/bcm: Boost cmake modules](https://github.com/boost-cmake/bcm) or [GitHub - robotology/ycm: YCM (YCM CMake modules) is a collection of various useful CMake modules.](https://github.com/robotology/ycm) but they seemed to suggest they provided a solution to this. They are old and unmaintained however.

---

<div class="post-metadata">

### Author: ![petk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/petk/32/4555_2.png) [@petk](https://discourse.cmake.org/u/petk)
#### Post date: [May 3, 2024, 6:13am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/32 "2024-05-03T06:13:49Z")

</div>

I’m experiencing same issue. The generator expressions of all those linked libraries and conditions make it impossible to do that from reading some target property.

I think this is the way to go in CMake’s case but seems to be stalled at the time of writing: [https://gitlab.kitware.com/cmake/cmake/-/issues/22621](https://gitlab.kitware.com/cmake/cmake/-/issues/22621)

Otherwise, I’ll probably have to collect these private libraries into a list variable over the configuration phase. Yes, quite annoying approach and feels like moving few steps back with all these imported and interface targets, but is probably the least error prone.

I’ve also tried parsing the CMakeFiles/\<target\_name\>.dir/link.txt file but it would be a bit difficult to get all libraries based on the various linkers and combinations correctly manually.

---

<div class="post-metadata">

### Author: ![traversaro](https://discourse.cmake.org/user_avatar/discourse.cmake.org/traversaro/32/546_2.png) [@traversaro](https://discourse.cmake.org/u/traversaro)
#### Post date: [May 17, 2024, 9:46am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/33 "2024-05-17T09:46:00Z")

</div>

Hello @Peter_Featherstone , `ycm-cmake-modules` maintainer here.

Just to understand, why do you state that it is unmaintained?

However, more in topic, in `ycm-cmake-modules` we do not provide any functionality to generate pkg-config’s `.pc` files.

---

<div class="post-metadata">

### Author: ![Peter\_Featherstone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/peter_featherstone/32/4279_2.png) [@Peter\_Featherstone](https://discourse.cmake.org/u/Peter_Featherstone)
#### Post date: [May 17, 2024, 10:02am UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/34 "2024-05-17T10:02:29Z")

</div>

Ah sorry, I was talking about BCM. That’s definitely old and unmaintained.

---

<div class="post-metadata">

### Author: ![Someone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/someone/32/3271_2.png) [@Someone](https://discourse.cmake.org/u/Someone)
#### Post date: [June 17, 2024, 7:02pm UTC](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337/35 "2024-06-17T19:02:00Z")

</div>

Hi! I think the original post is fairly clear in asking specifically about using `get_target_property` to generate a pkg-config file and running into the issue of the generator expressions not having been expanded at the time the file is generated.

My impression is that the original request implies that targets’ properties already implement some complex logic concerned with the includes, and flags, and dependencies, etc, and the author must wish to reuse that logic when generating the `.pc` export. It is a valid use-case to use CMake to generate package info consumable by non-CMake projects. I’m afraid your references to the docopt example, and `man pkg-config`, to `find_package` may have pulled the conversation in a completely different direction and possibly created some… tension.

On the subject matter, I don’t see any `file(GENERATE ...)` in [cmake-pcfilegenerator/PcFileGenerator.cmake at main · antoniovazquezblanco/cmake-pcfilegenerator · GitHub](https://github.com/antoniovazquezblanco/cmake-pcfilegenerator/blob/main/PcFileGenerator.cmake) (from [CMake module to automatically generate pkg-config PC files from targets](https://discourse.cmake.org/t/cmake-module-to-automatically-generate-pkg-config-pc-files-from-targets/8260)) but I do see a `get_target_property`, I wonder how that works.

[Previous page](https://discourse.cmake.org/t/how-to-create-pkgconfig-file/10337.md?page=1)
