성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] VT sequences to "CONOUT$" vs. STD_O...
[정성태] NetCoreDbg is a managed code debugg...
[정성태] Evaluating tail call elimination in...
[정성태] What’s new in System.Text.Json in ....
[정성태] What's new in .NET 9: Cryptography ...
[정성태] 아... 제시해 주신 "https://akrzemi1.wordp...
[정성태] 다시 질문을 정리할 필요가 있을 것 같습니다. 제가 본문에...
[이승준] 완전히 잘못 짚었습니다. 댓글 지우고 싶네요. 검색을 해보...
[정성태] 우선 답글 감사합니다. ^^ 그런데, 사실 저 예제는 (g...
[이승준] 수정이 안되어서... byteArray는 BYTE* 타입입니다...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>MASM + CRT 함수를 사용하는 경우 발생하는 컴파일 오류 정리</h1> <p> C/C++ 함수를 사용하는 경우 발생하는 링커 오류를 정리합니다.<br /> <br /> <a target='tab' href='https://www.sysnet.pe.kr/2/0/13182'>이전 글</a>에서 마지막 <a target='tab' href='https://www.sysnet.pe.kr/2/0/13182#hello_cpp_asm'>hello_cpp.asm</a>을 컴파일할 때, 그냥 Win32 API 호출하던 예제를 컴파일하듯이 명령어를 주면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib /entry:main </pre> <br /> 당연히 printf 함수를 찾을 수 없다는 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > hello_cpp.obj : error LNK2019: unresolved external symbol printf referenced in function main </pre> <br /> 그래서 단순히 이에 대한 정의만 포함하고 있는 legacy_stdio_definitions.lib를 추가하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib <span style='color: blue; font-weight: bold'>legacy_stdio_definitions.lib</span> /entry:main </pre> <br /> 이제는 더 많은 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __acrt_iob_func referenced in function _vwprintf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfwprintf referenced in function _vfwprintf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfwprintf_s referenced in function _vfwprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfwprintf_p referenced in function _vfwprintf_p_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfwscanf referenced in function _vfwscanf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vswprintf referenced in function _vsnwprintf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vswprintf_s referenced in function _vswprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsnwprintf_s referenced in function _vsnwprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vswprintf_p referenced in function _vswprintf_p_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vswscanf referenced in function _vswscanf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfprintf referenced in function _vfprintf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfprintf_s referenced in function _vfprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfprintf_p referenced in function _vfprintf_p_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vfscanf referenced in function _vfscanf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsprintf referenced in function _vsnprintf_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsprintf_s referenced in function _vsprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsnprintf_s referenced in function _vsnprintf_s_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsprintf_p referenced in function _vsprintf_p_l legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2019: unresolved external symbol __stdio_common_vsscanf referenced in function _vsscanf_l hello_cpp.exe : fatal error LNK1120: 19 unresolved externals </pre> <br /> 이 함수들에 대한 정의를 포함한 ucrt.lib를 다시 한 번 더 추가해야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib <span style='color: blue; font-weight: bold'>ucrt.lib</span> legacy_stdio_definitions.lib /entry:main </pre> <br /> 이제는 빌드까지는 잘 되지만 찜찜한 링크 경고가 하나 남습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > legacy_stdio_wide_specifiers.lib(legacy_stdio_wide_specifiers.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators </pre> <br /> 즉, <a target='tab' href='https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-initialization'>CRT 초기화</a>를 위한 진입점을 호출해야 하는 건데요, 따라서 진입점을 우리가 만든 main이 아니라 CRT의 main에 해당하는 mainCRTStartup로 /entry 옵션을 잡아줘야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib ucrt.lib legacy_stdio_definitions.lib /entry:main <span style='color: blue; font-weight: bold'>/entry:mainCRTStartup</span> </pre> <br /> 하지만 ^^; 그래도 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > LINK : error LNK2001: unresolved external symbol mainCRTStartup </pre> <br /> 그래서 mainCRTStartup를 정의한 libcmt.lib를 추가하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib <span style='color: blue; font-weight: bold'>libcmt.lib</span> ucrt.lib legacy_stdio_definitions.lib /entry:main /entry:mainCRTStartup </pre> <br /> 될 듯도 한데, 분명히 완벽한 데도 불구하고 오류가 발생합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > libucrt.lib(exit.obj) : error LNK2005: _c_exit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(exit.obj) : error LNK2005: _cexit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(exit.obj) : error LNK2005: _exit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(exit.obj) : error LNK2005: _register_thread_local_exe_atexit_callback already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(exit.obj) : error LNK2005: exit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(onexit.obj) : error LNK2005: _crt_at_quick_exit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(onexit.obj) : error LNK2005: _crt_atexit already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(onexit.obj) : error LNK2005: _execute_onexit_table already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(onexit.obj) : error LNK2005: _initialize_onexit_table already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(onexit.obj) : error LNK2005: _register_onexit_function already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(argv_data.obj) : error LNK2005: __p___argc already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(argv_data.obj) : error LNK2005: __p___argv already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(matherr.obj) : error LNK2005: __setusermatherr already defined in ucrt.lib(api-ms-win-crt-math-l1-1-0.dll) libucrt.lib(environment_initialization.obj) : error LNK2005: _get_initial_narrow_environment already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(environment_initialization.obj) : error LNK2005: _initialize_narrow_environment already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(wsetlocale.obj) : error LNK2005: _configthreadlocale already defined in ucrt.lib(api-ms-win-crt-locale-l1-1-0.dll) libucrt.lib(exception_filter.obj) : error LNK2005: _seh_filter_dll already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(exception_filter.obj) : error LNK2005: _seh_filter_exe already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) libucrt.lib(new_mode.obj) : error LNK2005: _set_new_mode already defined in ucrt.lib(api-ms-win-crt-heap-l1-1-0.dll) libucrt.lib(report_runtime_error.obj) : error LNK2005: _set_app_type already defined in ucrt.lib(api-ms-win-crt-runtime-l1-1-0.dll) hello_cpp.exe : fatal error LNK1169: one or more multiply defined symbols found </pre> <br /> 이 오류는, 명령행의 인자로 지정한 라이브러리 순서가 libcmt.lib + ucrt.lib인 것을 거꾸로 ucrt.lib + libcmt.lib 순으로 해주면 정상적으로 빌드가 됩니다. ^^;<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /subsystem:console kernel32.lib <span style='color: blue; font-weight: bold'>ucrt.lib libcmt.lib</span> legacy_stdio_definitions.lib /entry:main /entry:mainCRTStartup </pre> <br /> 자, 그럼 이 상태에서 <a target='tab' href='https://learn.microsoft.com/en-us/cpp/build/reference/nodefaultlib-ignore-libraries'>nodefaultlib</a> 옵션을 주면 어떨까요?<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link <span style='color: blue; font-weight: bold'>/nodefaultlib</span> /subsystem:console kernel32.lib ucrt.lib libcmt.lib legacy_stdio_definitions.lib /entry:main /entry:mainCRTStartup </pre> <br /> 이런 오류들이 쏟아져 나오는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > libcmt.lib(exe_main.obj) : error LNK2001: unresolved external symbol __C_specific_handler libcmt.lib(utility.obj) : error LNK2001: unresolved external symbol __C_specific_handler libcmt.lib(tncleanup.obj) : error LNK2019: unresolved external symbol __std_type_info_destroy_list referenced in function "void __cdecl __scrt_uninitialize_type_info(void)" (?__scrt_uninitialize_type_info@@YAXXZ) libcmt.lib(utility_desktop.obj) : error LNK2019: unresolved external symbol __current_exception referenced in function __scrt_unhandled_exception_filter libcmt.lib(utility_desktop.obj) : error LNK2019: unresolved external symbol __current_exception_context referenced in function __scrt_unhandled_exception_filter libcmt.lib(utility_desktop.obj) : error LNK2019: unresolved external symbol memset referenced in function __scrt_fastfail legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2001: unresolved external symbol __PLEASE_LINK_WITH_legacy_stdio_wide_specifiers.lib </pre> <br /> vcruntime.lib와 링크시키면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /nodefaultlib /subsystem:console kernel32.lib <span style='color: blue; font-weight: bold'>vcruntime.lib</span> ucrt.lib libcmt.lib legacy_stdio_definitions.lib /entry:main /entry:mainCRTStartup </pre> <br /> 그래도 여전히 오류가 발생합니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > legacy_stdio_definitions.lib(legacy_stdio_definitions.obj) : error LNK2001: unresolved external symbol __PLEASE_LINK_WITH_legacy_stdio_wide_specifiers.lib </pre> <br /> 다행히 이번에는 오류 메시지에서 legacy_stdio_wide_specifiers.lib를 포함해야 한다는 것을 알 수 있습니다. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > ml64 hello_cpp.asm /link /nodefaultlib /subsystem:console kernel32.lib vcruntime.lib ucrt.lib libcmt.lib legacy_stdio_definitions.lib <span style='color: blue; font-weight: bold'>legacy_stdio_wide_specifiers.lib</span> /entry:main /entry:mainCRTStartup </pre> <br /> 이제야 끝이 났습니다. ^^ 정리해 보면, C 런타임을 사용하는 경우 기본적으로 다음과 같은 라이브러리를 함께 링크시켜야 합니다 ^^<br /> <br /> <ul> <li>vcruntime.lib </li> <li>ucrt.lib </li> <li>libcmt.lib </li> <li>legacy_stdio_definitions.lib </li> <li>legacy_stdio_wide_specifiers.lib</li> </ul> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1005
(왼쪽의 숫자를 입력해야 합니다.)