Thread: Linux and C++

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12

    Linux and C++


    I have a file test.cpp that contains:

    Code:
    #include <iostream.h>
    
    int main()
    {
            cout<<"Hello World!";
            return 0;
    }
    I then type:

    Code:
    [phantom_@phantombox code]$ gcc test.cpp
    and it tells me this.

    Code:
    In file included from /usr/include/c++/3.2/backward/iostream.h:31,
                     from test.cpp:1:
    /usr/include/c++/3.2/backward/backward_warning.h:32:2: warning: #warning This fi
    le 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 in
    clude substituting the <X> header for the <X.h> header for C++ includes, or <sst
    ream> instead of the deprecated header <strstream.h>. To disable this warning us
    e -Wno-deprecated.
    /tmp/cchnyEK6.o: In function `main':
    /tmp/cchnyEK6.o(.text+0x19): undefined reference to `std::cout'
    /tmp/cchnyEK6.o(.text+0x1e): undefined reference to `std::basic_ostream<char, st
    d::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ost
    ream<char, std::char_traits<char> >&, char const*)'
    /tmp/cchnyEK6.o: In function `__static_initialization_and_destruction_0(int, int
    )':
    /tmp/cchnyEK6.o(.text+0x4a): undefined reference to `std::ios_base::Init::Init[i
    n-charge]()'
    /tmp/cchnyEK6.o: In function `__tcf_0':
    /tmp/cchnyEK6.o(.text+0x79): undefined reference to `std::ios_base::Init::~Init
    [in-charge]()'
    /tmp/cchnyEK6.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Now if I write this same program using strait C it works fine but I cannot get C++ to compile on my linuxbox?

    Can anyone help?

    ohh my gcc version if that helps

    Code:
    Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
    Thread model: posix
    gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    The old C headers end in ".h", try "#include <iostream>".

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    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
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12
    I did as you suggested and changed "#include <iostream.h>" to ""#include <iostream>".

    When I try to compile it I get this

    Code:
    [phantom_@phantombox code]$ gcc test.cpp
    test.cpp: In function `int main()':
    test.cpp:5: `cout' undeclared (first use this function)
    test.cpp:5: (Each undeclared identifier is reported only once for each function
       it appears in.)
    Any Ideas?
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Read up on namespaces


    You can do this:

    Code:
    #include <iostream>
    
    using namespace std; //<--- set namespace for entire file
    
    int main(){
    
        cout << "Hello World\n";
    
        return 0;
    }
    or this:

    Code:
    #include <iostream>
    
    int main(){
    
        std::cout << "Hello World\n"; //<--- fully qualify object
    
        return 0;
    }
    They are equivelent.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    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
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    New Zealand
    Posts
    28
    Rep Power
    0
    g++ test.cpp -o test ?
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    gcc ought to work just the same. I believe it will auto-detect the type of code it sees and compile with the appropriate flags.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    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
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12
    I tried G++ using the same code and I got this

    Code:
    [phantom_@phantombox code]$ g++ test.cpp
    test.cpp: In function `int main()':
    test.cpp:5: `cout' undeclared (first use this function)
    test.cpp:5: (Each undeclared identifier is reported only once for each function
       it appears in.)

    And if I try and use the "using namespace std;"

    I get:

    Code:
    [phantom_@phantombox code]$ gcc test.cpp
    /tmp/ccLtarLq.o: In function `main':
    /tmp/ccLtarLq.o(.text+0x19): undefined reference to `std::cout'
    /tmp/ccLtarLq.o(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
    /tmp/ccLtarLq.o: In function `__static_initialization_and_destruction_0(int, int)':
    /tmp/ccLtarLq.o(.text+0x4a): undefined reference to `std::ios_base::Init::Init[in-charge]()'
    /tmp/ccLtarLq.o: In function `__tcf_0':
    /tmp/ccLtarLq.o(.text+0x79): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
    /tmp/ccLtarLq.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    I can complie C fine on my system but I would really like to use C++.
  14. #8
  15. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    I got the same sort of errors on a BSD varient using gcc, but not when I used CC or g++. Try a different compiler and see what happens. The actual problem in the second example is during the linking process (as opposed to the compiling process) and I am not familiar enough to know what libs need to be included (it would be something like this, though (added to your compile line): -L<libname.so>). Different compilers are smarter/dumber about what default libs to link, so that is why it may work with a different one.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    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
  16. #9
  17. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    gcc isn't using the correct compiler. As stated above, use g++ and it will compile correctly. I tested this on rh 7.2 and it functioned correctly.

    g++ -o test test.cpp
  18. #10
  19. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    gcc does invoke the C++ compiler if the source file extension is .cpp. However it does not use the C++ libraries by default. gcc and g++ are identical apart from the default libraries.

    So:

    g++ -o test test.cpp

    and gcc -lstdc++ -o test test.cpp

    are equivilent. Try:

    gcc -version

    and

    g++ -version

    to check.

    The problem is not that, it is that unlike GCC 2.95 and earlier versions, GCC 3.x supports namespaces and more vigourously enforces and encourages adherence to standards. Hence standard C++ headers with .h extensions are moved to the "backward" folder (for backward compatibility) and the standard library is in the std:: namespace. The project in its wisdom, also more recently decided to add #warning directives in the depreciated headers to encourage better (or at least more standard compliant) coding practices.

    Regardless of whether you invoke g++ or gcc, this is true.

    The use of the 'extensionless' standard makes a lot of sense since it avoids confusion between <string.h> the C string library header, and <string>, the C++ string class header. The C++ library standard specifies alternate 'extensionless' headers for C library files; the .h is removed and a 'c' prefixed, so <string.h> becomes <cstring>.

    It is normal, and advised to use .h or .hpp for your own headers and headres for non-ansi libraries.

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo