hi there, currently i starting to learn C++.i using VC++ 2010.hmm,i want to ask why this code must be compile under console application

#include <iostream>

int main()
{
     std::cout << "123";
     return 0;
}

Edited 6 Years Ago by wwefriend: n/a

Are you asking why you can't compile it as a Windows program or am I misunderstanding you?

yup why its cant compile it as a windows program

Windows programs are different from console programs in three important ways.
1. the 'main' function is hidden from you. Windows programs start with a function called "WinMain". Instead of you calling functions to use, a Windows program calls you when something happens.
2. Windows programs have no console display. The console display is that familiar old black DOS/Unix screen, which is actually a pretty complicated and useful tool. When your console program says cout<< "hello";, that means 'ConsoleOUT', or, send this string to the console display. A window is much simpler than a console. It is just a frame with no picture inside. To see your text, the window must have a textbox or something else to hold text.
3. A Windows program operates through messaging, and must have a "message loop" in the WinMain. Newer versions of Windows, like Forms and WPF, hide that loop from you, but it is still there. So, the idea when you write a Windows program is, handle all the messages. All buttons, menu items, tab controls, etc. become messages, and you have to write code to deal with each one of them.

Windows programming, and to some extent all UI programming, is about handling messages.

Windows programs are different from console programs in three important ways.
1. the 'main' function is hidden from you. Windows programs start with a function called "WinMain". Instead of you calling functions to use, a Windows program calls you when something happens.
2. Windows programs have no console display. The console display is that familiar old black DOS/Unix screen, which is actually a pretty complicated and useful tool. When your console program says cout<< "hello";, that means 'ConsoleOUT', or, send this string to the console display. A window is much simpler than a console. It is just a frame with no picture inside. To see your text, the window must have a textbox or something else to hold text.
3. A Windows program operates through messaging, and must have a "message loop" in the WinMain. Newer versions of Windows, like Forms and WPF, hide that loop from you, but it is still there. So, the idea when you write a Windows program is, handle all the messages. All buttons, menu items, tab controls, etc. become messages, and you have to write code to deal with each one of them.

Windows programming, and to some extent all UI programming, is about handling messages.

o.o,nice .......i get it... thanks so much...

There are actually several ways for your code to work in a Windows program. You can cause a windows program to also have a console display by changing the subsystem type in Visual Studio. Then std::cout will work, but you need a separate thread, independent from WinMain to get input. Another option is to write a stream class derived from ostream which sends your text to a Windows TextBox. Both of these tricks require advanced skills.

It depends on what libraries you use with your code. If you use all functions from the standard libraries your code should be highly portable. See: http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library. Beyond that, you'd have to see if the library was identical for both platforms (some of them are, with the differences between the two are bridged "underneath the hood.")

If you use something written specifically for windows (e.g., Win32 API) it's almost guaranteed not to be portable. However, if you use a library like Qt for your GUI you should be able to compile it on Windows, Mac, and Linux (wxWidgets is also compatible across Windows and Linux I believe).

That probably gives you more questions but hopefully clears some of it up for you.

Edited 6 Years Ago by jonsca: n/a

Yeah wxWidgets is a great library to learn if im not mistaken i think there is a windows version of gtk as well.

o.o......now i learning from C++ how to program 6th edtion by deitel and from MSDN C++ beginner guide

MSDN will always give you Microsoft-specific things to use. It's part of the Microsoft business model. MSDN code will only work on MS systems.
"Standard C Library" functions that are ancient and portable have names like
printf, scanf, fopen, strcmp, puts, getchar, fwrite, strcspn.
Stick to these, and you will be able to write on any system in existence.

They are not graphical though. No mouse, no buttons. You will need special libraries for those no matter what system you use. Java has graphics built in, and it runs everywhere, but it's not C++. It is based on C++.

This article has been dead for over six months. Start a new discussion instead.