Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link libgfortran statically when features=static #88

Open
jianshu93 opened this issue Sep 2, 2022 · 14 comments
Open

Link libgfortran statically when features=static #88

jianshu93 opened this issue Sep 2, 2022 · 14 comments

Comments

@jianshu93
Copy link

jianshu93 commented Sep 2, 2022

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

@termoshtt
Copy link
Member

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.

@jianshu93
Copy link
Author

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

@termoshtt
Copy link
Member

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 termoshtt changed the title static libgfortran.so link Link libgfortran statically when features=static Sep 16, 2022
@jianshu93
Copy link
Author

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

@Dirreke
Copy link
Contributor

Dirreke commented Mar 15, 2023

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

@jianshu93
Copy link
Author

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

@Dirreke
Copy link
Contributor

Dirreke commented Mar 15, 2023

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

@jianshu93
Copy link
Author

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

Thanks

Jianshu

@Dirreke
Copy link
Contributor

Dirreke commented Mar 16, 2023 via email

@jianshu93
Copy link
Author

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

@Dirreke
Copy link
Contributor

Dirreke commented Mar 16, 2023

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

@jianshu93
Copy link
Author

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

Thanks,
Jianshu

@Dirreke
Copy link
Contributor

Dirreke commented Mar 17, 2023

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.

@jianshu93
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants