Results 1 to 5 of 5

Thread: c++ program refuses to compile with g++ on Ubuntu 12.04

  1. #1
    Join Date
    Dec 2011
    Beans
    17

    c++ program refuses to compile with g++ on Ubuntu 12.04

    Hello, I'm trying to compile a program & though the code seems to be ok, it has refused to compile, it keeps showing errors! The problem is the same code actually did run successfully on windows devc++ application. I saved the program on Desktop as "calc3.cpp", changed directory of terminal to Desktop, then i used this command "gcc calc3.cpp -o calc3" but i get errors. Below is the program code & also errors

    CODE:

    Code:
    /* A calculator program without error recovery*/
    #include <iostream>
    using namespace std;
    int main(int argc, char* argv[])
    {
        int numerator =1;
        cout << "Pls enter the numerator=";
        cin >>  numerator;
        int denominator =1;
        cout << "Pls enter the denominator=";
        cin >> denominator;
        int DivResult = (numerator/denominator);
        cout << " the answer is =" << DivResult << endl;
        char exitcharacter;
        cout << endl << "press a key + enter=";
        cin >> exitcharacter;
        return 0;
    }
    ERRORS:

    HTML Code:
    /tmp/ccUf8nqF.o: In function `main':
    calc3.cpp:(.text+0x1c): undefined reference to `std::cout'
    calc3.cpp:(.text+0x21): 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*)'
    calc3.cpp:(.text+0x30): undefined reference to `std::cin'
    calc3.cpp:(.text+0x35): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
    calc3.cpp:(.text+0x4c): undefined reference to `std::cout'
    calc3.cpp:(.text+0x51): 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*)'
    calc3.cpp:(.text+0x60): undefined reference to `std::cin'
    calc3.cpp:(.text+0x65): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
    calc3.cpp:(.text+0x8d): undefined reference to `std::cout'
    calc3.cpp:(.text+0x92): 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*)'
    calc3.cpp:(.text+0xa2): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)'
    calc3.cpp:(.text+0xaa): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
    calc3.cpp:(.text+0xb2): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
    calc3.cpp:(.text+0xba): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
    calc3.cpp:(.text+0xc1): undefined reference to `std::cout'
    calc3.cpp:(.text+0xc6): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
    calc3.cpp:(.text+0xd6): 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*)'
    calc3.cpp:(.text+0xe5): undefined reference to `std::cin'
    calc3.cpp:(.text+0xea): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
    /tmp/ccUf8nqF.o: In function `__static_initialization_and_destruction_0(int, int)':
    calc3.cpp:(.text+0x112): undefined reference to `std::ios_base::Init::Init()'
    calc3.cpp:(.text+0x117): undefined reference to `std::ios_base::Init::~Init()'
    collect2: ld returned 1 exit status
    Thank you very much.

  2. #2
    Join Date
    May 2009
    Location
    Bangalore
    Beans
    103
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: c++ program refuses to compile with g++ on Ubuntu 12.04

    Quote Originally Posted by zayid View Post
    Hello, I'm trying to compile a program & though the code seems to be ok, it has refused to compile, it keeps showing errors! The problem is the same code actually did run successfully on windows devc++ application. I saved the program on Desktop as "calc3.cpp", changed directory of terminal to Desktop, then i used this command "gcc calc3.cpp -o calc3" but i get errors.
    If you had used gcc to compile, then it is wrong. You have to use g++ to compile cpp files

    Regards,
    ksprasad

  3. #3
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: c++ program refuses to compile with g++ on Ubuntu 12.04

    Looks like you didn't include the std C++ library in your makefile for linking.
    $ gcc calc3.cpp -o calc3 -lstdc++

    You probably want to enable all warnings too -Wall does that.

    You might need to add a -L with the library path as well. It has been awhile since I did any C/C++ development.

    MS probably automatically added those libs for you ... so you didn't have to learn this yourself. I consider that a bad practice - YOU should have to know the libraries included in your output program.
    I'm pretty certain that gcc will compile C++ code - and has for years.

    This http://stackoverflow.com/questions/1...ween-g-and-gcc has a better explanation of the gcc/g++ differences. It appears that g++ automatically adds libstdc++ to your link for you.
    Last edited by TheFu; June 2nd, 2012 at 09:23 PM. Reason: added stackoverflow answer.

  4. #4
    Join Date
    May 2012
    Beans
    25

    Re: c++ program refuses to compile with g++ on Ubuntu 12.04

    try using g++ rather than gcc.
    infact, g++ (the c++ compiler) will not be installed unless you have installed it in the first place.!

  5. #5
    Join Date
    Dec 2011
    Beans
    17

    Re: c++ program refuses to compile with g++ on Ubuntu 12.04

    Thanks ksprasad worked like a charm

Bookmarks

Posting Permissions

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