且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

程序找不到glibc / libstdc ++的正确版本,尽管它是静态链接的

更新时间:2023-11-10 17:26:58


我试图用glibc静态链接我的程序,因为目标机器上的glibc版本几乎不可预知。我使用了链接器标志-static-libgcc和-static-libstdc ++,它工作正常。

这并不影响glibc的版本( libc ),与 libgcc libstdc ++不同 。有了这些标志,您仍然生成了一个动态链接的可执行文件,这是期望的不能用于较早的发行版。



您可以链接你的可执行文件使用 -static 标志,并且应该给你一个完全静态的可执行文件。



更新:



重新阅读您的问题后,你的问题是不是 glibc 。你的问题是你正在链接 libboost_log.so ,它本身依赖于 libstdc ++。so.6 。 p>

然后答案是与 libboost * .a 而不是 libboost * .so 。你可以尝试这样做:

  g ++ $(OBJS)-static-libgcc -static-libstdc ++ -Wl, - Bstatic -lboost_log ... \ 
-Wl,-Bdynamic

em> 对于 -Wl,-Bdynamic 来说很重要。)


I am trying to link my program statically with glibc, because version of the glibc on the target machine is pretty much unpredictable. I used linker flags -static-libgcc and -static-libstdc++ and it worked fine. The executable is big, but I can live with it. Unfortunately, when I run my executable on the target machine (it is named 'mytest' in the example below) I get the following error:

./mytest: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by libboost_log.so.1.53.0)
./mytest: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by libboost_log.so.1.53.0)
./mytest: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by libboost_log.so.1.53.0)
./mytest: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by libboost_date_time.so.1.53.0)
./mytest: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by libboost_thread.so.1.53.0)

If I do 'strings' on mytest, it gives me

$ strings mytest | grep GLIBC
GLIBC_2.9
GLIBC_2.7
GLIBC_2.8
GLIBC_2.3.2
GLIBC_2.2.5
GLIBCXX_3.4.15
GLIBCXX_3.4.11
GLIBCXX_3.4.14
GLIBCXX_3.4.9
GLIBCXX_3.4

What means, I think, that the static linking was working ok. Why does the loader still tries to look for my functions in shared glibc and libstdc++? What am I doing wrong?

Thanks!

I am trying to link my program statically with glibc, because version of the glibc on the target machine is pretty much unpredictable. I used linker flags -static-libgcc and -static-libstdc++ and it worked fine.

That didn't affect the version of glibc (libc), which is different from libgcc and libstdc++. With these flags, you still have produced a dynamically-linked executable, which is expected to not work on an older distribution.

You can link your executable with -static flag, and that should give you a completely static executable.

Update:

After re-reading your question; your problem is not with glibc. Your problem is that you are linking with libboost_log.so, which itself depends on libstdc++.so.6.

The answer then is to link with libboost*.a instead of libboost*.so. You can try to achieve it this way:

g++ $(OBJS) -static-libgcc -static-libstdc++ -Wl,-Bstatic -lboost_log ... \
  -Wl,-Bdynamic

(It is very important to have the trailing -Wl,-Bdynamic.)