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)
13426정성태10/13/20233287스크립트: 59. 파이썬 - 비동기 호출 함수(run_until_complete, run_in_executor, create_task, run_in_threadpool)
13425정성태10/11/20233105닷넷: 2149. C# - PLinq의 Partitioner<T>를 이용한 사용자 정의 분할파일 다운로드1
13423정성태10/6/20233083스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233224닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233254닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/20235356스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/20233108스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/20233790닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/20233353닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/20233161오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/20233642닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions)
13414정성태9/16/20233409디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/20233603닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
13412정성태9/14/20236877닷넷: 2142. C# 12 - 인라인 배열(Inline Arrays) [1]
13411정성태9/12/20233384Windows: 252. 권한 상승 전/후 따로 관리되는 공유 네트워크 드라이브 정보
13410정성태9/11/20234894닷넷: 2141. C# 12 - Interceptor (컴파일 시에 메서드 호출 재작성) [1]
13409정성태9/8/20233751닷넷: 2140. C# - Win32 API를 이용한 모니터 전원 끄기
13408정성태9/5/20233739Windows: 251. 임의로 만든 EXE 파일을 포함한 ZIP 파일의 압축을 해제할 때 Windows Defender에 의해 삭제되는 경우
13407정성태9/4/20233495닷넷: 2139. C# - ParallelEnumerable을 이용한 IEnumerable에 대한 병렬 처리
13406정성태9/4/20233449VS.NET IDE: 186. Visual Studio Community 버전의 라이선스
13405정성태9/3/20233869닷넷: 2138. C# - async 메서드 호출 원칙
13404정성태8/29/20233398오류 유형: 876. Windows - 키보드의 등호(=, Equals sign) 키가 눌리지 않는 경우
13403정성태8/21/20233230오류 유형: 875. The following signatures couldn't be verified because the public key is not available: NO_PUBKEY EB3E94ADBE1229CF
13402정성태8/20/20233296닷넷: 2137. ILSpy의 nuget 라이브러리 버전 - ICSharpCode.Decompiler
13401정성태8/19/20233533닷넷: 2136. .NET 5+ 환경에서 P/Invoke의 성능을 높이기 위한 SuppressGCTransition 특성 [1]
13400정성태8/10/20233376오류 유형: 874. 파이썬 - pymssql을 윈도우 환경에서 설치 불가
1  2  3  4  5  6  7  [8]  9  10  11  12  13  14  15  ...