[CMake] Fwd: Removing overkill linking

Paul McEnery pmcenery at gmail.com
Sun Oct 3 06:04:30 EDT 2010


On 2 October 2010 23:19, Andreas Pakulat <apaku at gmx.de> wrote:
[...]
>
> Thats because a link-interface for an executable doesn't make the
> slightest sense. The link-interface in cmake for a library target foo,
> defines which libraries should automatically get into the linker-call
> when linking some other target against foo. As one doesn't (except in
> very very rare cases) link against executable, there's no point in
> defining a link-interface.
>
> To reduce excessive linking for libraries, you need to set the
> target-property "LINK_INTERFACE_LIBRARIES" to an empty string and
> afterwards use target-link-libraries( foo LINK_INTERFACE_LIBRARIES )
> with those libs which are present in the public API of foo. Of course
> all this only works as long as you're inside a cmake project or if using
> imported cmake targets. Otherwise you have to rely on the responsive
> FindXYZ.cmake files not adding additional, unnecessary libs to the
> linker line (or whatever the find-module uses, like pkg-config files).
>

Thanks Andreas.

I now understand. I have removed LINK_INTERFACE_LIBRARIES, and started
investigating where the excessive linking is coming in. It appears
that pkg-config is the culprit. For example:

$ pkg-config --libs gtk+-2.0
-pthread -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lpangoft2-1.0
-lgdk_pixbuf-2.0 -lm -lpangocairo-1.0 -lcairo -lgio-2.0 -lpango-1.0
-lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lrt
-lglib-2.0

The resulting executable is unnecessarily linked against libm,
libfontconfig, etc.

I cant see any way to limit the way that pkg-config includes
libraries. One solution appears to be to write Find* modules. Is this
the way forward? Here is what I currently have:

=====================
find_package      (PkgConfig)
pkg_check_modules (DBUS       REQUIRED   dbus-1>=1.2.12)
pkg_check_modules (DBUS-GLIB  REQUIRED   dbus-glib-1>=0.80)
pkg_check_modules (GLADE      REQUIRED   libglade-2.0>=2.6.4)
pkg_check_modules (GTK2       REQUIRED   gtk+-2.0>=2.16.1)
pkg_check_modules (HAL        REQUIRED   hal>=0.5.12)
=====================

Many thanks,
Paul.


More information about the CMake mailing list