Jump to content

Recommended Posts

when i try to compile this simple c++ source

#include<iostream.h>
void main()
{
cout<<"HELLO WORLD";
}

with

[seeno@localhost seeno]$ gcc helloworld.cpp

i get this

In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
                from helloworld.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This file includes at
least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
helloworld.cpp:3: `main' must return `int'

and when i compile with the -Wno-deprecated option, i just get this

[seeno@localhost seeno]$ gcc -Wno-deprecated helloworld.cpp
helloworld.cpp:3: `main' must return `int'

Link to post
Share on other sites

you are almost there.. the main function must always return a int value. Now normal functions in C++ are called with void. That means they return no value. If you want them to return a value you call it with int main() {}

 

so try this

 

#include <iostream.h>

int main(int argc, char *argv[])
{
 cout << "Hello, world!" << endl;

}

 

now don't use gcc as your compiler. That is the C compiler and iostream is a C++ header. You need to use g++ as your compiler

 

g++ -o helloworld helloworld.cpp

 

Also you'll notice my main() includes some arguements. Those aren't needed for a basic hellow world app so you can just use int main() but if you want to make like a console based app where you are passing in flags then thats what those argc and argv values are for.

 

hope it helps.. if you have anymore C++ questions you can reach me on efnet.. under the nick Jy i'm always in fedora and #redhat..

Link to post
Share on other sites

A few things:

 

if you're using 'int main()' you should return an int!

so add return 0; to the program to tell linux your program exited with no error.

 

including with .h is deprecated... see the compiler error

 

cout is located in std namespace, so use that namespace or call cout with std::cout

 

#include <iostream>

using namespace std;

int main() {
       cout << "Hello, world!" << endl;
       return 0; // No error
}

 

save that file as 'helloworld.cpp' and type 'make helloworld' to compile it.

 

my 2 cents

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...