Get linker flags / include directories for a library target

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Get linker flags / include directories for a library target

ax487
Dear community,

I have been wondering on multiple occasions about how to get the
linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
for a library (i.e. a target added to the project using
add_library) within a cmake.

There are many cases in which the flags are required, noticeably
generating a suitable pkg-config file or creating a wrappers around
C/C++ libraries.

I think that it should be possible to derive the flags from
some of the target properties of the library, but I don't
know exactly how.

Could you give me a hand?

ax487

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

Kai Wolf
You can ask a CMake build target for its properties using get_target_property().
For instance, to retrieve the linker flags for a given target foo, you’d write:

    get_target_prooperty(foo_linker_flags foo LINK_FLAGS)

See [1] for a list of valid parameters.

Greetings,

Kai

[1] https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets

http://kai-wolf.me
http://effective-cmake.com

> Am 20.12.2018 um 23:14 schrieb Unknown <[hidden email]>:
>
> Dear community,
>
> I have been wondering on multiple occasions about how to get the
> linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
> for a library (i.e. a target added to the project using
> add_library) within a cmake.
>
> There are many cases in which the flags are required, noticeably
> generating a suitable pkg-config file or creating a wrappers around
> C/C++ libraries.
>
> I think that it should be possible to derive the flags from
> some of the target properties of the library, but I don't
> know exactly how.
>
> Could you give me a hand?
>
> ax487
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

signature.asc (499 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

ax487
Hello Kai,

thanks for your suggestion. I tried the property,
but I just got "foo_linker_flags-NOTFOUND" as a
result (I am using cmake 3.13.1 btw). The
same goes for

LINK_OPTIONS, and
INTERFACE_LINK_OPTIONS.

I tried using the INTERFACE_LINK_LIBRARIES property
instead. The resulting list contains things I can use,
such as /usr/lib/libz.so

However, I am using another cmake imported library.
Therefore, the list contains some libbaz::libbaz
dependency as well.

In order to get the actual list of libraries, I would
have to also resolve these names.

But this has to be done somewhere during the
generation of the Makefiles, so that functionality
must exist somewhere in cmake.


On Fri, 2018-12-21 at 09:33 +0100, Kai Wolf wrote:

> You can ask a CMake build target for its properties using
> get_target_property().
> For instance, to retrieve the linker flags for a given target foo,
> you’d write:
>
>     get_target_prooperty(foo_linker_flags foo LINK_FLAGS)
>
> See [1] for a list of valid parameters.
>
> Greetings,
>
> Kai
>
> [1]
> https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets
>
> http://kai-wolf.me
> http://effective-cmake.com
>
> > Am 20.12.2018 um 23:14 schrieb Unknown <[hidden email]>:
> >
> > Dear community,
> >
> > I have been wondering on multiple occasions about how to get the
> > linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
> > for a library (i.e. a target added to the project using
> > add_library) within a cmake.
> >
> > There are many cases in which the flags are required, noticeably
> > generating a suitable pkg-config file or creating a wrappers around
> > C/C++ libraries.
> >
> > I think that it should be possible to derive the flags from
> > some of the target properties of the library, but I don't
> > know exactly how.
> >
> > Could you give me a hand?
> >
> > ax487
> >
> > --
> >
> > Powered by www.kitware.com
> >
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Kitware offers various services to support the CMake community. For
> > more information on each offering, please visit:
> >
> > CMake Support: http://cmake.org/cmake/help/support.html
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Follow this link to subscribe/unsubscribe:
> > https://cmake.org/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

CMake mailing list
That functionality exists in the C++ side of the code base and is run
during the 'generate' stage  ( CMakeLists are parsed during
'configure', than a 'generate' step is run ).

To get access to how import targets and generator expressions are
resolved you have two options.
1. You can try to use file(GENERATE but it doesn't support all the
generator expressions currently
2. You use cmake-server
(https://cmake.org/cmake/help/latest/manual/cmake-server.7.html) to
query a build directory for all this information.

On Fri, Dec 21, 2018 at 9:12 AM Unknown <[hidden email]> wrote:

>
> Hello Kai,
>
> thanks for your suggestion. I tried the property,
> but I just got "foo_linker_flags-NOTFOUND" as a
> result (I am using cmake 3.13.1 btw). The
> same goes for
>
> LINK_OPTIONS, and
> INTERFACE_LINK_OPTIONS.
>
> I tried using the INTERFACE_LINK_LIBRARIES property
> instead. The resulting list contains things I can use,
> such as /usr/lib/libz.so
>
> However, I am using another cmake imported library.
> Therefore, the list contains some libbaz::libbaz
> dependency as well.
>
> In order to get the actual list of libraries, I would
> have to also resolve these names.
>
> But this has to be done somewhere during the
> generation of the Makefiles, so that functionality
> must exist somewhere in cmake.
>
>
> On Fri, 2018-12-21 at 09:33 +0100, Kai Wolf wrote:
> > You can ask a CMake build target for its properties using
> > get_target_property().
> > For instance, to retrieve the linker flags for a given target foo,
> > you’d write:
> >
> >     get_target_prooperty(foo_linker_flags foo LINK_FLAGS)
> >
> > See [1] for a list of valid parameters.
> >
> > Greetings,
> >
> > Kai
> >
> > [1]
> > https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets
> >
> > http://kai-wolf.me
> > http://effective-cmake.com
> >
> > > Am 20.12.2018 um 23:14 schrieb Unknown <[hidden email]>:
> > >
> > > Dear community,
> > >
> > > I have been wondering on multiple occasions about how to get the
> > > linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
> > > for a library (i.e. a target added to the project using
> > > add_library) within a cmake.
> > >
> > > There are many cases in which the flags are required, noticeably
> > > generating a suitable pkg-config file or creating a wrappers around
> > > C/C++ libraries.
> > >
> > > I think that it should be possible to derive the flags from
> > > some of the target properties of the library, but I don't
> > > know exactly how.
> > >
> > > Could you give me a hand?
> > >
> > > ax487
> > >
> > > --
> > >
> > > Powered by www.kitware.com
> > >
> > > Please keep messages on-topic and check the CMake FAQ at:
> > > http://www.cmake.org/Wiki/CMake_FAQ
> > >
> > > Kitware offers various services to support the CMake community. For
> > > more information on each offering, please visit:
> > >
> > > CMake Support: http://cmake.org/cmake/help/support.html
> > > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > > CMake Training Courses: http://cmake.org/cmake/help/training.html
> > >
> > > Visit other Kitware open-source projects at
> > > http://www.kitware.com/opensource/opensource.html
> > >
> > > Follow this link to subscribe/unsubscribe:
> > > https://cmake.org/mailman/listinfo/cmake
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

Bo Zhou
In reply to this post by ax487
All the flags/options in the CMake they could be considered as a list.

So it's much more easier to handle it as this

set(WIN_DEFINITIONS -DNOMINMAX)

Remember the list is the object, so we could construct the new list for the specific target.

Then to the specific target, just use the 3 commands.

target_compile_options() 
target_include_directories()
target_link_libraries()

We use this method to organize the cross-platform with library and executable without any problem, and super easy to control and manage.

Thanks.


On Fri, Dec 21, 2018 at 7:19 AM Unknown <[hidden email]> wrote:
Dear community,

I have been wondering on multiple occasions about how to get the
linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
for a library (i.e. a target added to the project using
add_library) within a cmake.

There are many cases in which the flags are required, noticeably
generating a suitable pkg-config file or creating a wrappers around
C/C++ libraries.

I think that it should be possible to derive the flags from
some of the target properties of the library, but I don't
know exactly how.

Could you give me a hand?

ax487

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

ax487
In reply to this post by CMake mailing list
I would like to thank all of you for your suggestions. I have gathered
the following (correct me if I am wrong):

- The LINK_FLAGS target property is used in cmake 2.x, the
  INTERFACE_LINK_LIBRARIES in cmake 3.x, the latter contains a ;-list
  of libraries.
- The list may contain generator expressions, it is however
  possible to to use file(GENERATE ..) to obtain evaluate those.
- In any case, the library list contains libraries having different
  formats (see [1]), full paths, (imported) targets, and plain names.
- When Makefiles (or files for other build systems) are generated,
  the list is turned into -L/-l flags used by ld. This happens
  on the C++ side of things, the functionality is not exposed
  to cmake scripts.

As for automatic generation of a pkg-config .pc file, there have
been some inquiries ([2], [3], and [4]), the last one being rather
recent.

The answers point out that pkg-config files can be generated using
configure_file(...), that cmake has its own (imported target) method
for handling dependencies.

I don't mean to be disrespectful or unappreciative of the work put
into cmake (in fact I think it is a vast improvement over automake),
but since there is no way to obtain the required information
programatically, all users of my library have to either use
cmake themselves, or use non-portable workarounds which are prone
to break sooner than later.

If I want to use an external tool (think setup.py) to build
extensions (in-place or not), the same problem occurs.

This is rather disappointing to be honest...

ax487



[1]
https://cmake.org/cmake/help/latest/command/target_link_libraries.html
[2] https://cmake.org/pipermail/cmake/2006-August/010747.html
[3] https://cmake.org/pipermail/cmake/2008-January/019533.html
[4] https://cmake.org/pipermail/cmake/2018-March/067289.html

On Mon, 2018-12-24 at 10:46 -0500, Robert Maynard wrote:

> That functionality exists in the C++ side of the code base and is run
> during the 'generate' stage  ( CMakeLists are parsed during
> 'configure', than a 'generate' step is run ).
>
> To get access to how import targets and generator expressions are
> resolved you have two options.
> 1. You can try to use file(GENERATE but it doesn't support all the
> generator expressions currently
> 2. You use cmake-server
> (https://cmake.org/cmake/help/latest/manual/cmake-server.7.html) to
> query a build directory for all this information.
>
> On Fri, Dec 21, 2018 at 9:12 AM Unknown <[hidden email]> wrote:
> > Hello Kai,
> >
> > thanks for your suggestion. I tried the property,
> > but I just got "foo_linker_flags-NOTFOUND" as a
> > result (I am using cmake 3.13.1 btw). The
> > same goes for
> >
> > LINK_OPTIONS, and
> > INTERFACE_LINK_OPTIONS.
> >
> > I tried using the INTERFACE_LINK_LIBRARIES property
> > instead. The resulting list contains things I can use,
> > such as /usr/lib/libz.so
> >
> > However, I am using another cmake imported library.
> > Therefore, the list contains some libbaz::libbaz
> > dependency as well.
> >
> > In order to get the actual list of libraries, I would
> > have to also resolve these names.
> >
> > But this has to be done somewhere during the
> > generation of the Makefiles, so that functionality
> > must exist somewhere in cmake.
> >
> >
> > On Fri, 2018-12-21 at 09:33 +0100, Kai Wolf wrote:
> > > You can ask a CMake build target for its properties using
> > > get_target_property().
> > > For instance, to retrieve the linker flags for a given target
> > > foo,
> > > you’d write:
> > >
> > >     get_target_prooperty(foo_linker_flags foo LINK_FLAGS)
> > >
> > > See [1] for a list of valid parameters.
> > >
> > > Greetings,
> > >
> > > Kai
> > >
> > > [1]
> > > https://cmake.org/cmake/help/latest/manual/cmake-properties.7.html#properties-on-targets
> > >
> > > http://kai-wolf.me
> > > http://effective-cmake.com
> > >
> > > > Am 20.12.2018 um 23:14 schrieb Unknown <[hidden email]>:
> > > >
> > > > Dear community,
> > > >
> > > > I have been wondering on multiple occasions about how to get
> > > > the
> > > > linker flags (i.e. all -llib -Ldir) and include flags (-Idir)
> > > > for a library (i.e. a target added to the project using
> > > > add_library) within a cmake.
> > > >
> > > > There are many cases in which the flags are required,
> > > > noticeably
> > > > generating a suitable pkg-config file or creating a wrappers
> > > > around
> > > > C/C++ libraries.
> > > >
> > > > I think that it should be possible to derive the flags from
> > > > some of the target properties of the library, but I don't
> > > > know exactly how.
> > > >
> > > > Could you give me a hand?
> > > >
> > > > ax487
> > > >
> > > > --
> > > >
> > > > Powered by www.kitware.com
> > > >
> > > > Please keep messages on-topic and check the CMake FAQ at:
> > > > http://www.cmake.org/Wiki/CMake_FAQ
> > > >
> > > > Kitware offers various services to support the CMake community.
> > > > For
> > > > more information on each offering, please visit:
> > > >
> > > > CMake Support: http://cmake.org/cmake/help/support.html
> > > > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > > > CMake Training Courses:
> > > > http://cmake.org/cmake/help/training.html
> > > >
> > > > Visit other Kitware open-source projects at
> > > > http://www.kitware.com/opensource/opensource.html
> > > >
> > > > Follow this link to subscribe/unsubscribe:
> > > > https://cmake.org/mailman/listinfo/cmake
> >
> > --
> >
> > Powered by www.kitware.com
> >
> > Please keep messages on-topic and check the CMake FAQ at:
> > http://www.cmake.org/Wiki/CMake_FAQ
> >
> > Kitware offers various services to support the CMake community. For
> > more information on each offering, please visit:
> >
> > CMake Support: http://cmake.org/cmake/help/support.html
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html
> > CMake Training Courses: http://cmake.org/cmake/help/training.html
> >
> > Visit other Kitware open-source projects at
> > http://www.kitware.com/opensource/opensource.html
> >
> > Follow this link to subscribe/unsubscribe:
> > https://cmake.org/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

frodak17

On Fri, Dec 28, 2018 at 6:04 PM Unknown <[hidden email]> wrote:
I would like to thank all of you for your suggestions. I have gathered
the following (correct me if I am wrong):

- The LINK_FLAGS target property is used in cmake 2.x, the
  INTERFACE_LINK_LIBRARIES in cmake 3.x, the latter contains a ;-list
  of libraries.
- The list may contain generator expressions, it is however
  possible to to use file(GENERATE ..) to obtain evaluate those.
- In any case, the library list contains libraries having different
  formats (see [1]), full paths, (imported) targets, and plain names.
- When Makefiles (or files for other build systems) are generated,
  the list is turned into -L/-l flags used by ld. This happens
  on the C++ side of things, the functionality is not exposed
  to cmake scripts.

As for automatic generation of a pkg-config .pc file, there have
been some inquiries ([2], [3], and [4]), the last one being rather
recent.

The answers point out that pkg-config files can be generated using
configure_file(...), that cmake has its own (imported target) method
for handling dependencies.

I don't mean to be disrespectful or unappreciative of the work put
into cmake (in fact I think it is a vast improvement over automake),
but since there is no way to obtain the required information
programatically, all users of my library have to either use
cmake themselves, or use non-portable workarounds which are prone
to break sooner than later.

If I want to use an external tool (think setup.py) to build
extensions (in-place or not), the same problem occurs.

This is rather disappointing to be honest...

ax487



The answer to [2] was that the information required to automatically generate a .pc file was not available.
I don't see how CMake could know which packages your library conflicts with or which versions of which libraries are required.
For example a library target can link against an imported target but it won't know that only imported target version 1.0.0 is compatible as opposed to versions >= 1.5.

It seems that you are trying to provide more than how to link against your library but also against everything your library wants to be linked against.
For example the .pc file you want generated contains "Libs: -L${libdirbar} -L${libdirfoo}  -lbar -lfoo".
But that isn't the proper way of a .pc file should reference separate libraries it should use the Requires field.

Also if your library is linking against an imported library `libbaz::libbaz` how is this library being provided to the people using your library?
Are you trying to generate a .pc file for the imported library because it didn't provide one and incorporate the flags into library you are creating?

I think that no one has volunteered to write a CMake package to create .pc files is because generating a .pc file is pretty simple. It's just a template and a configure_file() command.



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Get linker flags / include directories for a library target

ax487
On Sat, 2018-12-29 at 16:06 -0500, frodak17 wrote:

On Fri, Dec 28, 2018 at 6:04 PM Unknown <[hidden email]> wrote:
I would like to thank all of you for your suggestions. I have gathered
the following (correct me if I am wrong):

- The LINK_FLAGS target property is used in cmake 2.x, the
  INTERFACE_LINK_LIBRARIES in cmake 3.x, the latter contains a ;-list
  of libraries.
- The list may contain generator expressions, it is however
  possible to to use file(GENERATE ..) to obtain evaluate those.
- In any case, the library list contains libraries having different
  formats (see [1]), full paths, (imported) targets, and plain names.
- When Makefiles (or files for other build systems) are generated,
  the list is turned into -L/-l flags used by ld. This happens
  on the C++ side of things, the functionality is not exposed
  to cmake scripts.

As for automatic generation of a pkg-config .pc file, there have
been some inquiries ([2], [3], and [4]), the last one being rather
recent.

The answers point out that pkg-config files can be generated using
configure_file(...), that cmake has its own (imported target) method
for handling dependencies.

I don't mean to be disrespectful or unappreciative of the work put
into cmake (in fact I think it is a vast improvement over automake),
but since there is no way to obtain the required information
programatically, all users of my library have to either use
cmake themselves, or use non-portable workarounds which are prone
to break sooner than later.

If I want to use an external tool (think setup.py) to build
extensions (in-place or not), the same problem occurs.

This is rather disappointing to be honest...

ax487



The answer to [2] was that the information required to automatically generate a .pc file was not available.
I don't see how CMake could know which packages your library conflicts with or which versions of which libraries are required.
For example a library target can link against an imported target but it won't know that only imported target version 1.0.0 is compatible as opposed to versions >= 1.5.

It seems that you are trying to provide more than how to link against your library but also against everything your library wants to be linked against.
For example the .pc file you want generated contains "Libs: -L${libdirbar} -L${libdirfoo}  -lbar -lfoo".
But that isn't the proper way of a .pc file should reference separate libraries it should use the Requires field.

Also if your library is linking against an imported library `libbaz::libbaz` how is this library being provided to the people using your library?
Are you trying to generate a .pc file for the imported library because it didn't provide one and incorporate the flags into library you are creating?

I think that no one has volunteered to write a CMake package to create .pc files is because generating a .pc file is pretty simple. It's just a template and a configure_file() command.




Thank you for your reply,

As for the answer provided in [2]: I don't expect conflict or version detection to be conducted automatically. Clearly, you have to require the correct packages in the correct versions,
and you have to pass that information along to a generated pc file, that much is clear. But I think that the same holds true for imported targets, unless something really
clever happens in the background which I am not aware of.

You are quite right in assuming that I want to (mis-)use the Libs / Libs.private field. I would of course prefer the Requires field, but unfortunately a lot of my dependencies don't
generate pc files either, so I don't really see any other way to get pkg-config working.

A dependency of the form libbaz::libbaz would be provided as an imported target. That is, the authors of libbaz are using cmake as well, and they are installing a target.
I am assuming that the users of my library have the dependency libbaz installed as well. I simply want to pass that information along in a format understood by pkg-config.
I would also go through the dependency libraries, but I doubt that any contributions of pc file generators to those projects would be accepted.

Regarding the fact that no one has written a generator for .pc files: It is obviously possible to use configure_file templates. In fact it is quite easy. But it hinges on the libraries as well.
The post you mention references a project which generates linker flags by hand. You see that the CMakeLists.txt contains a lot of lines like this one:

set(PKGCONF_LIBS_PRIV "${PKGCONF_LIBS_PRIV} -lbass -lbassmidi")

This works as long as all dependencies are in a precisely known format, i.e. a list of -l flags. In fact, I think the approach would still work if the authors were to use the built-in pkg-config 
find scripts for their dependencies. But this approach will break down immediately once they add an imported target as a dependency. Note that the authors also use the Libs.private
field instead of the Requires field :)


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake