pkg-config search path: PKG_CONFIG_PATH vs. PKG_CONFIG_LIBDIR

pkg-config is a very convenient tool for auto-configuring builds. The documentation includes a manual page and a guide.

When I first needed pkg-config in a real build script, which was a cross-build, I needed to set the search path to the cross libraries and be sure none of the host libraries are referred in the build.

Dilema

The pkg-config behaviour is controlled by several environment variables, among them being:

PKG_CONFIG_PATH

A colon-separated (on Windows, semicolon-separated) list of directories to search for .pc files. The default directory will always be searched after searching the path; the default is libdir/pkgconfig:datadir/pkgconfig where libdir is the libdir where pkg-config and datadir is the datadir where pkg-config was installed.

PKG_CONFIG_LIBDIR

Replaces the default pkg-config search directory.

Source code

Since for me the above phrasing was somehow confusing, to fully clarify things, I took a look at the source code:

 search_path = getenv ("PKG_CONFIG_PATH");
 if (search_path) 
   {
     add_search_dirs(search_path, G_SEARCHPATH_SEPARATOR_S);
   }
 if (getenv("PKG_CONFIG_LIBDIR") != NULL) 
   {
     add_search_dirs(getenv("PKG_CONFIG_LIBDIR"), G_SEARCHPATH_SEPARATOR_S);
   }
 else
   {
     add_search_dirs(pkg_config_pc_path, G_SEARCHPATH_SEPARATOR_S);
   }

The behaviour is obvious now, PKG_CONFIG_PATH is always added in front of any other libraries searched and PKG_CONFIG_LIBDIR, when defined, fully replaces the default system path.

Conclusion

So, for cross-builds, as it was my case, it is mandatory to use PKG_CONFIG_LIBDIR to be sure no references to system packages are inserted into the build.

 

Speak Your Mind

*