Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active February 16, 2024 11:09
Show Gist options
  • Save scivision/ed109f7a1256141b3e821e2a82ec17f1 to your computer and use it in GitHub Desktop.
Save scivision/ed109f7a1256141b3e821e2a82ec17f1 to your computer and use it in GitHub Desktop.
CMake pkg-config .pc.in generate template
# 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 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@
@jrwrigh
Copy link

jrwrigh commented Apr 28, 2023

As a suggestion, should libdir be replaced by:

libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@

?

That way the pkgconfig always matches up with whatever GNUInstallDirs chooses (which can be different sometimes)?
Similar substitutions could also be made to includedir. See sewenew/redis-plus-plus#474 as reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment