Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 3개 있습니다.)
(시리즈 글이 3개 있습니다.)
Linux: 5. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency)
; https://www.sysnet.pe.kr/2/0/11845

개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
; https://www.sysnet.pe.kr/2/0/13490

Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/13494




Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency)

Visual C++의 경우 msvcr[version].dll에 대한 정적 링크를 다음과 같이 하면,

수동으로 구성해 본 VC++프로젝트 설정: ReleaseMinDependency
; https://www.sysnet.pe.kr/2/0/800

(* Visual Studio 버전에 따라 설정 옵션이 차이가 있습니다.)

순수하게 윈도우 시스템 dll에 대한 의존성만 갖게 됩니다. 마찬가지로 Linux 응용 프로그램도 그렇게 만들고 싶었는데요. 기본적으로는 다음과 같이 libstdc++, libgcc_s.so.1, libc.so.6에 대한 C/C++ 관련 런타임이 동적으로 바인딩되어 있습니다.

$ ldd ./projects/testapp/bin/x64/Debug/testapp.so
        linux-vdso.so.1 (0x00007ffdc17d4000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f71662ce000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f71660b6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7165cc5000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7165927000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f716686a000)

회사의 리눅스 개발자에 의하면, 다음의 옵션을 통해 libgcc_s와 libstdc++에 대한 의존성을 제거할 수 있다고 합니다.

[Linker / "Command Line"의 "Additional Options"]

-static-libgcc -static-libstdc++

빌드하면 이렇게 홀쭉해진 것을 볼 수 있습니다. ^^

~$ ldd ./projects/testapp/bin/x64/Debug/testapp.so
        linux-vdso.so.1 (0x00007ffd587e4000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2f521c9000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f2f527e7000)

그런데, CRT(C Runtime) 라이브러리인 libc.so.6에 대한 의존성을 제거할 수가 없습니다. 그래서 Visual C++와는 달리 libc 만큼은 빌드 환경과 실행 환경을 고려해야 합니다. (혹시 libc 의존성 없애는 방법을 아시는 분은 덧글 부탁드립니다. ^^)




검색해 보면, libc 의존성을 없애기 위해 "-static" 옵션을 주면 된다는 글들이 있습니다. 하지만 이 옵션을 [Linker / "Command Line"의 "Additional Options"]에 함께 주면 빌드 시 다음과 같은 식의 오류가 발생합니다.

1>  Linking objects
1>  Invoking 'ld'
1>  g++ -o "/home/usr23/projects/testapp/../testapp/bin/x64/Release/libtestapp.so" -Wl,--no-undefined -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -shared -static /home/usr23/projects/testapp/../testapp/obj/x64/Release/ClassFactory.o /home/usr23/projects/testapp/../testapp/obj/x64/Release/testapp.o /home/usr23/projects/testapp/../testapp/obj/x64/Release/dllmain.o /home/usr23/projects/testapp/../testapp/obj/x64/Release/ILRewriter.o
1>  /usr/bin/ld : error : /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
1>  /usr/bin/ld : error : final link failed: Nonrepresentable section on output
1>  collect2 : error : ld returned 1 exit status
1>  /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/crtbeginT.o: relocation R_X86_64_32 against hidden symbol `__TMC_END__' can not be used when making a shared object
1>  /usr/bin/ld: final link failed: Nonrepresentable section on output
1>  collect2: error: ld returned 1 exit status

__TMC_END__ 오류에 관한 것을 다시 검색해 보면,

how to make a static binary of coreutils?
; https://askubuntu.com/questions/530617/how-to-make-a-static-binary-of-coreutils

다음과 같이 기존 crtbeginT.o를 crtbeginS.o로 덮어씌우면 됩니다.

$ g++ --version
g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cd /usr/lib/gcc/x86_64-linux-gnu/7.3.0
$ sudo cp crtbeginT.o crtbeginT.orig.o
$ sudo crtbeginS.o crtbeginT.o

실제로 이렇게까지 처리하면 컴파일/링킹 오류는 발생하지 않습니다. 문제는 바이너리 의존성이 오히려 원래대로 돌아가 버립니다.

$ ldd ~/projects/testapp/bin/x64/Release/libtestapp.so
        linux-vdso.so.1 (0x00007fff71fe9000)
        libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa2814f0000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa2810ff000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa280d61000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fa281a8a000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa280b49000)




참고로 우분투의 경우 g++ 8 버전을 다음과 같이 설치할 수 있습니다.

$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update
$ sudo apt-get install gcc-8 g++-8
$ gcc-8 --version

그럼 /usr/lib/gcc/x86_64-linux-gnu/8 디렉터리에 g++ 8 버전 관련 파일들이 설치됩니다. 이렇게 설치해도 기본적으로 gcc, g++ 명령시에는 g++-8 컴파일러가 동작하지 않습니다. (이를 위해서는 별도의 처리를 해야 하는데 이건 나중에. ^^)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/20/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2019-05-20 10시26분
$ sudo apt install pax-utils
$ lddtree /bin/ps
정성태
2019-05-20 10시33분
The 101 of ELF files on Linux: Understanding and Analysis
; https://linux-audit.com/elf-binaries-on-linux-understanding-and-analysis/
정성태
2019-05-20 10시34분
정성태

1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...
NoWriterDateCnt.TitleFile(s)
13304정성태3/31/20234388VS.NET IDE: 181. Visual Studio - C/C++ 프로젝트에 application manifest 적용하는 방법
13303정성태3/30/20233732Windows: 241. 환경 변수 %PATH%에 DLL을 찾는 규칙
13302정성태3/30/20234359Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234454Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20234098Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20233870Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20233828Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234487Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233827Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234117Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234284.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234346오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234461Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20234835.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234328.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233521Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233634Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20233795Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234241Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20233827Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
13284정성태3/13/20234052Windows: 226. Win32 C/C++ - Dialog에서 값을 반환하는 방법파일 다운로드1
13283정성태3/12/20233589오류 유형: 852. 파이썬 - TypeError: coercing to Unicode: need string or buffer, NoneType found
13282정성태3/12/20233918Linux: 58. WSL - nohup 옵션이 필요한 경우
13281정성태3/12/20233846Windows: 225. 윈도우 바탕화면의 아이콘들이 넓게 퍼지는 경우 [2]
13280정성태3/9/20234597개발 환경 구성: 670. WSL 2에서 호스팅 중인 TCP 서버를 외부에서 접근하는 방법
13279정성태3/9/20234121오류 유형: 851. 파이썬 ModuleNotFoundError: No module named '_cffi_backend'
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...