Ship a Library With a pkg-config .pc File
Write a pkg-config .pc file so users get the right include paths and link flags for your library with one command, and install it where pkg-config will find it.

When you install a library, you also owe its users a way to compile against it. They need to know where the headers are, where the library is, which other libraries it needs and which compiler flags it requires. Without that, every user writes -I/usr/local/include -L/usr/local/lib -lvec -lm by hand, and the first one who installs it under a different prefix gets a broken build. pkg-config solves this with a small text file, installed next to the library, that describes the flags. This post writes a .pc file for a library, explains each field, installs it, and tests it from a build. It uses GCC 14 and pkgconf 2.1, the maintained implementation behind the pkg-config command on most distributions, and follows the pkg-config guide. The library itself is the one built in versioning a shared library.
What the file looks like
A .pc file has variables and fields. Variables are name=value lines you define and reuse. Fields are the ones pkg-config knows: Name, Description, Version, Requires, Libs and Cflags. Here is one for libvec:
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: vec
Description: Small vector arithmetic library
Version: 1.1.0
Requires:
Libs: -L${libdir} -lvec
Libs.private: -lm
Cflags: -I${includedir}/vecThe first four lines define variables. Writing the paths in terms of ${prefix} means one edit relocates the whole file. Name is a human-readable label, not the package name used on the command line: that comes from the file name, so vec.pc is the package vec. Version is what pkg-config --modversion and version constraints compare. Cflags carries the include directory and any required defines. Libs carries the link flags a user needs.
Public and private dependencies
Two pairs of fields cause most of the confusion: Requires against Requires.private, and Libs against Libs.private. The distinction is whether your library's headers expose the dependency to the user.
If a header of yours includes a header of another library, that library is a public requirement, because the user's compiler must find it. List it in Requires, and pkg-config adds its flags to the user's compile line. If the dependency is used only inside the compiled code, it is private. For a shared library, the private libraries are already recorded in its NEEDED entries and need no flag from the user. For a static library, the user's link line must name them, so pkg-config --static also reads the private fields.
In the file above, the math library is private: the header does not use it, and the shared build records it. It is a plain linker flag and not a pkg-config package, so it goes in Libs.private as -lm, never in Requires.private, which accepts only package names and would fail with a package-not-found error. The --static request adds -lm. Putting -lm in Libs instead would work but forces every user to link it even when they do not need it, which is the pollution that the private fields avoid.
Install it where pkg-config looks
The search path is a list of directories, shown with pkg-config --variable=pc_path pkg-config. The standard location for a library installed under /usr/local is /usr/local/lib/pkgconfig. A makefile rule installs it:
PREFIX ?= /usr/local
install: libvec.so.1.1.0 vec.pc
install -d $(DESTDIR)$(PREFIX)/lib $(DESTDIR)$(PREFIX)/include/vec \
$(DESTDIR)$(PREFIX)/lib/pkgconfig
install -m 755 libvec.so.1.1.0 $(DESTDIR)$(PREFIX)/lib/
ln -sf libvec.so.1.1.0 $(DESTDIR)$(PREFIX)/lib/libvec.so.1
ln -sf libvec.so.1 $(DESTDIR)$(PREFIX)/lib/libvec.so
install -m 644 vec.h $(DESTDIR)$(PREFIX)/include/vec/
install -m 644 vec.pc $(DESTDIR)$(PREFIX)/lib/pkgconfig/DESTDIR lets a packager install into a staging root without touching the real system. If you install to a prefix that is not in the default search path, users export PKG_CONFIG_PATH=$PREFIX/lib/pkgconfig so pkg-config finds the file.
Generate the file instead of editing it
Hard-coding prefix=/usr/local in a checked-in file breaks installs elsewhere. Keep a template and substitute at install time:
prefix=@PREFIX@
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: vec
Description: Small vector arithmetic library
Version: @VERSION@
Libs: -L${libdir} -lvec
Libs.private: -lm
Cflags: -I${includedir}/vecvec.pc: vec.pc.in
sed -e 's|@PREFIX@|$(PREFIX)|' -e 's|@VERSION@|$(VERSION)|' $< > $@CMake and Meson do the same with configure_file() and pkgconfig.generate(). Meson's generator also fills the dependency fields from the target definitions, which avoids drift.
Test it from the user's side
Query the package as a user would:
pkg-config --modversion vec
pkg-config --cflags --libs vec
pkg-config --static --libs vec1.1.0
-I/usr/local/include/vec -L/usr/local/lib -lvec
-L/usr/local/lib -lvec -lmThe shared query has no -lm, and the static one adds it. Now use it in a build:
#include <stdio.h>
#include <vec.h>
int main(void)
{
double a[] = {1, 2, 3}, b[] = {4, 5, 6};
printf("dot = %.1f\n", vec_dot(a, b, 3));
return 0;
}gcc app.c $(pkg-config --cflags --libs vec) -o app
./appdot = 32.0The #include <vec.h> resolves because -I/usr/local/include/vec came from Cflags. A user never types a path. Add a version check to make a build fail early when the library is too old:
pkg-config --atleast-version=1.1 vec && echo okokIn a build script, pkg-config --exists 'vec >= 1.1' does the same and returns a status the script can test.
Mistakes that break users
Putting -lvec before the objects that need it is not a .pc problem, but a .pc file that returns Libs and is used in the wrong position is the cause of many reports: place $(pkg-config --libs vec) after the source files on the command line. Omitting Cflags for a header that needs a define leaves users to rediscover it. Leaking a build-tree path such as /home/dev/build into an installed file makes it work on your machine alone, so grep the installed file for /home as a release check. And hard-coding -Wl,-rpath in Libs forces a runtime path on everyone, which packagers dislike, so leave it out unless the library lives somewhere unusual.
When pkg-config is not the right answer
CMake users often prefer find_package, which needs a vecConfig.cmake file, though CMake can also consume .pc files through pkg_check_modules. Shipping both reaches the most users. For a header-only or single-file library, a .pc file with only Cflags is enough, and for a library used only inside one project, a direct build dependency is simpler.
Version constraints and conflicts
The Requires field accepts version constraints, and they are how a library says which versions of its own dependencies it works with. A file that needs a newer release of a companion library writes Requires: zlib >= 1.2.11, and pkg-config refuses to produce flags if the installed copy is older, with an error that names both versions. The same applies to users: pkg-config --exists 'vec >= 1.1, vec < 2' expresses "any release in the first major series", which matches the soname rule that a new major number signals an incompatible interface.
Two more fields are worth knowing. Conflicts lists packages that cannot be used together with yours, for example an old library that exports the same symbols. And Provides lets one package stand in for another, which helps when you rename a library and want old build scripts to keep working. Use them sparingly, because each one is a promise you must keep across releases.
When a user reports that the flags are wrong, ask for the output of pkg-config --debug --cflags vec. It prints every directory searched, every file read and every variable expanded, and shows within a few lines whether the cause is a missing PKG_CONFIG_PATH, a stale copy of the file in another directory or a variable that did not expand the way you expected.


