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

Build: Support static linked executable #4152

Closed

Conversation

suzp1984
Copy link
Contributor

@suzp1984 suzp1984 commented Aug 22, 2024

related to #4148 : support static linked srs executable.

Only verified Linux and MacOS platform, Cygwin wait for verify.

How to use static linking option and verify the static linked executable?

docker run -it --rm -v `pwd`:/srs -w /srs ossrs/srs:ubuntu20 bash -c "./configure --static=on && make"
docker run -it --rm -v `pwd`:/srs -w /srs ossrs/srs:ubuntu20 bash -c "ldd ./objs/srs"

if you just want the static linking the c++ std library, use option --static-stdcpp=on:

docker run -it --rm -v `pwd`:/srs -w /srs ossrs/srs:ubuntu20 bash -c "./configure --static-stdcpp=on && make"

MacOS platform

Mac OS X does not support statically linked executable binaries. The compiler exit with error for -static.
@see https://developer.apple.com/library/archive/qa/qa1118/_index.html

for -static-libstdc++ compiler option: MacOS didn't implement it, the compiler just give a unused warning.

Linux platform

-static, -static-libstdc++ and -static-libgcc support them well.
But this PR not include the -static-libgcc.

Cygwin

Not yet verified.

@suzp1984 suzp1984 marked this pull request as ready for review August 22, 2024 08:01
@@ -224,6 +224,10 @@ fi
# so we need to link the c++ libraries staticly but not all.
# @see https://stackoverflow.com/a/26107550
if [[ $SRS_STATIC == YES ]]; then
Copy link
Member

@winlinvip winlinvip Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need SRS_STATIC_STD_CPP? Maybe we should only support SRS_STATIC which leads to -static-libstdc++ -static?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-static will generate a static linked executer,
-static-libstdc++ only link the static libstdc++ library.
So -static will cover -static-libstdc++ scope, I think it's no need to combine -static -static-libstdc++ together.

https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Copy link
Member

@winlinvip winlinvip Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the output if use -static without -static-libstdc++? User only want a static binary without depends, majority of users can't understand the nuance of these two options.

Copy link
Contributor Author

@suzp1984 suzp1984 Aug 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Linux:

-static output:

docker run -it --rm -v `pwd`:/srs -w /srs ossrs/srs:ubuntu20 bash -c "./configure --static=on && make"

/usr/bin/ld: ./objs/src/kernel/srs_kernel_error.o: in function `parse_symbol_offset(char*)':
/srs/./src/kernel/srs_kernel_error.cpp:73: warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: ./objs/src/kernel/srs_kernel_utility.o: in function `srs_dns_resolve(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int&)':
/srs/./src/kernel/srs_kernel_utility.cpp:161: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: ./objs/openssl/lib/libcrypto.a(b_sock.o): in function `BIO_gethostbyname':
b_sock.c:(.text+0x78): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
make[1]: Leaving directory '/srs'

The warning said: dlopen, getaddrinfo and gethostbyname requires libc similar runtime shared library to support.

docker run -it --rm -v `pwd`:/srs -w /srs ossrs/srs:ubuntu20 bash -c "ldd ./objs/srs"

not a dynamic executable

with both -static -static-ibstdc++

same warning like -static.
ldd ./objs/srs
same output: not a dynamic executable.

git show 6314c27

on above commit, SRS_STATIC use -static-libstdc++ to replace -static.
But I think keep fully static linked exec can be an option, but not recommended.

Copy link
Member

@winlinvip winlinvip Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon investigation, when we utilize certain functions such as dlopen and gethostbyname, we cannot achieve complete static linking, even though we use the -static flag; full static linking is not possible.

Only by removing the dependencies on these functions can we fully utilize the -static flag to create a version that truly does not depend on glibc.

Therefore, it is necessary to remove the dependencies on these functions. The dependency on dlopen comes from third-party libraries, which might be removed through compilation options. Functions like gethostbyname may require the use of different libraries or an implementation by the ST team itself.

https://stackoverflow.com/questions/15165306/compile-a-static-binary-which-code-there-a-function-gethostbyname

xmrig/xmrig#2902

https://www.reddit.com/r/haskell/comments/vqqq7x/comment/iez099a/

TRANS_BY_GPT4

Copy link
Member

@winlinvip winlinvip Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To facilitate full static linking, we can only support the -static option, rather than also having a -static-libstdc++ option, which users might not understand how to use.

Essentially, when we support -static, a multitude of warnings are generated. These warnings actually indicate potential issues, hence our support for -static is not robust and leaves underlying problems unresolved.

We absolutely cannot offer an option that carries potential risks, as it would make any arising issues even more difficult to address.

TRANS_BY_GPT4

@winlinvip winlinvip linked an issue Aug 22, 2024 that may be closed by this pull request
@winlinvip winlinvip changed the title support static linked executable Build: Support static linked executable Aug 22, 2024
@winlinvip winlinvip closed this Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regarding the issue of static compilation not taking effect
2 participants