Last active
August 25, 2025 15:34
-
-
Save scivision/ed109f7a1256141b3e821e2a82ec17f1 to your computer and use it in GitHub Desktop.
CMake pkg-config .pc.in generate template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this fragment generates build/my_package.pc | |
# | |
# cmake -B build -DCMAKE_INSTALL_PREFIX=~/mylib | |
cmake_minimum_required(VERSION 3.0) | |
project(mylib | |
LANGUAGES C | |
HOMEPAGE_URL https://github.invalid/username/mylib | |
DESCRIPTION "example library" | |
VERSION 1.1.2) | |
add_library(mine mine.c) | |
add_library(support support.c) | |
set(target1 mine) | |
set(target2 support) | |
set(pc_libs_private) | |
set(pc_req_private) | |
set(pc_req_public) | |
configure_file(my_package.pc.in my_package.pc @ONLY) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this template is filled-in by CMake `configure_file(... @ONLY)` | |
# the `@....@` are filled in by CMake configure_file(), | |
# from variables set in your CMakeLists.txt or by CMake itself | |
# | |
# Good tutoral for understanding .pc files: | |
# https://people.freedesktop.org/~dbn/pkg-config-guide.html | |
prefix="@CMAKE_INSTALL_PREFIX@" | |
exec_prefix="${prefix}" | |
libdir="${prefix}/lib" | |
includedir="${prefix}/include" | |
Name: @PROJECT_NAME@ | |
Description: @CMAKE_PROJECT_DESCRIPTION@ | |
URL: @CMAKE_PROJECT_HOMEPAGE_URL@ | |
Version: @PROJECT_VERSION@ | |
Requires: @pc_req_public@ | |
Requires.private: @pc_req_private@ | |
Cflags: -I"${includedir}" | |
Libs: -L"${libdir}" -l@target1@ -l@target2@ | |
Libs.private: -L"${libdir}" -l@target1@ -l@target2@ @pc_libs_private@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Additional note on the subject: when users use
cmake --install .
with a--prefix
, this pkg-config file template will have the wrongCMAKE_INSTALL_PREFIX
-> here's a discussion (with one solution) on that: https://discourse.cmake.org/t/how-to-generate-pc-pkg-config-file-supporting-prefix-of-the-cmake-install/4109