fpm icon indicating copy to clipboard operation
fpm copied to clipboard

ordering of link flags before archive, thus flag ignored by linker

Open scivision opened this issue 1 year ago • 2 comments

Description

The link flags are put before the archive in the command line, causing the link flags to be ignored by the linker (at least for GCC).

With at least FPM 0.10.0 and 0.10.1, regardless of operating system (Linux and Windows tried), and using Ffilesystem project (probably any project has this issue):

by either:

FPM_LDFLAGS=-lm fpm build

or

fpm build --link-flags "-lm"

results in command:

Windows:

gfortran    -Wall -Wextra -fPIC -fmax-errors=1 -g -fcheck=bounds -fcheck=array-temps -fbacktrace -fcoarray=single -fimplicit-none -Werror=implicit-interface -I.\.\include  -lm build\gfortran_CCDA9AB199436CCD\ffilesystem\app_fortran_main.f90.o build\gfortran_A18F5477E168D1CF\ffilesystem\libffilesystem.a -lstdc++ -o build\gfortran_E885EEFD2DC7A74C\app\main.exe

Linux:

gfortran    -Wall -Wextra -fPIC -fmax-errors=1 -g -fcheck=bounds -fcheck=array-temps -fbacktrace -fcoarray=single -fimplicit-none -Werror=implicit-interface -I././include  -lm build/gfortran_E17BCF1508095BB1/ffilesystem/app_fortran_main.f90.o build/gfortran_FA0A528919A6C0C3/ffilesystem/libffilesystem.a -lstdc++ -o build/gfortran_6A2ED2947B09C891/app/main

Expected Behaviour

I expect the link flag "-lm" (or say -lUserenv for Windows) to be AFTER the object file or archive, otherwise the linker ignores the link flag

Version of fpm

0.10.1, 0.10.0

Platform and Architecture

at least Linux, Windows

Additional Information

No response

scivision avatar Apr 07 '24 15:04 scivision

I can confirm the bug with,

> git clone https://github.com/scivision/ffilesystem.git
> fpm build --link-flag "-lm -lstdc++" --cxx-compiler g++-14 --verbose
...
ar: creating archive build/gfortran_FA0A528919A6C0C3/ffilesystem/libffilesystem.a
[ 96%]               libffilesystem.a  done.
[ 96%]                       main.f90
 + gfortran -c app/fortran/main.f90   -Wall -Wextra -fPIC -fmax-errors=1 -g -fcheck=bounds -fcheck=array-temps -fbacktrace -fcoarray=single -fimplicit-none -Werror=implicit-interface -ffree-form -I././include -J build/gfortran_E17BCF1508095BB1 -Ibuild/gfortran_E17BCF1508095BB1 -o build/gfortran_E17BCF1508095BB1/ffilesystem/app_fortran_main.f90.o
[ 98%]                       main.f90  done.
[ 98%]                           main
 + mkdir -p build/gfortran_675E47D95E3BFF1E/app/
 + gfortran    -Wall -Wextra -fPIC -fmax-errors=1 -g -fcheck=bounds -fcheck=array-temps -fbacktrace -fcoarray=single -fimplicit-none -Werror=implicit-interface -I././include  -lm -lstdc++ build/gfortran_E17BCF1508095BB1/ffilesystem/app_fortran_main.f90.o build/gfortran_FA0A528919A6C0C3/ffilesystem/libffilesystem.a -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -lc++ -o build/gfortran_675E47D95E3BFF1E/app/main
ld: warning: ignoring duplicate libraries: '-lc++'
[100%]                           main  done.
[100%] Project compiled successfully.

Also something very suspicious going on with the C++ standard libraries (scroll right to see the problem... 👀 ).

ivan-pi avatar Mar 14 '25 14:03 ivan-pi

Also seems to be a duplicate of https://github.com/fortran-lang/fpm/issues/811

ivan-pi avatar Mar 14 '25 14:03 ivan-pi