162

While I am running the simple code as below I have two errors as following:

#include <iostream>
#include <string>
using namespace::std;

template <class Type>
class Stack
{
public:
    Stack (int max):stack(new Type[max]), top(-1), maxsize(max){}
    ~Stack (void) {delete []stack;}
    void Push (Type &val);
    void Pop (void) {if (top>=0) --top;}
    Type& Top (void) {return stack[top];}
    //friend ostream& operator<< (ostream&, Stack&);
private:
    Type *stack;
    int top;
    const int maxSize;
};

template <class Type>
void Stack <Type>:: Push (Type &val)
{
    if (top+1<maxsize)
        stack [++top]=val;
}

Errors:

MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

What Should I do?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
NAIEM
  • 1,649
  • 3
  • 12
  • 6
  • 1
    Is this all the code? Where is your main function? – Connman Jul 08 '11 at 15:09
  • 4
    Also looks like the type of project is set wrong. The linker error regarding WinMain implies you tried to create a Win32 project. If you just want something to output text to the command prompt, try changing the project type to Console. – Kilanash Jul 08 '11 at 17:52
  • 4
    By the way, if you are getting compiler/linker errors, you are not "running" the code. – André Caron Feb 09 '12 at 08:55
  • 1
    I tried changing the project type, but project=exe worked instead of console. – Heather Oct 08 '14 at 12:53

13 Answers13

397

Thats a linker problem.

Try to change Properties -> Linker -> System -> SubSystem (in Visual Studio).

from Windows (/SUBSYSTEM:WINDOWS) to Console (/SUBSYSTEM:CONSOLE)

This one helped me

Bohdan
  • 16,531
  • 16
  • 74
  • 68
  • 4
    I've got the same issue. Your answer isn't helping. Any Other suggestion? – Parth Sane Jun 26 '14 at 09:30
  • 1
    I had that problem when using MS Visual Studio. If your environment is different that you might have to fix it differently. But it should still be a linker problem. – Bohdan Oct 02 '14 at 22:13
  • 3
    If you are using `tWinMain` as your main function, you must include tchar.h _or_ change it to either `WinMain` or `wWinMain` depending on whether or not your app is Unicode. Failure to do so also yields this linker error even with the correct subsystem. (/SUBSYSTEM:WINDOWS) – lisa Mar 29 '15 at 04:45
  • This helped me, apart from that I also had to disable **Avast** anti-virus. – XCS Dec 01 '15 at 20:28
  • Remember to not just change SUBSYSTEM:WINDOWS to SUBSYSTEM:CONSOLE. But the entire thing, I forgot to change Windows to Console before the parenthesis. – miniwolf Feb 14 '17 at 10:46
  • For anyone who cares why, see: http://stackoverflow.com/questions/7316433/difference-between-console-subsystemconsole-and-windows-subsystemwindows – kmiklas Mar 28 '17 at 00:09
  • You really saved my life, i was stuck during an hour on that problem !! Thank you !!! – Myriam Sarah Dec 07 '17 at 13:10
  • 1
    This worked only after I selected "all configurations" for both platform and type. Selecting "build" on the "Solution" attempted to build all and the first one tried was NOT the one had specified for console subsystem. – Joseph Stateson Nov 29 '19 at 18:37
  • To anyone using VS for the first time: you need to right-click on the project/solution to get the Properties option, it's not on the Menu bar – Pani Apr 06 '20 at 10:19
  • That helped my program compile. Why is windows so finicky? I usually compile with GCC on VSL but I'm doing a famous tutorial. I tried compiling with native tools but I got a logical error. I wanted to see if that error would come up in VS. Yes, but to get to that point I had to create solution and change GUI settings. Does windows really have to be this complicated? – gcr Feb 01 '21 at 04:41
91

As the others mentioned you can change the SubSystem to Console and the error will go away.

Or if you want to keep the Windows subsystem you can just hint at what your entry point is, because you haven't defined ___tmainCRTStartup. You can do this by adding the following to Properties -> Linker -> Command line:

/ENTRY:"mainCRTStartup"

This way you get rid of the console window.

Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
  • 3
    +1: "This way you get rid of the console window." - Cool! Learned sth. new today! – Valentin H Mar 13 '14 at 16:48
  • 1
    +1 for the advice, i was trying to figure this out for like 20 minutes, since with SFML i can just specify the sub system Windows, With GLFW that obviously differen't so thanks (= – daniel Jul 16 '16 at 10:54
15

If you are having this problem and are using Qt - you need to link qtmain.lib or qtmaind.lib

David Casper
  • 394
  • 2
  • 9
  • That's on Project -> Properties -> Linker -> Input. Add `$(QTDIR)\lib\qtmaind.lib` to Additional Dependencies. – mathiasfk Feb 09 '18 at 11:34
  • 1
    Adding `CONFIG += console` to the `.pro` file fixed the issue for my Qt project – Synck Feb 24 '20 at 11:57
  • Note that in QT6 qtmain.lib is gone. So you msut use "Morten Kristensen" answers to fix this. – jpo38 Jan 28 '22 at 08:32
15

Besides changing it to Console (/SUBSYSTEM:CONSOLE) as others have said, you may need to change the entry point in Properties -> Linker -> Advanced -> Entry Point. Set it to mainCRTStartup.

It seems that Visual Studio might be searching for the WinMain function instead of main, if you don't specify otherwise.

mathiasfk
  • 1,278
  • 1
  • 19
  • 38
10

Include <tchar.h> which has the line:

#define _tWinMain wWinMain
Tyler
  • 28,498
  • 11
  • 90
  • 106
Zaki
  • 158
  • 1
  • 7
8

If you use Unicode Character Set, but the entry wasn't set, you can specify /ENTRY:"wWinMainCRTStartup"

Petronius
  • 428
  • 4
  • 12
5

If you actually want to use _tWinMain() instead of main() make sure your project relevant configuration have

  1. Linker-> System -> SubSystem => Windows(/SUBSYSTEM:WINDOWS)
  2. C/C++ -> Preprocessor -> Preprocessor Definitions => Replace _CONSOLE with _WINDOWS
  3. In the c/cpp file where _tWinMain() is defined, add:

    #include <Windows.h> #include <tchar.h>

Alex
  • 583
  • 5
  • 18
4

If your project is Dll, then the case might be that linker wants to build a console program. Open the project properties. Select the General settings. Select configuration type Dynamic Library there(.dll).

4

i don't see the main function.

please make sure that it has main function.

example :

int main(int argc, TCHAR *argv[]){

}

hope that it works well. :)

musefan
  • 47,875
  • 21
  • 135
  • 185
  • 1
    This doesn't help. The linker is complaining about an undefined [WinMain](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633559.aspx) entry point. Defining an entry point called `main` does nothing to solve that. – IInspectable Jan 16 '18 at 11:10
  • The linker tries to resolve different `main`/`WinMain` versions, and if none of them is found it says _WinMain@16 not found_, but this message isn't exactly correct. – Lorinczy Zsigmond Jun 01 '18 at 19:29
3

I'm not sure where to post this answer of mine but I think it's the right place. I came across this very error today and switching the subsystems didn't change a thing.

Changing the 64bit lib files to 32bit (x86) did the trick for me, I hope it will help someone out there !

2

Your tried to turn that source file into an executable, which obviously isn't possible, because the mandatory entry point, the main function, isn't defined. Add a file main.cpp and define a main function. If you're working on the commandline (which I doubt), you can add /c to only compile and not link. This will produce an object file only, which needs to be linked into either a static or shared lib or an application (in which case you'll need an oject file with main defined).

_WinMain is Microsoft's name for main when linking.

Also: you're not running the code yet, you are compiling (and linking) it. C++ is not an interpreted language.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • 7
    Actually, he needs a [`WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)`](http://msdn.microsoft.com/en-us/library/ms633559%28v=vs.85%29.aspx) entry point. This linker error indicates that the project is being built for the Windows subsystem, not the console subsystem. – Adam Maras Jul 08 '11 at 18:11
  • @Adam: ah yes, I've been spoiled by Qt :) (which effectively hides WinMain from you). – rubenvb Jul 08 '11 at 19:12
  • WinMain is only for Windows applications. Console apps use a different name _tmain, which resolves to main or wmain depending on Unicode/MBCS setting. – Steve Townsend Jul 08 '11 at 19:29
  • @AdamMaras, perfect. I was looking for the requisite capitalization and prototype. It works perfectly now. Thanks! – Synetech Jul 14 '15 at 19:56
  • `_WinMain@16` is the decorated symbol of the user-provided entry point called by the startup code in the CRT, when targeting the Windows subsystem. It's not *"Microsoft's name for `main` when linking"*. If you target the console subsystem, the CRT that ships with Visual Studio will call an entry point named `main`. If undefined, the linker will complain about a missing symbol called `_main`. – IInspectable Jan 16 '18 at 11:23
1

If you are using CMake, you can also get this error when you set SET(GUI_TYPE WIN32) on a console application.

Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
0

The erudite suggestions mentioned above will solve the problem in 99.99% of the cases. It was my luck that they did not. In my case it turned out I was including a header file from a different Windows project. Sure enough, at the very bottom of that file I found the directive:

#pragma comment(linker, "/subsystem:Windows")

Needless to say, removing this line solved my problem.

Moshe Rubin
  • 1,944
  • 1
  • 17
  • 37