Compilation success or fail depending on the order of inclusion of fortran_stdlib_...
I am learning to use fortran_stdlib. I encountered an issue that took me hours to resolve and I wonder whether this is a bug or a feature.
Details:
- Ubuntu Linux 24.04.4
- Compiler: gfortran 13.3.0
- I compiled fortran_stdlib following the instructions on GitHub; all tests passed.
Please refer to the minimal working example below:
program stdlib_norm_mwe↲
use stdlib_linalg, only: norm↲
use stdlib_math, only: deg2rad↲
use stdlib_ascii, only: to_upper↲
implicit none↲
real, dimension(3) :: v↲
character(len=1) :: c↲
real :: theta↲
theta = 45.0↲
c = 'a'↲
v = [1.0, 2.0, 3.0]↲
print *, v, norm(v, 2)↲
print *, c, " -> to_upper() ->", to_upper(c)↲
print *, theta, deg2rad(theta)↲
print *, "success"↲
end program stdlib_norm_mwe↲
Compilation steps:
Step 1: gfortran -c stdlib_norm_mwe.f90 -I /usr/local/include/fortran_stdlib/GNU-13.3.0/
Step 2a (notice that fortran_stdlib_core is now the LAST library to be included): gfortran -o stdlib_norm_mwe stdlib_norm_mwe.o -L /usr/local/lib/ -lfortran_stdlib_linalg -lfortran_stdlib_linalg_core -lfortran_stdlib_lapack -lfortran_stdlib_blas -lfortran_stdlib_math -lfortran_stdlib_core
This is compiles without warning and the resulting program functions correctly.
Step 2b (notice that fortran_stdlib_core is now the FIRST library to be included): gfortran -o stdlib_norm_mwe stdlib_norm_mwe.o -L /usr/local/lib/ -lfortran_stdlib_core -lfortran_stdlib_linalg -lfortran_stdlib_linalg_core -lfortran_stdlib_lapack -lfortran_stdlib_blas -lfortran_stdlib_math
This fails with many messages. The last few lines are:
stdlib_linalg_lapack_aux.f90:(.text+0xaa23): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xaa9c): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xab15): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xac53): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xaccc): undefined reference to `__stdlib_error_MOD___vtab_INTEGER_4_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xae06): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xae7f): undefined reference to `__stdlib_error_MOD___vtab_INTEGER_4_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xaef8): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xaf71): undefined reference to `__stdlib_error_MOD___vtab_INTEGER_4_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xb0b5): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xb12e): undefined reference to `__stdlib_error_MOD___vtab_INTEGER_4_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xb1a7): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' /usr/bin/ld: stdlib_linalg_lapack_aux.f90:(.text+0xb2e5): undefined reference to `__stdlib_error_MOD___vtab_CHARACTER_1_' collect2: error: ld returned 1 exit status
In my experience, the order of inclusion of the libraries on the command line should not make a difference, but in this case, it does. I don't know whether this is a bug or a feature.
On static builds the libraries have to be linked in dependency order; pkg-config --libs fortran_stdlib prints them in a working order. On Linux its output also had a bare dl that breaks the link; I opened #1220 to fix that.
Hello, I’m interested in investigating this compilation and linking-order issue. I would reproduce it with the current CMake/pkg-config setup, identify the dependency-order assumptions, and add a focused regression test or documentation improvement. Is this issue available, and would maintainers recommend a particular build configuration to start with?
3. Hello, I’m interested in investigating this compilation and linking-order issue. I would reproduce it with the current CMake/pkg-config setup, identify the dependency-order assumptions, and add a focused regression test or documentation improvement. Is this issue available, and would maintainers recommend a particular build configuration to start with?
I included a minimal working example in my original post, perhaps you can use that as a starting point. I have been programming a bit, and as I added more source code and subroutines, I did encounter this issue where the order of inclusion makes a difference a few times. It is very frustrating, but until now I have been able to solve it every time.
Thanks for providing the minimal working example. I’ll use it to reproduce the linker-order behavior and compare the generated pkg-config output with the manual library order. Before I start implementing, would you be comfortable with me investigating this issue and proposing a focused fix or documentation update?
5. Thanks for providing the minimal working example. I’ll use it to reproduce the linker-order behavior and compare the generated pkg-config output with the manual library order. Before I start implementing, would you be comfortable with me investigating this issue and proposing a focused fix or documentation update?
If you wish to investigate, please go ahead, and I will try to help if needed.