来由:生产环境的Centos6.4,gcc 4.4.7,不支持C++11,如果全部升级gcc费劲,尝试以下办法解决

1.在centos6.4虚拟机上编译gcc5.3.0,安装在/usr/local/gcc-5.3.0目录下

2.写CMakeList文件,修改rpath目录为运行目录,具体内容是

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
project(test)

SET(CMAKE_C_COMPILER /usr/local/gcc-5.3.0/bin/gcc)
SET(CMAKE_CXX_COMPILER /usr/local/gcc-5.3.0/bin/g++)

INCLUDE_DIRECTORIES(/usr/local/gcc-5.3.0/include/c++/5.3.0/  ./)
LINK_DIRECTORIES(/usr/local/gcc-5.3.0/lib64/)
#以下为修改rpath
SET(CMAKE_SKIP_RPATH FALSE) 
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath,'$ORIGIN'" )

MESSAGE("build test")

IF (NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Debug)
ENDIF(NOT CMAKE_BUILD_TYPE)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=gnu99 -pedantic -s  -Wunused-function")#-Wno-long-long
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Os -ffunction-sections -fdata-sections")#-O2 -Os 

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -s -std=c++11 -fpermissive")#-Wno-long-long
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}  -g")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os -ffunction-sections -fdata-sections")#-O2 -Os 
SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -Wl,--gc-sections")

MESSAGE("makefile in build mode: ${CMAKE_C_FLAGS}")

ADD_EXECUTABLE(
	test
	test.cpp
)

TARGET_LINK_LIBRARIES(
	test
	dl
	pthread
)
3.编写测试程序

#include <stdlib.h>
#include <stdio.h>
#include <thread>
int main(void)
{
	std::thread t([]()
	{
		printf("hello,world!\n");
	});
	t.join();
	return 0;
}


4.执行命令

cmake ./src/ -DCMAKE_BUILD_TYPE=Debug && make

5.查看rpath

readelf -d ./test 

Dynamic section at offset 0x4980 contains 26 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000f (RPATH)              Library rpath: [$ORIGIN:/usr/local/gcc-5.3.0/lib64]
 0x000000000000000c (INIT)               0x401b68
 0x000000000000000d (FINI)               0x4033a8
 0x0000000000000004 (HASH)               0x400240
 0x0000000000000005 (STRTAB)             0x400c88
 0x0000000000000006 (SYMTAB)             0x4004a8
 0x000000000000000a (STRSZ)              3027 (bytes)
 0x000000000000000b (SYMENT)             24 (bytes)
 0x0000000000000015 (DEBUG)              0x0
 0x0000000000000003 (PLTGOT)             0x604b78
 0x0000000000000002 (PLTRELSZ)           360 (bytes)
 0x0000000000000014 (PLTREL)             RELA
 0x0000000000000017 (JMPREL)             0x401a00
 0x0000000000000007 (RELA)               0x4019b8
 0x0000000000000008 (RELASZ)             72 (bytes)
 0x0000000000000009 (RELAENT)            24 (bytes)
 0x000000006ffffffe (VERNEED)            0x401908
 0x000000006fffffff (VERNEEDNUM)         4
 0x000000006ffffff0 (VERSYM)             0x40185c
 0x0000000000000000 (NULL)               0x0

6.查看依赖的运行库

[root@*****] ldd ./test
	linux-vdso.so.1 =>  (0x00007fff3c92b000)
	libdl.so.2 => /lib64/libdl.so.2 (0x0000003753800000)
	libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003753c00000)
	libstdc++.so.6 => /usr/local/gcc-5.3.0/lib64/libstdc++.so.6 (0x00007fa8c6207000)
	libm.so.6 => /lib64/libm.so.6 (0x0000003754800000)
	libgcc_s.so.1 => /usr/local/gcc-5.3.0/lib64/libgcc_s.so.1 (0x00007fa8c5ff0000)
	libc.so.6 => /lib64/libc.so.6 (0x0000003753400000)        
	/lib64/ld-linux-x86-64.so.2 (0x0000003753000000)

看出依赖两个库:libstdc++.so.6 libgcc_s.so.1

7.拷贝依赖库和程序到生产机器上目录test内,在上一级目录执行验证

./test/test

02:07 [root@*****]$ ./test/test 
hello,world!
tty:[1] jobs:[0] cwd:[/opt/xx]







Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐