Find the answer to your Linux question:
Results 1 to 4 of 4
Well what I am trying to do is just learn the basics of C++ and all I want to do for now is just look at basic programms and see ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Nov 2003
    Location
    Brooklyn, NY
    Posts
    347

    GCC and C++ Newbie


    Well what I am trying to do is just learn the basics of C++ and all I want to do for now is just look at basic programms and see how they work and get the baiscs. Anyway what I am trying to do is simply compile a baisc 'Hello World' program:

    Code:
     #include <iostream.h>
    
    int main&#40;&#41;
    &#123;
        cout<<"Hello World";
        return&#40;0&#41;;
    &#125;
    but when I try to compile I am getting this:
    slip@localhost C++ $ gcc world.c
    world.c:1:21: iostream: No such file or directory
    world.c: In function `main':
    world.c:5: `cout' undeclared (first use in this function)
    world.c:5: (Each undeclared identifier is reported only once
    world.c:5: for each function it appears in.)

    I really have no clue what I am doing so can please point it out?

  2. #2
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    try this:
    Code:
    #include <stdio.h>
    that should get it..
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  3. #3
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    I may be wrong, but AFAIK c++ code needs to be compiled with g++? and have a .cpp file extension? for c++, iostream.h is the c equivilent of stdio.h, afaik stdio.h does not provide a cout function, which is c++ specific.

    So, the code should be compiled with:
    Code:
    g++ helloworld.cpp -Wno-deprecated -Wall -o programname
    where helloworld.cpp is the name of your source code, and programname is the name of the binary you want it to create.

    Jason

  4. #4
    Just Joined!
    Join Date
    Sep 2003
    Posts
    1
    Code:
     
    #include <iostream>
    
    using namespace std;
    
    int main&#40;&#41;
    &#123;
        cout << "Hello World";
        return&#40; 0 &#41;;
    &#125;
    The correct C++ code is as printed above. Using iostream.h has been deprecated in standard C++ and is now just iostream. You also have to include the 'using namespace std;' line or you have to put std:: in front of all of your calls using cout. Declaring the namespace is easiest though.

    You should also use g++ when compiling c++ programs. Somebody else already listed the command.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •