Skip to content

How should C++ dependencies of C libraries be properly linked? #14234

Open
@alyssais

Description

When C and C++ objects are being linked together, a C++ compiler should be used. Meson handles this case for internal C and C++ objects, but if a C project links with a C++ dependency, Meson does not know to use a C++ linker. Because static libraries don't contain dependency information, linking a static C executable with a C++ dependency will fail with undefined symbol errors for the libstdc++ (etc.) symbols. If a C executable depends on a C library that depends on a C++ library, that executable also has to be linked with a C++ linker, and so on.

For example, in Nixpkgs, we link libtiff (C) with LERC (C++), and we support building as much of our entire package set as possible using only static linking. Getting this to work properly would be very invasive — fixing libtiff is fine, but every C program that even transitively depends on libtiff also needs to be made to use a C++ linker. For example, I'm currently looking at appstream (C) -> librsvg (C [and Rust but that's irrelevant]) -> gdk-pixbuf (C) -> libtiff (C) -> LERC (C++), and wondering if it really makes sense to be teaching appstream's build system to be using link_language: 'cpp' whenever it's linking an executable with librsvg. Working around this in the build system is especially complicated when, as in this case, the C++ dependency layers down the dependency chain is optional, so a C++ compiler shouldn't always be used to prevent needlessly requiring one when there's no C++ involved.

What would the right solution be here? To me "this library needs to be linked using a C++ linker" is information that is analagous to the dependency information for static libraries included in pkg-config files, but there's no way to express that in a pkg-config file. Technically you could add the C++ standard library as a Libs.private entry, but that doesn't feel right, since AFAICT it's supposed to be an implementation detail of the C++ compiler, and there might be other C++-specific behaviors it's necessary to use a C++ compiler to get besides linking the standard library. (This isn't even really a Meson-specific problem, but it is a problem that Meson has, and it so happens that most of the packages I'm looking at are using Meson, and this is the place I trust I can find people who care about solving this problem.)

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