Thread: newbie : compiling a C++ program in linux

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    newbie : compiling a C++ program in linux

    Hi !

    I recently shifted to linux OS and compilation of all my C programs on it has worked fine with the gcc compiler. I tried to compile my c++ programs on linux but it somehow gave me some errors which I cannot understand. I used the command
    "g++ filename.cpp"

    This is a simple program I tried to compile

    Code:
    #include<iostream>
    int main()
    {
            cout << " hello";
            return 0;
    }
    The error I get is :

    eg325.cpp: In function `int main()':
    eg325.cpp:4: `cout' undeclared (first use this function)
    eg325.cpp:4: (Each undeclared identifier is reported only once for each
    function it appears in.)
    what am I doing worng,

    also when i replace the iostream with iostream.h why do I get a huge warning ?

    /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.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    add "using namespace std;" just under your last include or swap 'std::cout' for 'cout'.

    The .h versions of the C++ headers are, as the warning tells you, for backward compatibilty only and should never be used for new development.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You get an error because you havent told the compiler that cout is located in the standard namespace. There are 3 ways to fix this:
    Code:
    // For the lazy person but also least prefered method
    #include <iostream>
    using namespace std;
    ...
    
    // A better method
    #include <iostream>
    using std::cout;
    
    // The method that is considered best:
    #include <iostream>
    ...
    std::cout << "bla bla bla";

    You get the warning because iostream.h is not a standard header and should not be used.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  4. #4
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    If you put
    using std::cout;
    You can only use the cout function of namespace std, correct?
    So then you would need to put
    using std::cin; also if you wanted to use cin.

  5. #5
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I understand why you may not want to put using namespace std but why would this
    Quote Originally Posted by Shakti
    // The method that is considered best:
    #include <iostream>
    ...
    std::cout << "bla bla bla";
    be better than this
    Quote Originally Posted by Shakti
    // A better method
    #include <iostream>
    using std::cout;

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Ganoosh, yes, that's correct.

    MadCow257, later, when you try to use the same technique when using sort() or max() or some other commonly named function, and it conflicts with one of your functions or another function in a library you are using, you will need to explicitly state the namespace that you are using. For cout, it is not a big deal, because few people use the "cout" name in their program.

    BTW, another common technique is to put the using declaration or using directive inside the scope that you need it. So instead of making it global for the whole file, you just use it for the function or code block you need it for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about linux compiling with GCC
    By elsheepo in forum C++ Programming
    Replies: 23
    Last Post: 03-07-2008, 11:36 PM
  2. Compiling Linux kernel on Windows
    By jmd15 in forum Linux Programming
    Replies: 9
    Last Post: 04-10-2006, 07:28 AM
  3. Inheritance and program structure planning please help a newbie
    By ninjacookies in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 12:18 PM
  4. compiling qt program
    By rip1968 in forum Linux Programming
    Replies: 3
    Last Post: 04-07-2003, 07:06 PM