'Linking to CRT (unresolved external symbol WinMainCRTStartup)
I'm trying to create a windows application and statically link to the CRT. I'm getting this error,
LINK : error LNK2001: unresolved external symbol WinMainCRTStartup
I'm compiling with this command line
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\cl.exe" /c main.cpp /O2 /I"C:\Program Files (x86)\Windows Kits\8.1\include\shared" /I"C:\Program Files (x86)\Windows Kits\8.1\include\um" /I"C:\Program Files (x86)\Windows Kits\8.1\include\winrt" /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\Include" /DWIN32 /D_WINDOWS /Zi /MT /nologo
and linking with this command line
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\link.exe" user32.lib libcmt.lib kernel32.lib main.obj crt_win64.obj /SUBSYSTEM:WINDOWS /DEBUG /nologo /MACHINE:x64 /LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\x64" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\Lib"
As you can see I'm using the /MT switch on the compiler and using the /SUBSYSTEM:WINDOWS switch on the linker.
I'm also linking to libcmt.lib.
The signature of my main function is
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
What am I missing?
Solution 1:[1]
Necroposting.
Here's a bunch of useful stuff (that I encountered in the past when dealing with various challenges) about VStudio (C / C++) builds:
[SO]: How to build a DLL version of libjpeg 9b? (@CristiFati's answer)
[SO]: Simstring (python) installation in windows (@CristiFati's answer)
[SO]: How to include OpenSSL in Visual Studio (@CristiFati's answer)
[SO]: LNK2005 Error in CLR Windows Form (@CristiFati's answer)
Of course, one way is to manually specify every path, but that can be very painful (especially when cross-building).
That's where VCVarsAll (also VSDevCmd, but I prefer the former) comes into play. It sets all the VC related paths (.lib files path and even build tools') for the current build configuration, so you don't have to worry about them. Check [MS.Docs]: Use the Microsoft C++ toolset from the command line for more details.
Generally, vcvarsall.bat is located in:
VStudio 8.0 (2005) and above:
- ${VSTUDIO_INSTALL_DIR}\VC
VStudio 16.0 (2017) and above:
- ${VSTUDIO_INSTALL_DIR}\VC\Auxiliary\Build
What your build commands could look like:
:: Setup build for pc064
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" amd64
:: Compile
cl /nologo /O2 /DWIN32 /D_WINDOWS /Zi /MD /Fomain.obj /c main.cpp
:: Link
link /NOLOGO /DEBUG /MACHINE:x64 /SUBSYSTEM:WINDOWS /OUT:main.exe main.obj kernel32.lib user32.lib
Note: Since vcvarsall.bat adds (lots of) directories to the existing environment variables (PATH, INCLUDE, LIBPATH, ...), launching it multiple times (switching between pc032 and pc064 builds) might result in the variable char limit (32760) to be reached (which will trigger problems). Therefore, it's best to keep one Cmd window open for each target CPU architecture (and don't launch VCVarsAll multiple times).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CristiFati |
