openblas-src icon indicating copy to clipboard operation
openblas-src copied to clipboard

Link libgfortran statically when `features=static`

Open jianshu93 opened this issue 3 years ago • 14 comments
trafficstars

Hello Team,

With openblas static feature, it can be linked statically but the gfortran itself was linked dynamically (gcc -lgfortran was required to link openblas), e.g, libgfortran.so.5 was actually linked to the binary if using ldd binary, together with libquadmath.so.0. I am wondering whether gfortran can be linked statically by gcc.

Thanks,

Jianshu

jianshu93 avatar Sep 02 '22 14:09 jianshu93

I am wondering whether gfortran can be linked statically by gcc.

This is possible if libgfortran.a exists on your system. gfortran has a command line flag -static-libgfortran, but it does not work if libgfortran.a does not exist. https://gcc.gnu.org/onlinedocs/gfortran/Link-Options.html

On systems that provide libgfortran as a shared and a static library, this option forces the use of the static version. If no shared version of libgfortran was built when the compiler was configured, this option has no effect.

On some Linux distribution, including ArchLinux which I usually use, gfortran package does not distribute static system libraries. Please use ubuntu or other distribution which distributes libgfortran.a.

static libgfortran.so link

Basically, it is not possible to link share library in static manner.

termoshtt avatar Sep 16 '22 05:09 termoshtt

Strange enough,I have both static and dynamic libraries installed on my system. However rustc link dynamically gfortran and quadmath. is there a way to ask rustc link statically? i understand by default dynamic is better. but for portability of binaries,static is more convenient.

thanks,

Jianshu

jianshu93 avatar Sep 16 '22 06:09 jianshu93

I have both static and dynamic libraries installed on my system. However rustc link dynamically gfortran and quadmath.

This behavior should be fixed. I'll look into it.

termoshtt avatar Sep 16 '22 09:09 termoshtt

It seems this question is related to gcc/gfotran (due to libquadmath not linked statically): https://stackoverflow.com/questions/17910684/static-libgfortran-in-library-build . It is the same on linux for me. On ubuntu like system, libgfortran.a and libquadmath.a are all installed after installing gcc/gfortran, they are just forced to link dynamically.

I have been struggling with this problem for several weeks.

Thanks,

Jianshu

jianshu93 avatar Oct 01 '22 02:10 jianshu93

Hello! Is there any solution to it? It really confuse me for times. Thanks @jianshu93 @termoshtt

Dirreke avatar Mar 15 '23 14:03 Dirreke

I think still no. linux force dynamic link for library quadmath while gfortran relies on it. therefore libfortran.so will be used instead of .a

jianshu93 avatar Mar 15 '23 15:03 jianshu93

I found a temporary solution.

rustc can link libgfortran.a by cargo:rustc-link-lib=static=gfortran and cargo:rustc-link-search=path/to/- your/libgfortran.a at its link stage.

However, path/to/your/libgfortran.a is required because it's only in the search path of gcc/g++/gfortran...but not rustc.

the path of libgfortran.a can be get by ${PREFIX}-gfortran --print-file-name=libgfortran.a

Dirreke avatar Mar 15 '23 16:03 Dirreke

but quadmath is still linked dynamically right. but still this is much better than before, at least gfortran is static now.

Thanks

Jianshu

jianshu93 avatar Mar 15 '23 18:03 jianshu93

and you can use the same way to the quadmath, it works

---- Replied Message ---- | From | @.> | | Date | 03/16/2023 02:45 | | To | @.> | | Cc | @.>@.> | | Subject | Re: [blas-lapack-rs/openblas-src] Link libgfortran statically when features=static (Issue #88) |

but quadmath is still linked dynamically right. but still this is much better than before, at least gfortran is static now.

Thanks

Jianshu

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

Dirreke avatar Mar 16 '23 04:03 Dirreke

is there a easy way to add those to a configure file so that I do not need to worry about them next time? Thanks Jianshu

jianshu93 avatar Mar 16 '23 06:03 jianshu93

I have no idea.

I use ${PREFIX}-gfortran --print-file-name=libgfortran.a to get the path because different target has diferrent path of these libs.

Then, I use the following content in build.rs to link them

println!("cargo:rustc-link-lib=static=gfortran");
println!("cargo:rustc-link-search=path/to/your/libgfortran.a");
if cfg!(target_arch="x86_64") {
    println!("cargo:rustc-link-lib=static=quadmath");
    println!("cargo:rustc-link-search=path/to/your/libquadmath.a");
}

Maybe this method can be done in openblas-src

Dirreke avatar Mar 16 '23 06:03 Dirreke

This does not work for me. Is that because I have both dynamic and static installed?

Thanks, Jianshu

jianshu93 avatar Mar 17 '23 02:03 jianshu93

I tried this method on Ubuntu22, which has both dynamic and static library, and it works.

I dont know why it doesn't work for you.

I'm sorry that I can't help you.

Dirreke avatar Mar 17 '23 06:03 Dirreke

It turns out that I am on a linux server, which is annoying for building/linking/installing libraries.

I use your strategy and it compiles but the last step it complained that native static libgortran.a was not found despite I provide the search path for libgfortran.a as you did. I will test on a regular Linux system and get back.

Thanks,

Jianshu

jianshu93 avatar Jun 27 '23 18:06 jianshu93