keywords: C++, std_Binderstd_Unforced, sockaddr, int __cdecl invoke_main(void)

unresolved external symbol main referenced in function “int __cdecl invoke_main(void)”

Error:

MSVCRTD.lib(exe_main.obj): Error LNK2019 : unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)

Solution:
Add /subsystem:windows:

#ifdef _WIN32
#pragma comment(linker, "/subsystem:windows")
#include <windows.h>
#endif
cannot convert from ‘std::_Binder’

Error:

Error C2440 : 'initializing': cannot convert from 'std::_Binder<std::_Unforced,int &,sockaddr *,size_t>' to 'int'

Solution:
Give a global function symbol.

int rs = ::bind(ServerSocket, (sockaddr*)&oAddr, sizeof(oAddr));
Failed to create virtual memory for PCH

Error:

c1xx: Error C3859 : Failed to create virtual memory for PCH
  c1xx: note: the system returned code 1455: The paging file is too small for this operation to complete.
  c1xx: note: please visit https://aka.ms/pch-help for more details
c1xx: Error C1076 : compiler limit: internal heap limit reached

Solution:
Enable virtualization option (Intel VT or AMD SVM) in the BIOS.
e.g. for AMD CPU, Overclocking -> Advanced CPU Configuration -> Enable SVM Mode.

Origin:
https://docs.microsoft.com/en-us/answers/questions/361265/failed-to-create-virtual-memory-for-pch.html

attempting to reference a deleted function

Error:

Reference C2280 : compiler has generated 'RecvBuf::operator =' here

Caused by:

class A
{
public:
   // explicit
   A(){}
   A(A &&){}

   // implicit
   A(const A&) = delete;
   A& operator=(const A&) = delete;
};

Solution:

A(const A&) = default;
A& operator=(const A&) = default;

C++ Compiler Error C2280 “attempting to reference a deleted function”
https://stackoverflow.com/a/31266254/1645289

CMake Issues

No CMAKE_C_COMPILER could be found

Building error:

CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
CMake error at CMakeLists.txt:30 (project): No CMAKE_CXX_COMPILER could be found

Solution:
Mac OS:

sudo xcode-select --reset

Windows:

call "C:\Program_Filesx86\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"

Reference:
https://stackoverflow.com/a/46906065/1645289

Unknown CMake command “FetchContent_Declare”

Building error:

CMake Error at CMakeLists.txt:4 (FetchContent_Declare):
Unknown CMake command “FetchContent_Declare”

Solution:
add include(FetchContent) in CMakeLists.txt

include(FetchContent)

Origin:
https://discourse.cmake.org/t/fetchcontent-declare-not-recognized/443

error C2220: the following warning is treated as an error

Error on building:

error C2220: the following warning is treated as an error

Solution:
set C_FLAGS_WARNINGS in CMakeLists.txt:

set(C_FLAGS_WARNINGS "/W4 /WX-")
fatal error LNK1104: cannot open file ‘kernel32.lib’

Error on building in cmake:

fatal error LNK1104: cannot open file 'kernel32.lib'

Solution:
Execute Cross Tools Command Prompt of Visual Studio firstly:

call "C:\Program_Filesx86\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
error LNK2019: unresolved external symbol _vkCreateInstance@12

Error on building using Ninja

hello_triangle.cpp.obj : error LNK2019: unresolved external symbol _vkCreateInstance@12 referenced in function "private: void __thiscall HelloTriangleApplication::createInstance(void)" (?createInstance@HelloTriangleApplication@@AAEXXZ)
hello_triangle.cpp.obj : error LNK2019: unresolved external symbol _vkDestroyInstance@8 referenced in function "private: void __thiscall HelloTriangleApplication::cleanup(void)" (?cleanup@HelloTriangleApplication@@AAEXXZ)
hello_triangle.cpp.obj : error LNK2019: unresolved external symbol _vkEnumeratePhysicalDevices@12 referenced in function "private: void __thiscall HelloTriangleApplication::pickPhysicalDevice(void)" (?pickPhysicalDevice@HelloTriangleApplication@@AAEXXZ)

Solution:

  1. Visual Studio version isn’t compatible with Ninja, update ninja version.
    https://github.com/ninja-build/ninja/releases

  2. Execute cmake command using x64 Native Tools Command Prompt, not x64_x86 Cross Tools Command Prompt.

mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file

Errors on building:

MT: command "C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe /nologo /manifest main.exe.manifest /outputresource:main.exe;#1" failed (exit code 0x1f) with the following output:

mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file "main.exe". The process cannot access the file because it is being used by another process.
NMAKE : fatal error U1077: 'D:\sdks\cmake-3.23.1-windows-x86_64\bin\cmake.exe' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

Similar errors:

mt.exe : general error c101008d: Failed to write the updated manifest to the resource of file "main.exe". The system cannot open the device or file specified.

Solution:
Restart computer.

warning LNK4098: defaultlib ‘MSVCRT’ conflicts with use of other libs
2>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
2>LINK : warning LNK4217: symbol 'strncmp' defined in 'libucrt.lib(strncmp.obj)' is imported by 'glfw3.lib(init.obj)' in function '_glfwParseUriList'
2>LINK : warning LNK4286: symbol 'strncmp' defined in 'libucrt.lib(strncmp.obj)' is imported by 'glfw3.lib(input.obj)'
2>LINK : warning LNK4286: symbol 'strncmp' defined in 'libucrt.lib(strncmp.obj)' is imported by 'glfw3.lib(context.obj)'
2>LINK : warning LNK4217: symbol 'strncmp' defined in 'libucrt.lib(strncmp.obj)' is imported by 'glfw3.lib(egl_context.obj)' in function 'getEGLErrorString'
2>LINK : warning LNK4217: symbol 'free' defined in 'libucrt.lib(free.obj)' is imported by 'glfw3.lib(init.obj)' in function 'defaultDeallocate'
2>LINK : warning LNK4217: symbol 'free' defined in 'libucrt.lib(free.obj)' is imported by 'glfw3.lib(null_init.obj)' in function '_glfwConnectNull'
2>LINK : warning LNK4217: symbol 'malloc' defined in 'libucrt.lib(malloc.obj)' is imported by 'glfw3.lib(init.obj)' in function 'defaultAllocate'
2>LINK : warning LNK4217: symbol 'realloc' defined in 'libucrt.lib(realloc.obj)' is imported by 'glfw3.lib(init.obj)' in function 'defaultReallocate'
2>LINK : warning LNK4217: symbol 'strtol' defined in 'libucrt.lib(strtox.obj)' is imported by 'glfw3.lib(init.obj)' in function '_glfwParseUriList'
2>LINK : warning LNK4217: symbol '__stdio_common_vsprintf' defined in 'libucrt.lib(output.obj)' is imported by 'glfw3.lib(init.obj)' in function '_glfwInputError'
2>LINK : warning LNK4217: symbol '__stdio_common_vsprintf' defined in 'libucrt.lib(output.obj)' is imported by 'glfw3.lib(win32_joystick.obj)' in function '_glfwDetectJoystickConnectionWin32'
2>LINK : warning LNK4217: symbol 'strcspn' defined in 'libucrt.lib(strcspn.obj)' is imported by 'glfw3.lib(input.obj)' in function 'glfwUpdateGamepadMappings'
2>LINK : warning LNK4217: symbol 'strtoul' defined in 'libucrt.lib(strtox.obj)' is imported by 'glfw3.lib(input.obj)' in function 'parseMapping'
2>LINK : warning LNK4217: symbol 'qsort' defined in 'libucrt.lib(qsort.obj)' is imported by 'glfw3.lib(monitor.obj)' in function 'refreshVideoModes'
2>LINK : warning LNK4217: symbol 'qsort' defined in 'libucrt.lib(qsort.obj)' is imported by 'glfw3.lib(win32_joystick.obj)' in function 'deviceCallback'
2>glfw3.lib(init.obj) : error LNK2019: unresolved external symbol __imp_strtok referenced in function _glfwParseUriList
2>glfw3.lib(window.obj) : error LNK2019: unresolved external symbol __imp_strncpy referenced in function glfwWindowHintString
2>glfw3.lib(input.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>glfw3.lib(monitor.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>glfw3.lib(win32_joystick.obj) : error LNK2001: unresolved external symbol __imp_strncpy
2>glfw3.lib(input.obj) : error LNK2019: unresolved external symbol __imp_strspn referenced in function glfwUpdateGamepadMappings
2>glfw3.lib(context.obj) : error LNK2019: unresolved external symbol __imp___stdio_common_vsscanf referenced in function sscanf

Solution:

if(WIN32)
    # suppress warning - LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
endif()

This method also can fix error:
error LNK2019: unresolved external symbol __imp_strtok referenced in function _glfwParseUriList

Origin:
https://discourse.glfw.org/t/could-not-link-with-glfw-on-windows/1584/5


“Trying to predict the future is like trying to drive down a country road at night with no lights while looking out the back window. ” ― Peter Drucker