Sign in to follow this  
plankton21

Normal C++ on VC 2005 EE

Recommended Posts

Just starting and when reading older tutorials, to make a hello world program all that is needed in the code is: #include <iostream> using namespace std; void main(void) { cout << "Hello World!" <<endl; } ...To make a Hello World program, while in VS C++ 2005EE you have to add more code: #include "stdafx.h" #include <iostream> using namespace std; void main(void) { cout << "Hello World!" <<endl; cin.get(); // so the program doesnt close right away <_< } ... So I was wondering why is it different, why do i need to include stdafx.h now, and i have to add code so the program doesnt close right away? Is it because of .net? Because I want to program some stuff to put on my PSP, as sort of a hobby, and the PSP doesnt have a .net Framework, so... I don't want to have it using .net coding. And I rather like Visual C++ 2005 EE, and don't really want to use dev++. :/ So, as a beginner, how do i know if i'm using .net or not. The new tutorials never tell you what you are doing in terms of requirements on the user side. Going to sleep for now. ;)

Share this post


Link to post
Share on other sites
First off it's int main and not void main... although some compilers might (wrongly)allow void I am almost certain VS2005 does not.

To your question, VS2005 will compile the simpler "Hello World", You just need to create an empty project or turn off pre-compiled headers setting. By default VS will use precompiled headers which then require the extras you speak of.

Share this post


Link to post
Share on other sites
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}

Share this post


Link to post
Share on other sites
Quote:
Original post by DigiDude
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}


(emphasis mine)
It should really be the other way around. The program needs to return something to the OS. I guess its not required, but it is definately good practice.

As for the OP, just make sure to create a blank project (or simply turn off precompiled headers). Then your code will look exactly the same. Also, instead of just hitting F5 to run your code, use ctrl-F5 (Run w/out debugging), that will make sure the console doesn't close as soon as the app finishes.

Share this post


Link to post
Share on other sites
They way I've come to think of stdafx.h (and this could be completely wrong for all I know) is as a container for all of my global headers. All of the #include<> I slap in stdafx.cpp, and then I put my local #include"" in the files I'm using them in. I've also got into the habit of using:

system("PAUSE");

to stop the console from closing, as cin.get() can have unexpected results if you are using a cin call above it, also..

void main()
{
}

The main call just tells the compiler there will be no return from the main function. Apparently using int main(void) is considered bad practice(dont ask me!). While the stdafx thing is local to msvc, other C++ compilers have had the same issue with the console.

If you're concerned about compatibility with your PSP, there is a way to remove the stdafx stuff, If I had the link handy I'd provide it..but it was something I read in passing. It's a flag in the project settings if I remember correctly.

Share this post


Link to post
Share on other sites
Quote:
As far as main goes, void main() ( note the empty () ) is viable in C++ ee.
void main is non-standard. Just because your compiler of choice will let it slide does not mean other compilers or future versions will. It's like the if() scope issue of MSVC6. People would declare variables inside if blocks and use them outside in VC6 because, you know, it like, worked. Consequence: compiling in GCC or a more modern version of VC would give you compilation errors.

Quote:
I've also got into the habit of using:

system("PAUSE")

to stop the console from closing, as cin.get() can have unexpected results if you are using a cin call above it. While the stdafx thing is local to msvc, other C++ compilers have had the same issue with the console.
system("PAUSE") is great and all, but the truth is that it could do anything, even on different computers with the same version of the OS.

If you want to stop the console window from closing, either run from an external console, or "Run without debugging" (for MSVC).


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sr_Guapo
Quote:
Original post by DigiDude
You can get away with this in VC++ EE:

#include <iostream>

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0; // you might have to put this in, but really you shouldn't have to
}


(emphasis mine)
It should really be the other way around. The program needs to return something to the OS. I guess its not required, but it is definately good practice


(second emphasis mine)Actually the first guy is correct. The standard allows an implicit
return 0;
from main if you don't put it at the end. ALL other functions should have one though, and you should probably put one at the end of main for consistancy.

Quote:
from msdn:
The return value of main represents a program's exit status. By convention, an exit status of zero indicates program success. With standard C++, a main function without an explicit return value means the compiler inserts a
return 0;
prior to the end of main.

Share this post


Link to post
Share on other sites
Hi everyone; I'm a beginner just like the OP and having similar problems and confusion with the VC 2005 Express IDE/compiler. I tried 3 approaches to making a console app for "Hello World": selecting a CLR console app, selecting a Win32 console app, and selecting a Win32 console app but removing the pre-prepared MS code and adding my own. Here are the results:


CLR Console Application: This works!

// CLR_Console_App1.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}


_______________________________

Win 32 Console Appication: This doesn't work. I'm using pre-printed code supplied by the compiler and added the "Hello World" line.


// Win32_Console_App1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])

{
std::cout<< "Hello World!" << std::endl;
}

return 0;

________________________________

Win 32 Console App: (Removing the Microsoft code from the source file and adding what's below). This still doesn't work.

#include <iostream>
using namespace std;

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0;
}

______________________

If I've made some obvious errors, please bear with me. Just trying to get the compiler working initially with C++ code seems a monumental task at this point!

Dave FF



Share this post


Link to post
Share on other sites
You do have the PSDK properly installed right? After, create a console win32 C++ application, when the little dialog window comes up where you click submit/apply or whatever, to ocnfim the creation, click on settings on the left, then check the 'empty project' checkbox. After that, the solution and project will be created without any source files. You then add your main source file, say, 'main.cpp', by right clicking the source folder and clicking add new file. Then put in the code you'd like, i.e.:


#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
cout << "Yep, it works." << endl;
return 0;
}




So that it doesn't close fast, compile and then 'Run without Debugging', adding a system call to command 'PAUSE' can create a lot of overhead (i.e. first contact the os, give it executation permission, let it do it's thing, then waiting for it to give it back to you, and then blah)

Hope that helps?

EDIT: I really doubt you have the PSDK installed (which lets you create native C++ applications), check out these instructions: http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/

Share this post


Link to post
Share on other sites
You can do that without the Platform SDK. I *just* installed the PSDK two days ago but I had been building Win32 console test programs like the above prior to that. The key is the "empty project" setting. I believe what the Platform SDK allows you to do is native Win32 GUI apps, otherwise you would be forced to use the CLR. Correct me if I've got that wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hollower
The key is the "empty project" setting.


Not really. The actual key is that the precompiled header option is disabled in the project settings. Which just so happens to be the case when you create an empty project.

Share this post


Link to post
Share on other sites
Accually I already have the Microsoft SDK installed. :) Thanks for the help, the Hello World runs without the extra stuff in a empty project and press ctrl+F5 to run instead. Still concerned about my other question, how do I know if i'm using .net features in my coding? Is there certain headers I have to avoid?

Share this post


Link to post
Share on other sites
Quote:

CLR Console Application: This works!

But this is a C++/CLI program, not a C++ one.

Quote:

Win 32 Console Appication: This doesn't work.


Shouldn't compile; you have a return statement outside of the body of main. It's also possible that stdafx.h is not including iostream.

Quote:

Win 32 Console App: This still doesn't work.


This should work. You probably still have precompiled headers enabled. Turn them off (Project options -> C++ -> Precompiled Headers)

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
Quote:

CLR Console Application: This works!

But this is a C++/CLI program, not a C++ one.

Quote:

Win 32 Console Appication: This doesn't work.


Shouldn't compile; you have a return statement outside of the body of main. It's also possible that stdafx.h is not including iostream.

Quote:

Win 32 Console App: This still doesn't work.


This should work. You probably still have precompiled headers enabled. Turn them off (Project options -> C++ -> Precompiled Headers)

______________________

Thanks for catching my mistake of putting "return0;" outside the last curly brace.

I found this advice to C++ beginners about using MS Visual Studio to write and compile simple C++ programs in Brian Overland's book, "C++ Without Fear." (published in 2005). Overland suggests that you first click on "console application" as the project type. Look for the resource_file.cpp (name may be different) on the left side if it is not immediately available on the screen. Double click the resource_file.cpp to bring it into the viewer if it is not already in view. Next, delete all the Microsoft generated code on the view screen except for #include <stdafx.h> Overland writes that this statement is always needed in console applications created with Visual Studio. Otherwise, all other standard C++ code that would be processed without error for the GNU C++ compiler should compile with the Visual Studio console application also. Again, the only difference required according to Overland is the addition of the #include <stdafx.h> line.

Overland offers this rendition of the "Hello World" as a console app for the MS Visual Studio C++ IDE/compiler:


#include <stdafx.h>
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}



So my understanding of Overland's advice is that you can simply delete the Microsoft prepared code on a console application, add back in the #include <stdafx.h> line, and follow any standard C++ code for a console application that would compile for the GNU C++ compiler, and you should have no problem with the MS Visual C++ compiler. I tried this with the VC++ 2005 Express edition, and the above program works. Am I missing anything important here?


Dave FF


P.S.: Brian Overland was a former Microsoft employee, who worked as a C++ programmer and project director for 10 years.


Share this post


Link to post
Share on other sites
Overland's work record proves exactly nothing, and in fact either the statement in his book is incorrect, or your interpretation of it is. In any case the steps you described are going to be specific to a certain version of Visual Studio.

Quote:

Overland writes that this statement is always needed in console applications created with Visual Studio.


If by "this statement" you mean the #include "stdafx.h" then, unfortunately, Overland is 100% dead wrong. It is only required when using precompiled headers, and the name is not required to be stdafx.h. You can disable precompiled headers, and you should until you know how they work.

"Precompiled headers" are a technique that can reduce compile times if correctly used. Perhaps in an effort to promote the use of precompiled headers, most of the templates in most versions of Visual Studio ship with them enabled and with a set of skeleton files for using them. These skeleton files are stdafx.h and stdafx.cpp. The things you need to know, however, is that a) precompiled headers are not required to write and compile code, and b) precompiled headers are nonstandard and their implementation varies between compilers.

Incidentally, stdafx.h is not usually in the system include path so you generally want to include it with quotes, not angle brackets.

Visual Studio (all versions worth using) can compile the following program just fine, assuming precompiled headers are disabled:

int main(void)
{
return (0);
}


No #include directives at all are neccessary.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by plankton21
Accually I already have the Microsoft SDK installed. :) Thanks for the help, the Hello World runs without the extra stuff in a empty project and press ctrl+F5 to run instead. Still concerned about my other question, how do I know if i'm using .net features in my coding? Is there certain headers I have to avoid?


Quoted for answer. :/

Share this post


Link to post
Share on other sites
By .NET, do you mean the .NET Framework? Or do you mean you want to avoid compiler-specific features or extensions to the language that are available to you in Visual Studio 7+ (marketed as Visual Studio .Net?)

For the former, choose a regular C++ project when creating a new one (choose templates from the General, not CLR, submenu in VS2005 EE). You won't be able to call into the .Net framework accidentally this way.

For the latter, you can go to project options and set "Disable Language Extensions" to Yes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ummm, i'm not sure, the one that makes it so I can program for things without a .net framework.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, well the empty project in General has no debug information... I'm not sure what that means. In Win32 a empty project has debug information.

"Debug information for 'Empty.exe' cannot be found or does not match. Binary was not built with debug information. Do you want to continue debugging? (Yes or No)

is it working as intended, and I jsut ignore this error?

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave FF
Hi everyone; I'm a beginner just like the OP and having similar problems and confusion with the VC 2005 Express IDE/compiler. I tried 3 approaches to making a console app for "Hello World": selecting a CLR console app, selecting a Win32 console app, and selecting a Win32 console app but removing the pre-prepared MS code and adding my own. Here are the results:


CLR Console Application: This works!

// CLR_Console_App1.cpp : main project file.

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}


_______________________________

Win 32 Console Appication: This doesn't work. I'm using pre-printed code supplied by the compiler and added the "Hello World" line.


// Win32_Console_App1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])

{
std::cout<< "Hello World!" << std::endl;
}

return 0;

________________________________

Win 32 Console App: (Removing the Microsoft code from the source file and adding what's below). This still doesn't work.

#include <iostream>
using namespace std;

int main()
{
std::cout<< "Hello World!" << std::endl;
return 0;
}

______________________

If I've made some obvious errors, please bear with me. Just trying to get the compiler working initially with C++ code seems a monumental task at this point!

Dave FF



The problem with the last two I can gather is that the default setting for VS2005EE is to compile for unicode and what you are trying to print is not unicode either Disable unicode or change the "Hello World" to L"Hello World" so the compiler knows that its a unicode string.

It should work fine with pre-compiled headers.

Share this post


Link to post
Share on other sites
[quote]Original post by Sr_Guapo
Quote:
Original post by DigiDude
[code removed]
(emphasis mine)
It should really be the other way around. The program needs to return something to the OS. I guess its not required, but it is definately good practice.
I'm pretty sure it says that you don't need it in The C++ Programming Language. I'll see if I can find it later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this