Thread: Code not compiling in Linux

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Code not compiling in Linux

    I'm not sure why, but this code will compile fine in VS.NET 2003, but not gcc under Linux. It seems to be something with the using statement, almost like it can't find the std stuff. I only posted a small portion of the error messages, but they seem to just repeat over and over. Plus, it was too much to post in one message.

    Full source:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    // displays main menu
    int main_menu(void);
    // display news sites in file
    void print_sites(void);
    
    int main()
    {
            bool cont = true;
            int option = -1;
    
            do
            {
                    // display main menu
            option = main_menu();
    
                    // switch statement for options
                    switch(option)
                    {
                    case 0:
                            cout << "Exiting program..." << endl;
                            cont = false;
                            break;
                    case 1:
                            break;
                    case 2:
                            break;
                    case 3:
                            break;
                    case 4:
                            print_sites();
                            break;
                    }
            } while(cont);
    }
    
    int main_menu()
    {
            int option;
    
        cout << "*** Welcome to RSS Reader ***" << endl;
        cout << "[1] Choose News Site" << endl;
        cout << "[2] Add News Site" << endl;
        cout << "[3] Remove News Site" << endl;
            cout << "[4] Show News Sites" << endl;
        cout << "[0] Exit Program" << endl;
        cout << "Or enter url: ";
    
            cin >> option;
            return option;
    }
    
    void print_sites()
    {
            ifstream sitefile("sites.txt");
            string str1, str2, str3;
    
            cout << "\n";
    
            while( !sitefile.eof() )
            {
                    getline(sitefile, str1, '\t');
                    getline(sitefile, str2, '\t');
                    sitefile >> str3;
    
                    sitefile.ignore(80, '\n');
    
                    cout << str1 << endl;
                    cout << "\t" << str2 << endl;
                    cout << "\t" << str3 << endl;
            }
    
            cout << "\n";
    
            sitefile.close();
    }
    Part of the error during compile:
    Code:
    chris@zion rss_reader $ gcc rss_reader.cpp
    rss_reader.cpp:81:2: warning: no newline at end of file
    /tmp/ccAWoF1y.o(.text+0x50): In function `main':
    : undefined reference to `std::cout'
    /tmp/ccAWoF1y.o(.text+0x55): In function `main':
    : 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*)'

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I got it to compile fine using:
    g++ -o bt bt.cpp -Wall

    If you used make you might want to make sure that its using the c++ compiler and not the C compiler.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What thantos said. Remember g++ will compile C code but gcc will not compile C++ code.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Sample Makefile:
    Code:
    .SUFFIXES:
    .SUFFIXES: .cpp
    
    CC := g++
    CCOPS := -Wall
    
    %:  %.cpp
      $(CC) $(CCOPS) -o $@ $<
    I use this as a basic Makefile and it works for puttering around. When I get into a project I just expand upon it (Usally by adding .o to the SUFFIXES, adding a .o target, and making the source code compile to object code)

  5. #5
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Ahh, that's my issue. I was using gcc. I'll try g++ in a bit.

  6. #6
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Worked fine. Thanks for the clue.

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 C code at visual C++ 6.0
    By hattusili in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 01:26 PM
  3. Replies: 27
    Last Post: 10-25-2007, 10:47 AM
  4. Problems with compiling code in cygwin
    By firyace in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2007, 08:16 AM
  5. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 12:50 PM