Skip to content

zig build-exe should not strip indirectly needed libraries #23215

@martinetd

Description

@martinetd

Zig Version

0.13.0
also tested latest 0.15.0-dev.37+423907c27

Steps to Reproduce and Observed Behavior

Building the following main.c against libgsl produces a binary that cannot run

  • main.c:
#include <gsl/gsl_cdf.h>
#include <stdio.h>

int main(){
    double bottom_tail = gsl_cdf_gaussian_P(-1.96, 1);
    printf("Area betweeen [-1.96, 1.96]: %g\n", 1 - 2*bottom_tail);
}
  • build (and comparison with gcc/clang)
$ pkg-config --cflags --libs gsl
 -lgsl -lgslcblas -lm
$ zig build-exe main.c -femit-bin=test-zig -lgsl -lgslcblas -lm
$ gcc main.c -o test-gcc -lgsl -lgslcblas -lm
$ clang main.c -o test-clang -lgsl -lgslcblas -lm
$ for t in test-*; do echo $t; ldd $t | grep gsl; done
test-clang
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f7c2fe00000)
	libgslcblas.so.0 => /lib64/libgslcblas.so.0 (0x00007f7c301dc000)
test-gcc
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007f7213600000)
	libgslcblas.so.0 => /lib64/libgslcblas.so.0 (0x00007f7213914000)
test-zig
	libgsl.so.27 => /lib64/libgsl.so.27 (0x00007fdc05400000)
$ ./test-zig 
./test-zig: symbol lookup error: /lib64/libgsl.so.27: undefined symbol: cblas_ctrmv
$ LD_PRELOAD=/lib64/libgslcblas.so.0 ./test-zig
Area betweeen [-1.96, 1.96]: 0.950004

Expected Behavior

The immediate problem is that main.c itself doesn't depend on libgslcblas, so the library is stripped from the elf's NEEDED libs, but libgsl does need it and it's not explicit with libgsl on purpose to allow using other cblas libraries (according to https://www.gnu.org/software/gsl/doc/html/usage.html )

gcc/clang properly keep the indirectly needed libraries (even with -Wl,--as-needed), so zig probably should detect it is needed as well somehow.

Thank you!

Credits

Thanks to @ju1m for the reproducer, coming from https://discourse.nixos.org/t/gsl-dependency-cblas-in-nix-flake/38133/3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions