Skip to content

Using pkg-config to link against static libraries does not correctly call pkg-config #23382

@AnthonyZhOon

Description

@AnthonyZhOon

Zig Version

0.14.0

Steps to Reproduce and Observed Behavior

Given a pkg-config file

prefix=/home/fedora/dev/glslang/install
exec_prefix=/home/fedora/dev/glslang/install
libdir=${exec_prefix}/lib64
includedir=${prefix}/include

Name: glslang
Description: OpenGL and OpenGL ES shader front end and validator
Requires:
Requires.private: SPIRV-Tools 
Version: 
Libs: -L${libdir} -lglslang -lSPVRemapper 
Libs.private: -lSPIRV-Tools -lSPIRV-Tools-opt -lSPIRV-Tools-link
Cflags: -I${includedir}

(at <prefix>/lib64/pkgconfig/glslang.pc)
Which can be exposed by placing it inside a directory on the PKG_CONFIG_PATH env var.
pkg-config --cflags --libs glslang will default to the flags needed for linking as a shared library (only Libs and Cflags) while pkg-config --cflags --libs --static glslang .

export PREFIX="<prefix>"
PKG_CONFIG_PATH="$PREFIX/lib64/pkgconfig" pkg-config --cflags --libs glslang
PKG_CONFIG_PATH="$PREFIX/lib64/pkgconfig" pkg-config --cflags --libs --static glslang

Expected Behavior

https://github.com/ziglang/zig/blob/5ad91a646a753cc3eecd8751e61cf458dadd9ac4/lib/std/Build/Step/Compile.zig#L745C5-L751C6

       --static
               Compute a deeper dependency graph and use compiler/linker flags intended  for
               static linking.

       --shared
               Compute a simple dependency graph that is only suitable for shared linking.

pkg-config must be called with --static to produce a deeper set of transitive dependencies for static linking the archive, however the link_object's preferred link mode is not used to configure pkg-config for linking system libraries. A shared library would represent this as transitive dependencies to be fulfilled at runtime.

As zig does not configure --static to link against system libraries a workaround is to move Libs.private flags to Libs however this leads to overlinking1 of shared libraries. I think the intended behaviour is to provide the --shared or --static flags to pkg-config depending on the targeted link mode to support this.

Footnotes

  1. https://wiki.mageia.org/en/Overlinking_issues_in_packaging

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorzig build systemstd.Build, the build runner, `zig build` subcommand, package management

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions