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)
13503정성태12/27/20232191Linux: 67. WSL 환경 + mlocate(locate) 도구의 /mnt 디렉터리 검색 문제
13502정성태12/26/20232298닷넷: 2187. C# - 다른 프로세스의 환경변수 읽는 예제파일 다운로드1
13501정성태12/25/20232098개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232180디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20232857닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232296오류 유형: 885. Visual Studiio - error : Could not connect to the remote system. Please verify your connection settings, and that your machine is on the network and reachable.
13496정성태12/21/20232311Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232322Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232502Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232564닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232266개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
13491정성태12/19/20232234Linux: 63. 리눅스 - 다중 그룹 또는 사용자를 리소스에 권한 부여
13490정성태12/19/20232350개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/20232138개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20232070오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
13487정성태12/16/20232384개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행 [1]
13486정성태12/15/20232199개발 환경 구성: 695. Nuget config 파일에 값 설정/삭제 방법
13485정성태12/15/20232088오류 유형: 883. dotnet build/restore - error : Root element is missing
13484정성태12/14/20232162개발 환경 구성: 694. Windows 디렉터리 경로를 WSL의 /mnt 포맷으로 구하는 방법
13483정성태12/14/20232302닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법파일 다운로드1
13482정성태12/13/20232878닷넷: 2183. C# - eFriend Expert OCX 예제를 .NET Core/5+ Console App에서 사용하는 방법 [2]파일 다운로드1
13481정성태12/13/20232272개발 환경 구성: 693. msbuild - .NET Core/5+ 프로젝트에서 resgen을 이용한 리소스 파일 생성 방법파일 다운로드1
13480정성태12/12/20232617개발 환경 구성: 692. Windows WSL 2 + Chrome 웹 브라우저 설치
13479정성태12/11/20232311개발 환경 구성: 691. WSL 2 (Ubuntu) + nginx 환경 설정
13477정성태12/8/20232485닷넷: 2182. C# - .NET 7부터 추가된 Int128, UInt128 [1]파일 다운로드1
13476정성태12/8/20232213닷넷: 2181. C# - .NET 8 JsonStringEnumConverter의 AOT를 위한 개선파일 다운로드1
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...