Linking to CRT (unresolved external symbol WinMainCRTStartup)
Asked Answered
A

1

1

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?

Albatross answered 17/1, 2015 at 23:7 Comment(3)
What the signature of you main function?Solifidian
Try to use VC\Lib\amd64 in library path for x64 buildPless
frymode: That was it! Thank you so much. If you want to post it as an answer I will accept it as such.Albatross
H
2

Necroposting.

Here's a bunch of useful stuff (that I encountered in the past when dealing with various challenges) about VStudio (C / C++) builds:

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 (might want to also check [MS.MSDN.Social]: Where is vcvarsall.bat file?) is located in:

  • VStudio 2005 (8) and above:

    • ${VSTUDIO_INSTALL_DIR}\VC
  • VStudio 2017 (15) 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

Of course, you could create a Makefile.mak for the build: [MS.Docs]: Running NMAKE.

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).

Horace answered 14/4, 2022 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.