我通过将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
    

    我还需要在链接过程中包括哪些其他库来解决这些问题?

    最佳答案

    我们遇到了完全相同的问题。通过在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似乎具有不同的实现,链接器似乎会根据链接的目标重写这些实现。

    对于我们来说,从链接行中删除/ NOENTRY选项可修复6个错误。显然,/ NOENTRY阻止链接器提取crt的实现。

    显然,/ NOENTRY实际上意味着没有代码,而我们绝对有。

    https://docs.microsoft.com/en-us/cpp/build/reference/noentry-no-entry-point?view=vs-2019

    关于c++ - 哪个库包含_is_c_termination_complete,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54124505/

    10-09 13:25