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분
정성태

... 16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  ...
NoWriterDateCnt.TitleFile(s)
12921정성태1/14/20225626개발 환경 구성: 624. Docker Desktop에서 별도 서버에 설치한 docker registry에 이미지 올리는 방법
12920정성태1/14/20226396오류 유형: 786. Camtasia - An error occurred with the camera: Failed to Add Video Sampler.
12919정성태1/13/20226226Windows: 199. Host Network Service (HNS)에 의해서 점유되는 포트
12918정성태1/13/20226443Linux: 47. WSL - shell script에서 설정한 환경 변수가 스크립트 실행 후 반영되지 않는 문제
12917정성태1/12/20225676오류 유형: 785. C# - The type or namespace name '...' could not be found (are you missing a using directive or an assembly reference?)
12916정성태1/12/20225408오류 유형: 784. TFS - One or more source control bindings for this solution are not valid and are listed below.
12915정성태1/11/20225677오류 유형: 783. Visual Studio - We didn't find any interpreters
12914정성태1/11/20227580VS.NET IDE: 172. 비주얼 스튜디오 2022의 파이선 개발 환경 지원
12913정성태1/11/20228122.NET Framework: 1133. C# - byte * (바이트 포인터)를 FileStream으로 쓰는 방법 [1]
12912정성태1/11/20228745개발 환경 구성: 623. ffmpeg.exe를 사용해 비디오 파일의 이미지를 PGM(Portable Gray Map) 파일 포맷으로 출력하는 방법 [1]
12911정성태1/11/20226099VS.NET IDE: 171. 비주얼 스튜디오 - 더 이상 만들 수 없는 "ASP.NET Core 3.1 Web Application (.NET Framework)" 프로젝트
12910정성태1/10/20226560제니퍼 .NET: 30. 제니퍼 닷넷 적용 사례 (8) - CPU high와 DB 쿼리 성능에 문제가 함께 있는 사이트
12909정성태1/10/20227961오류 유형: 782. Visual Studio 2022 설치 시 "Couldn't install Microsoft.VisualCpp.Redist.14.Latest"
12908정성태1/10/20225815.NET Framework: 1132. C# - ref/out 매개변수의 IL 코드 처리
12907정성태1/9/20226249오류 유형: 781. (youtube-dl.exe) 실행 시 "This app can't run on your PC" / "Access is denied." 오류 발생
12906정성태1/9/20226865.NET Framework: 1131. C# - 네임스페이스까지 동일한 타입을 2개의 DLL에서 제공하는 경우 충돌을 우회하는 방법 [1]파일 다운로드1
12905정성태1/8/20226523오류 유형: 780. Could not load file or assembly 'Microsoft.VisualStudio.TextTemplating.VSHost.15.0, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
12904정성태1/8/20228553개발 환경 구성: 623. Visual Studio 2022 빌드 환경을 위한 github Actions 설정 [1]
12903정성태1/7/20227148.NET Framework: 1130. C# - ELEMENT_TYPE_INTERNAL 유형의 사용 예
12902정성태1/7/20227184오류 유형: 779. SQL 서버 로그인 에러 - provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.
12901정성태1/5/20227274오류 유형: 778. C# - .NET 5+에서 warning CA1416: This call site is reachable on all platforms. '...' is only supported on: 'windows' 경고 발생
12900정성태1/5/20228922개발 환경 구성: 622. vcpkg로 ffmpeg를 빌드하는 경우 생성될 구성 요소 제어하는 방법
12899정성태1/3/20228408개발 환경 구성: 621. windbg에서 python 스크립트 실행하는 방법 - pykd (2)
12898정성태1/2/20228961.NET Framework: 1129. C# - ffmpeg(FFmpeg.AutoGen)를 이용한 비디오 인코딩 예제(encode_video.c) [1]파일 다운로드1
12897정성태1/2/20227843.NET Framework: 1128. C# - 화면 캡처한 이미지를 ffmpeg(FFmpeg.AutoGen)로 동영상 처리 [4]파일 다운로드1
12896정성태1/1/202210709.NET Framework: 1127. C# - FFmpeg.AutoGen 라이브러리를 이용한 기본 프로젝트 구성파일 다운로드1
... 16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  ...