I am trying to generate a pkg-config (.pc) file in cmake for my library.
This requires finding the public compile flags and linker flags.
I’m currently using
Then I use configure_file using the following .in file
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: @PROJECT_NAME@
Description: My special library
Version: @VERSION@
Libs: -L${libdir} -lmylib @MYTARGET_LINK_LIBRARIES@
Cflags: -I${includedir} @MYTARGET_LINK_LIBRARIES@
My problem is MYTARGET_LINK_LIBRARIES and MYTARGET_LINK_LIBRARIES still have generator expressions and MYTARGET_LINK_LIBRARIES references imported targets of the form blah::blah which of course pkg-config doesn’t understand.
How do I resolve the full linker and compiler options?
It would also be really useful when generating cmake config files at installation to avoid having to use things like find_dependency and installing custom cmake module files.
So the problem is with pkgConfig files not cmake config files, specifically when using generator expressions and imported targets. Neither configure_file or install() resolves the generator expressions or converts imported targets like blah::blah down to compile options and linker options like -I and -L where blah::blah is a dependency of the library you’re trying to install
METADATA FILE SYNTAX
To add a library to the set of packages pkg-config knows about, simply install a .pc file. You should install
this file to libdir/pkgconfig.
Here is an example file:
# This is a comment
prefix=/home/hp/unst # this defines a variable
exec_prefix=${prefix} # defining another variable in terms of the first
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: GObject # human-readable name
Description: Object/type system for GLib # human-readable description
Version: 1.3.1
URL: http://www.gtk.org
Requires: glib-2.0 = 1.3.1
Conflicts: foobar <= 4.5
Libs: -L${libdir} -lgobject-1.3
Libs.private: -lm
Cflags: -I${includedir}/glib-2.0 -I${libdir}/glib/include
Precisely so if my library has dependencies which have been linked like so: target_link_libraries(my_library X11::X11 JPEG::JPEG PNG::PNG CUDA::cublas CUDA::curand MKL::MKL ...)
how do i populate Libs and Cflags in the pkg config file?
As i wrote in my original post i tried using:
which of course makes no sense for pkg config.
So i need a way to resolve all the imported targets, generator expressions etc down to explicit compiler and linker flags
At the end of the day, cmake should know how to do this because it has to generate makefiles build-ninja files etc which must use explicit compiler/linker flags. I just wish there was a way to expose this functionality at configure time.
What if libpng is an optional dependency in my library controlled by a cmake option ? That’s my use case. So you can’t guarantee it’s always a dependency.