colcon-pkg-config
colcon-pkg-config is a colcon extension that provides an environment hook to correctly set the `PKG_CONFIG_PATH` during `colcon build` operations. It ensures that `share/pkgconfig` directories of previously installed packages within the workspace are added to `PKG_CONFIG_PATH`, making `pkg-config` files discoverable for subsequent builds. The current version is 0.1.0, and its release cadence is infrequent, typically aligning with colcon core developments or specific ROS releases.
Common errors
-
Package 'my_lib' not found
cause During a `colcon build`, `pkg-config` failed to locate the `.pc` file for 'my_lib'. This often means the package providing 'my_lib' was not built/installed yet, `colcon-pkg-config` is not active, or `pkg-config` cannot find the `install/share/pkgconfig` directory.fixVerify that the package providing 'my_lib' has been successfully built and installed (or symlinked) *before* the package attempting to use it. Confirm `colcon-pkg-config` is installed correctly in the same environment as `colcon`. Ensure `pkg-config` itself is installed on your system. -
CMake Error at /usr/share/cmake-X.Y/Modules/FindPkgConfig.cmake:NNN (message): A required package was not found.
cause This is a common CMake error indicating that `pkg-config` (which CMake uses via `FindPkgConfig`) failed to find a required dependency's `.pc` file. This is the underlying CMake manifestation of the 'Package not found' issue.fixAs above, check the build order of your packages, ensure `colcon-pkg-config` is properly installed, and confirm `pkg-config` is available on your system. Sometimes cleaning and rebuilding the workspace (`colcon build --merge-install --cmake-args -DCMAKE_BUILD_TYPE=Release`) can resolve transient issues.
Warnings
- gotcha colcon-pkg-config must be installed in the same Python environment (e.g., virtual environment or system Python) as your colcon-core installation for it to be discovered and activated. If they are in separate environments, the extension will not function.
- gotcha This extension only augments the `PKG_CONFIG_PATH` environment variable during the `colcon build` and `colcon install` steps. It does not ensure `pkg-config` itself is installed or in your system's PATH. If `pkg-config` is missing, you will still encounter errors.
- gotcha When manually setting `PKG_CONFIG_PATH` in your shell, be aware that colcon-pkg-config will prepend workspace-specific paths. Depending on the order of precedence and how `pkg-config` resolves paths, your manual settings might be ignored or interfere with the automated ones. It's generally best to let colcon manage this path for workspace-internal dependencies.
Install
-
pip install colcon-pkg-config
Quickstart
# colcon-pkg-config is a colcon extension and does not provide direct Python APIs for userland code.
# Its functionality is automatically integrated into the 'colcon build' process once installed.
# To use:
# 1. Install colcon-pkg-config in the same Python environment as colcon-core:
# pip install colcon-pkg-config
# 2. Set up a colcon workspace with packages that produce and consume .pc files.
# For example, 'pkg_a' installs 'libpkg_a.pc' and 'pkg_b' uses 'pkg-config --cflags --libs libpkg_a'.
# mkdir -p my_colcon_ws/src/pkg_a
# mkdir -p my_colcon_ws/src/pkg_b
# # (populate pkg_a's CMakeLists.txt to install a .pc file to share/pkgconfig)
# # (populate pkg_b's CMakeLists.txt to use find_package(PkgConfig) and pkg_check_modules(libpkg_a ...))
# 3. Build your colcon workspace from the root of 'my_colcon_ws':
# import os
# if os.path.exists('src'): # Simulate running in a colcon workspace
# print('Building colcon workspace. colcon-pkg-config will automatically augment PKG_CONFIG_PATH.')
# # This conceptual command would be run in a shell:
# # os.system('colcon build --symlink-install')
# else:
# print('Please run this conceptual quickstart within a colcon workspace root.')
# The extension ensures that pkg_b correctly finds pkg_a's .pc file without manual PKG_CONFIG_PATH setup.