Thread: This code won't compile in Red Hat Linux 9

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    This code won't compile in Red Hat Linux 9

    Hi all,

    I've been using Dev C++ compiler in windows for C & C++.
    I started out reading Schildt's book, "Teach Yourself C" which I finished a couple months ago. Then a friend who is going to school for computing said that he was taking a C++ course and suggested that I give C++ a try. I already had Jesse Liberty's book, "Teach Yourself C++ in 21 days which was put out in 1999 and I worked with it a little bit back then and the examples from book compiled fine in Linux and in Windows. I heard that the standards had changed but Schildt's Book "Teach Yourself C++" 3rd Edition lived up to those standards.

    So heres my problem.

    This next bit of code works fine in Windows but I get errors in Red hat Linux 9.0

    I'll just write out a little example code:

    #include <iostream>
    using namespace std;

    int main()
    {

    cout << "Just a test\n";

    return 0;
    }

    This won't code won't compile for me in Red hat 9.

    Can someone please show me what I need to do to get this code compiled.

    Thanks so much

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I get errors in Red hat Linux 9.0
    And those errors are...

    >Can someone please show me what I need to do to get this code compiled.
    Gladly. Show us how you're doing it and we'll tell you what you're doing wrong.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You did remember to use g++ for compiling C++, and not gcc which is used to compile C
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    12

    It worked!

    Hi Prelude & Salem. I have to tell you both that I didn't even know about the "g++ thing." It was about 5 years ago that I last fooled around with C++ so I guess they changed things around somewhere in between then and now. Anyway, the program compiled. Thank you both for replying.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    gcc will happily compile c++ code for you, you do however need to tell it to link against the standard c++ library when it calls the linker. Ie:

    gcc -lstdc++ myfile.cc -o myfile

    Alan

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    I think it can also look at the extension of the source code file.

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    9
    woodalan@jehovah:~ > gcc test.cc -o test
    /tmp/ccxrJYpP.o(.text+0x1b): In function `main':
    : undefined reference to `std::cout'
    /tmp/ccxrJYpP.o(.text+0x20): In function `main':
    : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'

    .....
    woodalan@jehovah:~ > gcc test.cc -o test -lstdc++
    woodalan@jehovah:~ > ./test
    TEST
    Looks like it can't find the symbols unless you use -lstdc++ to me. Could just be my version of gcc

    Alan

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Looks like it can't find the symbols unless you use -lstdc++ to me.
    That's because, as Prelude and Salem said, gcc is a C compiler by default. If you don't want to use -lstdc++ each time, use g++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Errors in my Code. Can anyone help?
    By DGLaurynP in forum C Programming
    Replies: 1
    Last Post: 10-06-2008, 09:36 AM
  2. Compile problem on linux
    By cnb in forum C Programming
    Replies: 3
    Last Post: 09-29-2008, 04:14 AM
  3. Problem with desktop on Red Hat linux
    By osal in forum Linux Programming
    Replies: 0
    Last Post: 11-10-2004, 01:07 PM
  4. Code will not compile - code from a book!
    By Chubz in forum C++ Programming
    Replies: 19
    Last Post: 09-12-2004, 10:21 PM
  5. how to compile C++ file on linux OS
    By singh_nav in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2004, 06:30 AM