How can I programatically determine where my C++ runtime libraries are?

无人久伴 提交于 2019-11-30 18:44:09

问题


I'm building C++11 with GCC 4.8 on CentOS 6 using "hax", then deploying on arbitrary CentOS 6 targets (on which anything C++-related out of the box will have been built as C++03 with GCC 4.3) (ref).

To make this work, I'm going to ship all of my third-party libs as well as the g++ runtimes, and rpath my executables so the newer libs are assuredly found in the proper place. For the runtimes, by my count I need to ship libstdc++ and libgcc_s. But I need to know where they are on my build system so that I can package them up.

Is there some neat way I can query for their location from within my packaging script?

(If the best approach is too awkward I'll just link them statically, but I'd like to avoid that if I can as my project includes several executables. Also if I were to statically link everything I believe I'd run the risk of GPL-ing my whole project, e.g. by statically linking the MySQL C API via my C++ MySQL wrapper lib. Could do a mixture of both, I suppose, though some sources warn against this…)

For bonus points, do I need to add anything to this list, out of libssl, libcrypto, libm, libpthread, libc, librt, libz and ld-linux-x86-64?


回答1:


If I understand correctly, you have already built your binaries and just want to get a list of runtime libraries to package them along with binaries? You can try using ldd for this, like:

> ldd /usr/bin/ls
    linux-vdso.so.1 (0x00007ffe76dd2000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc97131f000)
    libcap.so.2 => /lib64/libcap.so.2 (0x00007fc97111a000)
    libacl.so.1 => /lib64/libacl.so.1 (0x00007fc970f10000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fc970b68000)
    libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007fc970902000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fc9706fd000)
    /lib64/ld-linux-x86-64.so.2 (0x000055c4ba4ed000)
    libattr.so.1 => /lib64/libattr.so.1 (0x00007fc9704f8000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc9702db000)

This way you will see all libraries needed except, of course, the ones that are used via dlopen().




回答2:


In my Makefile:

GCC_INSTALL_DIR := $(shell $(CXX) -print-search-dirs | grep install | cut -d' ' -f2)

…then my main build target will perform:

ln -sf $(GCC_INSTALL_DIR)/libstdc++.so $(BIN_DIR)/deps/

…and I can dump everything in $(BIN_DIR)/deps into the right place on install.

I think.



来源:https://stackoverflow.com/questions/39001658/how-can-i-programatically-determine-where-my-c-runtime-libraries-are

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!