调用Cmake连接指定的GLIBC

如何使用CMAKE或者GCC连接指定的GLIBC运行程序

安装新的GLIBC 2.34

wget 下载源码包

wget https://ftp.gnu.org/gnu/glibc/glibc-2.34.tar.gz

tar 解压压缩包

tar xzfv glibc-2.34.tar.gz

configure 设置一下安装路径

./configure –prefix=/opt/glibc-2.34

--prefix= 你要存放的路径(建议不要覆盖自带的GLIBC)

make -j4

-j 你需要开启的线程数目(用来加快编译速度)

sudo make install

sudo 因为我是安装到/opt目录下, 所以需要sudo

测试用例

转自 https://stackoverflow.com/questions/9705660/check-glibc-version-for-a-particular-gcc-compiler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
#ifdef __GLIBC__
#include <gnu/libc-version.h>
#endif

int
main(void)
{
#ifdef __GLIBC__
printf("GNU libc compile-time version: %u.%u\n", __GLIBC__, __GLIBC_MINOR__);
printf("GNU libc runtime version: %s\n", gnu_get_libc_version());
return 0;
#else
puts("Not the GNU C Library");
return 1;
#endif
}

简单运行一遍

GCCLinkNewGlibc-1

我系统自带的是GLIBC3.31

GCC编译命令

1
g++ -std=c++11 ../helloworld.cc -Wl,--rpath=/opt/glibc-2.34/lib -Wl,--dynamic-linker=/opt/glibc-2.34/lib/ld-linux-x86-64.so.2

-Wl,--rpath= 重新指定库的路径
-Wl,--dynamic-linker= 指定动态连接库

GCCLinkNewGlibc-2

CMAKE编译

1
2
3
4
5
6
7
8
9
10
cmake_minimum_required(VERSION 3.15)

project(helloworld)

include_directories(./)

add_compile_options("-std=c++11")
set(CMAKE_CXX_FLAGS "-Wl,--rpath=/opt/glibc-2.34/lib -Wl,--dynamic-linker=/opt/glibc-2.34/lib/ld-linux-x86-64.so.2")

add_executable(hello helloworld.cc)

主要的是add_compile_options中关于CXXFLAGS,其实这个跟GCC一样

GCCLinkNewGlibc-3

结论

调用rpath以及dynamic-linker可以指定GLIBC