Skip to content

Reduce -L flag pollution by resolving -L -l flags to on-disk files #155

Open
@nirbheek

Description

@nirbheek

When picking up libraries from multiple prefixes (which is quite common in practice, for example when you have /usr and /opt), it is very easy to get into a situation where the wrong library is being picked up by the linker. For example:

/prefix1/lib/libfoo.a
/prefix1/lib/libbar.a
/prefix1/lib/pkgconfig/foo.pc
/prefix1/lib/pkgconfig/bar.pc

/prefix2/lib/libfaz.a
/prefix2/lib/libbar.a
/prefix2/lib/pkgconfig/faz.pc
/prefix2/lib/pkgconfig/bar.pc

With PKG_CONFIG_PATH=/prefix1/lib/pkgconfig:/prefix2/lib/pkgconfig you'd expect libbar.a to be picked up from prefix1. But if you are aggregating flags like so:

pkg_config::probe_library("faz").unwrap();
pkg_config::probe_library("bar").unwrap();

This will generate a linker line like:

[...] -L/prefix2/lib -L/prefix1/lib  [...] -lfaz -lbar

The linker will resolve this to:

/prefix2/lib/libfaz.a /prefix2/lib/libbar.a

The solution is that probe_library() should resolve the -L -l flags it receives from the pc file to on-disk files immediately, and pass those files to the linker:

/prefix2/lib/libfaz.a /prefix1/lib/libbar.a

If it is unable to find the corresponding library, it can fall back to the current behaviour. This should reduce the likelihood of incorrect libs being picked up considerably.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions