You're using a Win32 application project when you want a console application project. Win32 applications expect WinMain instead of main.

But it still doesn't work for me.
I'm just a freshmen. I set up a blank workplace, then win32 application project, then a c++ cource file.
Then type as below:

#include <iostream>

void main()
{
std::cout <<"welcome" <<std::endl;
}

No matter I use "main" or "WinMain" in the second line, it just doesn't work.
Also the linking problem.

Recommended Answers

All 6 Replies

But it still doesn't work for me.
I'm just a freshmen. I set up a blank workplace, then win32 application project, then a c++ cource file.
Then type as below:

#include <iostream>

void main()
{
std::cout <<"welcome" <<std::endl;
}

No matter I use "main" or "WinMain" in the second line, it just doesn't work.
Also the linking problem.

This is not a "win32 application", it's a "console application". They are two completely different things.

And "it just doesn't work" tells us nothing. It outputs the Gettysburg Address instead of "welcome"? It turns your computer off? It has compiler errors? There are hundreds of ways it might not work. Be specific. Like void main() is wrong. It is supposed to be int main() with a return 0 at the end of the program, as defined by the C++ Standard.

And code tags are to surround all code postings. Begin code with [[I]code[/I]] and end with [[I]/code[/I]]

commented: Agrees - Salem +2

This is not a "win32 application", it's a "console application". They are two completely different things.

And "it just doesn't work" tells us nothing. It outputs the Gettysburg Address instead of "welcome"? It turns your computer off? It has compiler errors? There are hundreds of ways it might not work. Be specific. Like void main() is wrong. It is supposed to be int main() with a return 0 at the end of the program, as defined by the C++ Standard.

Oh, sorry. When I say "it doesn't work" I mean it has the same problem above. VC++ says:
--------------------Configuration: task1_4 - Win32 Debug--------------------
Linking...
LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol [EMAIL="_WinMain@16"]_WinMain@16[/EMAIL]
Debug/task1_4.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
task1_4.exe - 2 error(s), 0 warning(s)

Thanks

When you created the project, you didn't tell it the correct type of application to make. If you are making a console application, use main ; if you are making a GUI application, use WinMain .

commented: Agrees - Salem ( the new mod :) ) +2

> with a return 0 at the end of the program, as defined by the C++ Standard.

Actually, return 0 is implicit for main() if a return statement is omitted. It's probably still good practice to explicitly return something, however.

commented: Agrees - Salem +2

> with a return 0 at the end of the program, as defined by the C++ Standard.

Actually, return 0 is implicit for main() if a return statement is omitted. It's probably still good practice to explicitly return something, however.

Then why bother with the comment, other than to just confuse the newbie who's already confused? ;)

Well, just because I think it's good practice doesn't make it so. Sometimes people say that not specifying a return value for main is nonstandard, which it isn't.

Just nitpicking, really.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.