Skip to content

cmd/go: #cgo pkg-config: add conditional --static flag to pkg-config #12058

@kolyshkin

Description

@kolyshkin

I found this case when working on Docker. Docker can be linked either statically or dynamically. For static linking, one has to specify all the libraries explicitly, including those required indirectly (i.e. by libraries that you use). For dynamic linking, one needs to specify only the libraries one uses directly. For example, when linking against libxml2 on Linux, for the case of dynamic linking you have to specify only -lxml2, while for the static linking case you have to also specify all the libraries used by libxml2 (like -lz -lm -llzma).

Fortunately, pkg-config supports this:

$ pkg-config --libs libxml-2.0
-lxml2 
$ pkg-config --libs --static libxml-2.0
-lxml2 -lz -llzma -lm 

I was expecting Go to add --static flag when the result is going to be statically linked, unfortunately it never happens. My current workaround is to do this:

// #cgo pkg-config: --static libxml-2.0

It solves my issues, but the problem with this approach is this leads to "overlinking" in the dynamic case, as described e.g. here: https://wiki.openmandriva.org/en/Overlinking_issues_in_packaging

Is it possible to implement conditionally adding --static to pkg-config invocation?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions