Linux-Noob Forums

Full Version: gcc
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

when i try to compile this simple c++ source

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




with

Code:
[seeno@localhost seeno]$ gcc helloworld.cpp




i get this

Code:
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



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





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

 



Code:
#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

 



Code:
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..


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

 



Code:
#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


Thanks alot :)

Now i can go ahead and read the tuts without banging my head :P