gcc c++ cmake 设置 CXX_FLAGS 不依赖 忽略 libc 版本

创建日期: 2024-11-14 20:13 | 作者: 风波 | 浏览次数: 16 | 分类: C++

来源:https://stackoverflow.com/questions/17390117/reducing-the-version-of-libc-required

原回答

If you want to use the newer library you have two options 1. statically link libstdc++. 1. distributing your version of the library with the binary and using RPATH while linking. You can statically link with g++ using the -static-libgcc -static-libstdc++ -static this should pretty much statically link everything but will enlarge your binary.

Another option is using RPATH the -rpath linker option can be passed like so :

-Wl,-rpath,$PATH replacing $PATH with something like libs or maybe just . for same directory as binary this way the header of the generated ELF (executable) will know where to look for the shared libraries required and you can just redistribute the shared libraries with your binary.

otherwise you could just compile and link with the version supported on your target machines.

在 cmake 中指定静态链接

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libgcc -static-libstdc++ -static")
16 浏览
0 评论