我通过将C ++代码与标准库C ++库链接而收到LNK2019错误。我需要知道哪些库包含功能:
is_c_termination_complete
,__acrt_initialize
,__acrt_uninitialize
,__acrt_uninitialize_critical
,__acrt_thread_attach
,__acrt_thread_detach
将其包含在链接过程中。
我正在用MS Visual Studio社区2017和Intel Parallel Studio xe 2019 update 1用C ++编写代码,这些代码将集成到第三方软件中。第三方软件提供了一个“ make”选项来编译目标文件并将它们链接在一起。
编译正常,链接提供了一个问题。
第三方软件提供了一个条目,以变量的形式提供基本的链接器选项。默认选项如下:
link_sl='LINK', '/nologo', '/NOENTRY', '/INCREMENTAL:NO', '/subsystem:console', '/machine:AMD64',
' /NODEFAULTLIB:LIBC.LIB', '/NODEFAULTLIB:LIBCMT.LIB','/DEFAULTLIB:OLDNAMES.LIB', '/DEFAULTLIB:LIBIFCOREMD.LIB', '/DEFAULTLIB:LIBIFPORTMD.LIB', '/DEFAULTLIB:LIBMMD.LIB', '/DEFAULTLIB:kernel32.lib', '/DEFAULTLIB:user32.lib', '/DEFAULTLIB:advapi32.lib','/FIXED:NO', '/dll','/def:%E', '/out:%U', '%F', '%A', '%L', '%B',
'oldnames.lib', 'user32.lib', 'ws2_32.lib', 'netapi32.lib','advapi32.lib',
'msvcrt.lib', 'vcruntime.lib', 'ucrt.lib']
链接时出现以下11个错误:
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __vcrt_initialize referenced in function __scrt_initialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __vcrt_uninitialize referenced in function __scrt_initialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __vcrt_uninitialize_critical referenced in function __scrt_dllmain_uninitialize_critical
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __vcrt_thread_attach referenced in function __scrt_dllmain_crt_thread_attach
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __vcrt_thread_detach referenced in function __scrt_dllmain_crt_thread_attach
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol _is_c_termination_complete referenced in function __scrt_dllmain_uninitialize_c
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_initialize referenced in function __scrt_initialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize referenced in function __scrt_uninitialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize_critical referenced in function __scrt_dllmain_uninitialize_critical
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_attach referenced in function __scrt_dllmain_crt_thread_attach
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_detach referenced in function __scrt_dllmain_crt_thread_detach
这基本上意味着我没有包括所有必需的库。 我已经发现,在选项中包含库'libvcruntime.lib'可以将错误减少到6个。因此,使用:
link_sl='LINK', '/nologo', '/NOENTRY', '/INCREMENTAL:NO', '/subsystem:console', '/machine:AMD64',
' /NODEFAULTLIB:LIBC.LIB', '/NODEFAULTLIB:LIBCMT.LIB','/DEFAULTLIB:OLDNAMES.LIB', '/DEFAULTLIB:LIBIFCOREMD.LIB', '/DEFAULTLIB:LIBIFPORTMD.LIB', '/DEFAULTLIB:LIBMMD.LIB', '/DEFAULTLIB:kernel32.lib', '/DEFAULTLIB:user32.lib', '/DEFAULTLIB:advapi32.lib','/FIXED:NO', '/dll','/def:%E', '/out:%U', '%F', '%A', '%L', '%B',
'oldnames.lib', 'user32.lib', 'ws2_32.lib', 'netapi32.lib','advapi32.lib',
'msvcrt.lib', 'vcruntime.lib', 'ucrt.lib',**'libvcruntime.lib'**]
结果:
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol _is_c_termination_complete referenced in function __scrt_dllmain_uninitialize_c
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_initialize referenced in function __scrt_initialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize referenced in function __scrt_uninitialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize_critical referenced in function __scrt_dllmain_uninitialize_critical
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_attach referenced in function __scrt_dllmain_crt_thread_attach
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_detach referenced in function __scrt_dllmain_crt_thread_detach
我还需要在链接过程中包括哪些其他库来解决这些问题?
答案 0 :(得分:0)
我们遇到了完全相同的问题。通过在Visual Studio中创建一个简单的控制台项目,我们发现以下代码:
#include <stdio.h>
extern "C" {
int _is_c_termination_complete(...);
int __acrt_initialize(...);
int __acrt_uninitialize(...);
}
int main()
{
if(_is_c_termination_complete())
{
__acrt_initialize();
}
fprintf(stdout, "Hello world\n");
return 0;
}
在拆卸时将其终止。
main:
<snip>
00007FF68B44194A call __scrt_stub_for_is_c_termination_complete (07FF68B441140h)
00007FF68B44194F test eax,eax
00007FF68B441951 je main+38h (07FF68B441958h)
00007FF68B441953 call __acrt_initialize (07FF68B441393h)
00007FF68B441958 mov ecx,1
00007FF68B44195D call qword ptr [__imp___acrt_iob_func (07FF68B4502F0h)]
00007FF68B441963 lea rdx,[string "Hello world\n" (07FF68B449C28h)]
00007FF68B44196A mov rcx,rax
00007FF68B44196D call fprintf (07FF68B44119Ah)
00007FF68B441972 xor eax,eax
00007FF68B441974 lea rsp,[rbp+0C8h]
00007FF68B44197B pop rdi
00007FF68B44197C pop rbp
链接器显然将_is_c_termination_complete重写为__scrt_stub_for_is_c_termination_complete。据我们所知,函数_is_c_termination_complete在任何特定crt中都不存在。不同的crt似乎有不同的实现方式,链接器似乎会根据您要链接的crt重写这些实现。
对于我们来说,从链接行中删除/ NOENTRY选项可修复6个错误。显然,/ NOENTRY阻止了链接器提取crt的实现。
显然,/ NOENTRY实际上意味着没有代码,我们肯定有。
https://docs.microsoft.com/en-us/cpp/build/reference/noentry-no-entry-point?view=vs-2019
答案 1 :(得分:0)
在此第三方软件提供的框架内工作时,我遇到了完全相同的问题。尽管我无法直接回答查找包含_is_c_termination_complete
的库的问题,但我意识到仍然可以使您的代码正常工作:只需在/FORCE
标志列表中添加一个link_sl
标志。 / p>
根据MSVC official documentation:
/ FORCE选项告诉链接器创建有效的.exe文件或DLL,即使已引用但未定义符号或已将符号多重定义的情况。
因此,LNK2019
错误消息将不会阻止链接程序生成dll
库,这对于运行第三方软件至关重要。链接器消息如下所示:
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol _is_c_termination_complete referenced in function __scrt_dllmain_uninitialize_c
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_initialize referenced in function __scrt_initialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize referenced in function __scrt_uninitialize_crt
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_uninitialize_critical referenced in function __scrt_dllmain_uninitialize_critical
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_attach referenced in function __scrt_dllmain_crt_thread_attach
msvcrt.lib(utility.obj) : error LNK2019: unresolved external symbol __acrt_thread_detach referenced in function __scrt_dllmain_crt_thread_detach
: warning LNK4088: image being generated due to /FORCE option; image may not run
msvcprt.lib(locale0_implib.obj) : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators
这当然不是解决方案,也不保证在任何情况下都可以使用,但是至少对我的代码有效。