How can I exclude some library paths listed in " pkg-config --variable pc_path pkg-config"?

7,193

Stuff in /usr/local usually supersedes stuff in /usr, so I'm a bit confused as to why you would install libraries there to have a "a nice custom distro", but then not want to compile against them. Those are the libraries the system will use actually use.

Anyway, man pkg-config claims the base search path:

is libdir/pkgconfig:datadir/pkgconfig where libdir is the libdir for pkg-config and datadir is the datadir for pkg-config when it was installed.

This implies they are compiled in. I notice it is different on ubuntu than fedora -- the former is long and inclusive, whereas the latter is short and exclusive; on fedora I have to set a $PKG_CONFIG_PATH to include /usr/local.

Since paths in $PKG_CONFIG_PATH are checked first, you could just set:

PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig

The fact that these are at the end of the built-in paths won't matter; if the check makes it to there without finding anything, there's nothing to be found.


To demonstrate how this works, create a temporary directory /opt/bs/pkg and copy a .pc file from one of the directories in the default path into it -- e.g., alsa.pc. First check;

> pkg-config --libs alsa
-lasound

Now go into /opt/bs/pkg/alsa.pc and change -lasound (it's in the Libs: field) to -foobar. Set $PKG_CONFIG_PATH and try again:

> PKG_CONFIG_PATH=/opt/bs/pkg pkg-config --libs alsa
-foobar

Eureka, $PKG_CONFIG_PATH has overridden the built-in paths...you can delete /opt/bs/pkg, of course.

Share:
7,193
kenn
Author by

kenn

Updated on September 18, 2022

Comments

  • kenn
    kenn over 1 year

    I have Ubuntu 14.04 upgraded from 12.04 making dist-upgrades. I did many manual installations such as ffmpeg, libglib and so on, in the past. I have a nice custom distro now, it works well but I have problems while trying to compile applications. It stems from library conflicts between manually installed packages from source code and native distro libraries. A guy advised me to rename /usr/local it works but boot failed on next reboot.

    When I look for directories added by pkg-config with

      pkg-config --variable pc_path pkg-config
    

    it lists

     /usr/local/lib/i386-linux-gnu/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/share/pkgconfig:/usr/lib/i386-linux-gnu/pkgconfig:/usr/lib/pkgconfig:/usr/share/pkgconfig
    

    I don't want it to look for paths in /usr/local/lib...

    How can I ban those paths not to let pkg-config look for?

  • goldilocks
    goldilocks almost 10 years
    Right -- you haven't changed the default path, but paths in $PKG_CONFIG_PATH are checked first. After you set it, you can check by calling, e.g. pkg-config --libs somepackage where "somepackage" has a .pc file in multiple places.
  • kenn
    kenn almost 10 years
    manpage says pkg-config retrieves information about packages from special metadata files. These files are named after the package, and has a .pc extension. On most systems, pkg-config looks in /usr/lib/pkgconfig, /usr/share/pkgconfig, /usr/local/lib/pkgconfig and /usr/local/share/pkgconfig for these files. It will additionally look in the colon-separated (on Windows, semicolon-separated) list of directories speci‐ fied by the PKG_CONFIG_PATH environment variable. so isn't there any way to exclude /usr/local/share/pkgconfig ?
  • goldilocks
    goldilocks almost 10 years
    No, but it doesn't matter if you prioritize the paths you want to prioritize. I've added an example of what I'm talking about to demonstrate that using $PKG_CONFIG_PATH works.
  • kenn
    kenn almost 10 years
    let me test your method again, I got library error while compiling this github.com/MaartenBaert/ssr.git I hope it works, I believe I tried your method and it didn't work
  • goldilocks
    goldilocks almost 10 years
    It works to do what you've asked for in the question, so if you use it properly and have errors compiling something, the problem isn't to do with what you've asked about.
  • kenn
    kenn almost 10 years
    Unfortunately it doesn't work. It's permissive and allows looking for libraries in usr/local/... I think only way to achieve what I want is to rename pkgconfig directories in usr/local as a workaround
  • goldilocks
    goldilocks almost 10 years
    Okay, but the only way it will find those is if the libraries are not installed anywhere else. In this case your choices are: 1) Use version from /usr/local. 2) Do not use the library at all, in which case, if they are required, ./configure will bail out. If it's choosing a .pc file from /usr/local when there is one in /usr then you did not export $PKG_CONFIG_PATH properly. End of story. There is no ambiguity about this.