none
/ENTRY causes multiple unrelated LNK2019 errors

    Question

  • The following code will not compile unless the uncommented #pragma is commented out.

    Visual Studio Version 14.0.25422.01 Update 3 (This code did not compiler under Update 2 either).

    #include <stdio.h>
    #include <stdlib.h>
    
    //#pragma comment(lib, "msvcrtd.lib") // This didn't help. . .
    #pragma comment(linker, "/ENTRY:entrypoint") // Comment me out and this will compile.
    
    int main() {
    	printf("main()\n");
    
    	system("PAUSE");
    
        return 0;
    }
    
    int entrypoint() {
    	printf("entrypoint()\n");
    
    	return main();
    }

    The project is a standard C++ Console program with no changes to any of the Project's settings.

    Similar (and more complex) code compiled without a problem in the past. This is the most simple example I can make to demonstrate the problem.

    Below is the output from the build.

    1>------ Rebuild All started: Project: entrypoint, Configuration: Debug Win32 ------
    1>  stdafx.cpp
    1>  entrypoint.cpp
    1>  Generating Code...
    1>entrypoint.obj : error LNK2019: unresolved external symbol __imp____acrt_iob_func referenced in function _printf
    1>entrypoint.obj : error LNK2019: unresolved external symbol __imp____stdio_common_vfprintf referenced in function __vfprintf_l
    1>entrypoint.obj : error LNK2019: unresolved external symbol __imp__system referenced in function _main
    1>MSVCRTD.lib(_init_.obj) : error LNK2019: unresolved external symbol __CrtDbgReport referenced in function __CRT_RTC_INIT
    1>MSVCRTD.lib(_init_.obj) : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function __CRT_RTC_INITW
    1>MSVCRTD.lib(_error_.obj) : error LNK2019: unresolved external symbol ___stdio_common_vsprintf_s referenced in function __vsprintf_s_l
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol __wmakepath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol __wsplitpath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol _wcscpy_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol ___vcrt_GetModuleFileNameW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol ___vcrt_GetModuleHandleW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)
    1>MSVCRTD.lib(_pdblkup_.obj) : error LNK2019: unresolved external symbol ___vcrt_LoadLibraryExW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)
    1>MSVCRTD.lib(_chandler4gs_.obj) : error LNK2019: unresolved external symbol __except_handler4_common referenced in function __except_handler4
    1>C:\Projects\C\entrypoint\Debug\entrypoint.exe : fatal error LNK1120: 13 unresolved externals
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

    Is there a solution to get this to compile correctly or is this a Visual Studio bug?

    I know what the LNK2019 error means, but defining my own entry point shouldn't suddenly make all of this msvcrtd.dll stuff unresovable.

    Thank you for your time.


    • Edited by Rasler Heios Nabradia Wednesday, July 13, 2016 1:43 AM changed msvcrt.dll to msvcrtd.dll to reflect that it is using the Debug version of the C Runtime.
    Wednesday, July 13, 2016 1:35 AM

Answers

All replies

  • Try adding this line:

    #pragma comment(linker, "/INCLUDE:_mainCRTStartup")


    Wednesday, July 13, 2016 5:08 AM
  • Thank you Viorel, that actually worked.

    I have no idea why it is necessary now when it wasn't in the past, but it worked.

    EDIT:

    Because the name for a x64 platform project isn't decorated with a leading underscore a little macro magic can be used to get the code to compile under both x86 and x64 platforms.

    #if defined(_M_IX86)
    # pragma comment(linker, "/INCLUDE:_mainCRTStartup")
    #else
    # pragma comment(linker, "/INCLUDE:mainCRTStartup")
    #endif

    Wednesday, July 13, 2016 5:15 AM