Closed guenhter closed 1 year ago
If I understand correctly, you want to build libzmq statically but also link statically to your GLIBC runtime, instead of the default dynamic.
I personally statically link to musl
instead, although musl
can have lower performance than glibc in some use cases, so I'm not too familiar with this kind of issue.
Based on the ouput you provided, the issue seems to be related to this kind of linking:
undefined reference to symbol '__tls_get_addr@@GLIBC_2.3'
After doing a little big of research, it seems that statically linking against glibc
is advised against because you lose some functionality, specifically related to networking (NSS), and there also potential licensing issues. So my guess is that your error is due to glibc static linking to some functionality that can't be statically linked.
I would try static linking against musl
if you can, otherwise I'm not sure I can help.
Thank you so much even for checking this. Musl would be also perfeclty fine. When I run it with it, I get some error regarding musl-g++
. When I create then a link for musl-g++
to point to g++
and run
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target x86_64-unknown-linux-musl
I get this
running: "musl-g++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-m64" "-static" "-I" "/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/include" "-I" "/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src" "-I" "/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src" "-I" "/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/external/sha1" "-I" "/work/target/x86_64-unknown-linux-musl/release/build/zmq-sys-748afc6f53e07fb4/out" "-Wall" "-Wextra" "-DZMQ_BUILD_TESTS=OFF" "-DZMQ_USE_CV_IMPL_STL11=1" "-DZMQ_STATIC=1" "-DZMQ_USE_BUILTIN_SHA1=1" "-DZMQ_HAVE_WS=1" "-DZMQ_IOTHREAD_POLLER_USE_EPOLL=1" "-DZMQ_POLL_BASED_ON_POLL=1" "-DZMQ_HAVE_IPC=1" "-DHAVE_STRNLEN=1" "-DZMQ_HAVE_UIO=1" "-DZMQ_HAVE_STRLCPY=1" "-o" "/work/target/x86_64-unknown-linux-musl/release/build/zmq-sys-748afc6f53e07fb4/out/lib/711a6e249c8746d4-ws_listener.o" "-c" "/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/ws_listener.cpp"
cargo:warning=In file included from /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/ws_engine.cpp:57:
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/compat.hpp: In instantiation of 'int strcpy_s(char (&)[size], const char*) [with long unsigned int size = 2049]':
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/ws_engine.cpp:461:64: required from here
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/compat.hpp:58:32: error: 'strlcpy' was not declared in this scope; did you mean 'strncpy'?
cargo:warning= 58 | const size_t res = strlcpy (dest_, src_, size);
cargo:warning= | ~~~~~~~~^~~~~~~~~~~~~~~~~~~
cargo:warning= | strncpy
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/compat.hpp: In instantiation of 'int strcpy_s(char (&)[size], const char*) [with long unsigned int size = 256]':
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/ws_engine.cpp:475:69: required from here
cargo:warning=/usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/zeromq-src-0.2.5+4.3.4/vendor/src/compat.hpp:58:32: error: 'strlcpy' was not declared in this scope; did you mean 'strncpy'?
cargo:warning= 58 | const size_t res = strlcpy (dest_, src_, size);
cargo:warning= | ~~~~~~~~^~~~~~~~~~~~~~~~~~~
cargo:warning= | strncpy
exit status: 1
How do you link to musl?
Alright, so interestingly RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target x86_64-unknown-linux-gnu
, which statically links against glibc
runs fine on my system. So I'm guessing you are missing some required libraries on your system. My glibc version is 2.31
btw.
As for linking against musl
, the command you provided would run fine:
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target x86_64-unknown-linux-musl
The problem is that you need to have musl
installed. On debian its musl
, musl-dev
, and musl-tools
. However since debian does not provided a musl-g++
cross compiler, you would have to build it yourself, which is probably a major pain. See this link (section Building a cross compiler targeting musl libc) for more info. It would probably be simpler for you to try to make the glibc build work. I'm afraid I can't help you more than that.
The interesting think is, that I do this things always in a fresh docker container of the image rust:1.71.0
.
You could try using a rust alpine linux container since alpine uses musl natively if I recall correctly. That might work. Something like rust:1.71-alpine3.18
.
Running the alpine container and adding apk add g++
fixed the issue. Thx. Appreciate your excellent support.
This is absolutely good enough for my use case. Even though I'll investigate a little bit more what dependencies are missing in the normal rust image and if I find it, I'll update the findings in this issue. here.
Hi,
I'm pretty certain that this is not a direct issue of this library but maybe you still can help me out and in the best case we could adapt the README to make static linking easier.
I have this main:
and a
lib.rs
which is just the copy of the testcrate of this library.my
build.rs
just is:but when I run
this error results:
Running the code without static linking works like a charm. Do you have any idea what the cause can be?