Saturday, July 11, 2020

Hello World app building errors

I build a simple application from the book in Allan Feuer's first example. I got Hello World app building errors. then I research these errors and the leaned many good configurations. documented here for future reference. it touched a lot of configurations in Visual Studio on DLL linking though this Hello Word does not use MFC DLL.

I documented all my research for future reference on these configurations of Visual Studio C++ project.

These two action items are very important:

Action 1. configuration property->linker->system->sub system: Windows

Action 2. configuration property->C/C++ -> code generation -> runtime library -> MT , MTD etc..

the short summary would be:

If you are using “Use of MFC -> Use MFC in a Static Library” then you must use /MT (release) or /MTd (debug) CRT library.

If you are using “Use of MFC -> Use MFC in a Shared DLL” then you must use /MD (release) or /MDd (debug) CRT library.

The following topic discusses the various .lib files that comprise the C run-time libraries as well as their associated compiler options and preprocessor directives.

The C Run-time Library (CRT) is the part of the C++ Standard Library that incorporates the ISO C99 standard library. The Visual C++ libraries that implement the CRT support native code development, and both mixed native and managed code. All versions of the CRT support multi-threaded development. Most of the libraries support both static linking, to link the library directly into your code, or dynamic linking to let your code use common DLL files.

Starting in Visual Studio 2015, the CRT has been refactored into new binaries. The Universal CRT (UCRT) contains the functions and globals exported by the standard C99 CRT library. The UCRT is now a Windows component, and ships as part of Windows 10. The static library, DLL import library, and header files for the UCRT are now found in the Windows 10 SDK. When you install Visual C++, Visual Studio setup installs the subset of the Windows 10 SDK required to use the UCRT. You can use the UCRT on any version of Windows supported by Visual Studio 2015 and later versions. You can redistribute it using vcredist for supported versions of Windows other than Windows 10. For more information, see Redistributing Visual C++ Files.

  • CRT Library Features
  • warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs
  • one new configuration I paid attention:

    2) C/C++ -> Code Generation -> Basic Runtime Checks as Both (/RTC1, equiv. to /RTCsu)

  • Warning lnk4098: defaultlib "msvcrt" conflicts with use of other libs; Use/nodefaultlib
  • There is a table to show all kinds of combinations of Runtime library to be used.

    it seems like the above post is translated from the following article in Chinese:

  • warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library
  • warning LNK4098: defaultlib "MSVCRT" conflicts with use of other libs; use /NODEFAULTLIB:library
  • warning LNK4098: 默认库“MSVCRT”与其他库的使用冲突;使用 /NODEFAULTLIB:library问题解决方法
  • Guide - How to avoid C/C++ runtime on Windows
  • Avoiding the Visual C++ Runtime Library
  • solution is:

    Link Warning Solutions: Engineering Attributes - > Linker - > Input - > ignore specific default liries by adding the omitted item msvcrt.lib.


    I got following errors:

    Error (active) E0035 #error directive: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] HelloWin C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\atlmfc\include\afx.h 24

    1. error Please #define _AFXDLL or do not use /MD[d] occurs even after making changes in Project Properties
    2. if I use MFC static link library, I got this error after recompiling it:

      Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) HelloWin C:\Demo\HelloWin\HelloWin\msvcrtd.lib(exe_main.obj) 1

    3. /MD, /MT, /LD (Use Run-Time Library)
    4. Error LNK2019 unresolved external symbol _main referenced in function “int __cdecl invoke_main(void)” (?invoke_main@@YAHXZ)
    5. I made this change:

      Check project configuration. Linker->System->SubSystem should be Windows.

    6. What is an undefined reference/unresolved external symbol error and how do I fix it?
    7. #error Please use the /MD switch for _AFXDLL builds
    8. Settings for CRT linking and MFC linking must be coherent. So, actually, there are two possible answers at this question:

      step 1. Use /MT (Properties -> C/C++ -> Code Generation) and static MFC (Properties -> General -> Use of MFC)

      step 2. Use /MD (Properties -> C/C++ -> Code Generation) and shared MFC (Properties -> General -> Use of MFC)

    9. Error C1189 #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d] Hello2 c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\atlmfc\include\afx.h 24

      based on above descriptions, I selected MFC shared DLL and C/C++ -> code generation -> runtime library -> /MD

      Warning LNK4098 defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library Hello2 C:\Demo\Hello2\Hello2\LINK 1

    10. How to use /NODEFAULTLIBS option in compilation?
    11. Resolving LNK4098: defaultlib 'MSVCRT' conflicts with
    12. Unresolved external symbols while moving C++ MFC-based application to VS2015 from VS2013

    No comments:

    Post a Comment