Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 652. ml64.exe와 link.exe x64 실행 환경 구성 [링크 복사], [링크+제목 복사]
조회: 4659
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)

ml64.exe와 link.exe x64 실행 환경 구성

지난 글에,

Windows 환경에서의 Hello World x64 어셈블리 예제 (MASM 버전)
; https://www.sysnet.pe.kr/2/0/13182

MASM + CRT 함수를 사용하는 경우 발생하는 컴파일 오류 정리
; https://www.sysnet.pe.kr/2/0/13183

masm을 이용한 hello world 예제를 컴파일 해봤는데요, "x64 Native Tools Command Prompt for VS 2022" 명령행 창을 이용해 편안하게 할 수 있었습니다.

그렇다면, 만약 그 환경이 아니라면 어떻게 될까요? ^^

일반적인 명령행 창에서 실행하려면, 우선 ml64.exe와 link.exe의 경로를 알아야 합니다. 이는 "x64 Native Tools Command Prompt for VS 2022" 창에서 쉽게 찾을 수 있습니다.

c:\temp> where ml64.exe
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64\ml64.exe

c:\temp> where link.exe
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64\link.exe

둘 다 "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64"에 위치하고 있으니 이런 경우 PATH 환경 변수에 그것을 등록해 주면 됩니다.

SET PATH=%PATH%;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64

일반 명령행 창에서 위의 PATH를 등록하고 지난 글의 예제(hello_cpp.asm)를 빌드하면,

c:\temp> ml64 hello_cpp.asm /link /nodefaultlib /subsystem:console ucrt.lib libcmt.lib vcruntime.lib legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib kernel32.lib /entry:mainCRTStartup
Microsoft (R) Macro Assembler (x64) Version 14.34.31933.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: hello_cpp.asm
Microsoft (R) Incremental Linker Version 14.34.31933.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/OUT:hello_cpp.exe
hello_cpp.obj
/nodefaultlib
/subsystem:console
ucrt.lib
libcmt.lib
vcruntime.lib
legacy_stdio_definitions.lib
legacy_stdio_wide_specifiers.lib
kernel32.lib
/entry:mainCRTStartup
LINK : fatal error LNK1181: cannot open input file 'ucrt.lib'

지정한 lib 파일을 찾을 수 없다고 오류가 발생합니다. 당연합니다. ^^ 해당 파일들을 어디서 찾아야 할지 모를 것입니다. 직접 확인을 해보면, vcruntime.lib, libcmt.lib, legacy_stdio_definitions.lib, legacy_stdio_wide_specifiers.lib는 다음의 경로에 있는 것을 찾고,

// 이 경로는 ml64.exe 경로에 포함된 "14.34.31933" 버전을 따릅니다.

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x64

ucrt.lib는 다음의 경로에서 찾습니다.

// 이 경로는 설치한 Windows Kits의 버전에 따라 달라질 수 있습니다.

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64

마지막으로 kernel32.lib 등의 Win32 DLL들은 다음의 경로에서 찾습니다.

// 이 경로는 설치한 Windows Kits의 버전에 따라 달라질 수 있습니다.

C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64

ml64.exe 명령행에 저 경로를 일일이 함께 입력해도 되겠지만, 그럼 빌드 명령어가 너무 길어져 문제입니다. 다행히, link.exe는 라이브러리 참조를 위한 환경 변수 LIB를 사용하는데요, 따라서 위의 경로들을 다음과 같이 등록해 주면 됩니다.

SET LIB=C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64

이제 다시 hello_cpp.asm을 빌드하면 정상적으로 완료됩니다.

참고로, 지금까지 알아본 ml64.exe 명령어에서는 "/link" 옵션을 통해 compile + linking을 함께 처리하고 있는데요, 이를 나눠서 다음과 같이 실행하는 것도 가능합니다.

c:\temp> SET PATH=%PATH%;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64

c:\temp> SET LIB=C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64

c:\temp> ml64 /c hello_cpp.asm
Microsoft (R) Macro Assembler (x64) Version 14.34.31933.0
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: hello_cpp.asm

c:\temp> link.exe hello_cpp.obj /nodefaultlib /subsystem:console ucrt.lib libcmt.lib vcruntime.lib legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib kernel32.lib /entry:mainCRTStartup
Microsoft (R) Incremental Linker Version 14.34.31933.0
Copyright (C) Microsoft Corporation.  All rights reserved.


c:\temp> hello_cpp.exe
Hello World!




Visual Studio의 경우 "Developer Command Prompt for VS 2022" 명령행 창이 제공됩니다. 아마도 대부분의 닷넷 개발자들은 "x64 Native Tools Command Prompt for VS 2022"보다는 그것을 더 자주 애용할 텐데요, 그 환경은 "x86"을 위한 빌드 환경으로 기본 세팅되어 있습니다.

실제로 그 명령행 창을 띄우고 link.exe의 경로를 보면,

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.4.2
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************

C:\Program Files\Microsoft Visual Studio\2022\Enterprise> where link.exe
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx86\x86\link.exe

이렇게 x86 경로가 나옵니다. 또한, 이 환경에서 link.exe는 경로에 있지만, ml64.exe는 없습니다. 따라서, 이것을 위한 경로를 추가해 줍니다.

SET PATH=%PATH%;C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64

하지만, "Developer Command Prompt for VS 2022" 명령행 창은 전반적으로 x86을 위한 환경 설정이 돼 있어서 linking하는 라이브러리들의 경로 역시 x86으로 잡혀 있습니다. 그래서 다음과 같은 식의 오류가 발생합니다.

c:\temp> link.exe hello_cpp.obj /nodefaultlib /subsystem:console ucrt.lib libcmt.lib vcruntime.lib legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib kernel32.lib /entry:mainCRTStartup
Microsoft (R) Incremental Linker Version 14.34.31933.0
Copyright (C) Microsoft Corporation.  All rights reserved.

LINK : error LNK2001: unresolved external symbol mainCRTStartup
hello_cpp.obj : error LNK2019: unresolved external symbol ExitProcess referenced in function main
hello_cpp.obj : error LNK2019: unresolved external symbol printf referenced in function main
C:\Program Files (x86)\Windows Kits\10\lib\10.0.22000.0\ucrt\x86\ucrt.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x86\libcmt.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x86\vcruntime.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x86\legacy_stdio_definitions.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x86\legacy_stdio_wide_specifiers.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22000.0\\um\x86\kernel32.lib : warning LNK4272: library machine type 'x86' conflicts with target machine type 'x64'
hello_cpp.exe : fatal error LNK1120: 3 unresolved externals

당황하지 마시고 ^^ 그냥 x64 라이브러리가 있는 디렉터리에서 찾으라고 LIB 환경 변수를 재정의하면 됩니다.

SET LIB=C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.34.31933\lib\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64;C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\um\x64




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 12/5/2022]

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

비밀번호

댓글 작성자
 




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/20233085스크립트: 58. 파이썬 - async/await 기본 사용법
13422정성태10/5/20233224닷넷: 2148. C# - async 유무에 따른 awaitable 메서드의 병렬 및 예외 처리
13421정성태10/4/20233255닷넷: 2147. C# - 비동기 메서드의 async 예약어 유무에 따른 차이
13420정성태9/26/20235357스크립트: 57. 파이썬 - UnboundLocalError: cannot access local variable '...' where it is not associated with a value
13419정성태9/25/20233109스크립트: 56. 파이썬 - RuntimeError: dictionary changed size during iteration
13418정성태9/25/20233790닷넷: 2146. C# - ConcurrentDictionary 자료 구조의 동기화 방식
13417정성태9/19/20233355닷넷: 2145. C# - 제네릭의 형식 매개변수에 속한 (매개변수를 가진) 생성자를 호출하는 방법
13416정성태9/19/20233161오류 유형: 877. redis-py - MISCONF Redis is configured to save RDB snapshots, ...
13415정성태9/18/20233643닷넷: 2144. C# 12 - 컬렉션 식(Collection Expressions)
13414정성태9/16/20233410디버깅 기술: 193. Windbg - ThreadStatic 필드 값을 조사하는 방법
13413정성태9/14/20233604닷넷: 2143. C# - 시스템 Time Zone 변경 시 이벤트 알림을 받는 방법
13412정성태9/14/20236877닷넷: 2142. C# 12 - 인라인 배열(Inline Arrays) [1]
13411정성태9/12/20233387Windows: 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/20233497닷넷: 2139. C# - ParallelEnumerable을 이용한 IEnumerable에 대한 병렬 처리
13406정성태9/4/20233450VS.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/20233298닷넷: 2137. ILSpy의 nuget 라이브러리 버전 - ICSharpCode.Decompiler
13401정성태8/19/20233535닷넷: 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  ...