c++ in linux....

This is a discussion on c++ in linux.... within the Linux Programming forums, part of the Platform Specific Boards category; Well, I am getting into Linux, and decided to try to write a hello world app, for mthe tutorials on ...

  1. #1
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77

    c++ in linux....

    Well, I am getting into Linux, and decided to try to write a hello world app, for mthe tutorials on the site.

    So, here is my code..
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
      cin.get();
      return 1;
    }
    So I try compiling it with GCC and I get...

    Code:
    fhqwhgads@ryan1:~/EtopacSoftware> gcc HelloWorld.cpp
    /tmp/cc1ADmAR.o(.text+0x25): In function `main':
    : undefined reference to `std::cout'
    /tmp/cc1ADmAR.o(.text+0x2a): 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*)'
    /tmp/cc1ADmAR.o(.text+0x35): In function `main':
    : undefined reference to `std::cin'
    /tmp/cc1ADmAR.o(.text+0x3a): In function `main':
    : undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
    /tmp/cc1ADmAR.o(.text+0x66): In function `__static_initialization_and_destruction_0(int, int)':
    : undefined reference to `std::ios_base::Init::Init[in-charge]()'
    /tmp/cc1ADmAR.o(.text+0x95): In function `__tcf_0':
    : undefined reference to `std::ios_base::Init::~Init [in-charge]()'
    /tmp/cc1ADmAR.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    fhqwhgads@ryan1:~/EtopacSoftware> gcc HelloWorld.cpp
    /tmp/ccZKk43V.o(.text+0x25): In function `main':
    : undefined reference to `std::cout'
    /tmp/ccZKk43V.o(.text+0x2a): 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*)'
    /tmp/ccZKk43V.o(.text+0x35): In function `main':
    : undefined reference to `std::cin'
    /tmp/ccZKk43V.o(.text+0x3a): In function `main':
    : undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
    /tmp/ccZKk43V.o(.text+0x66): In function `__static_initialization_and_destruction_0(int, int)':
    : undefined reference to `std::ios_base::Init::Init[in-charge]()'
    /tmp/ccZKk43V.o(.text+0x95): In function `__tcf_0':
    : undefined reference to `std::ios_base::Init::~Init [in-charge]()'
    /tmp/ccZKk43V.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
    collect2: ld returned 1 exit status
    Any idea? I am not new to c++, just Linux.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you either need to add the proper flags to GCC or use G++ (which is just GCC with flags)

    basically, it's trying to compile C code and you gave it C++ code.

    I suggest trying this:
    Code:
    g++ test.cpp -Wall -W -pedantic -ansi -o test.exe
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    use g++ to compile. GCC understands c++, but by default it tries to link it with c libraries, hence all the undefined references.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    Quote Originally Posted by major_small
    you either need to add the proper flags to GCC or use G++ (which is just GCC with flags)

    basically, it's trying to compile C code and you gave it C++ code.

    I suggest trying this:
    Code:
    g++ test.cpp -Wall -W -pedantic -ansi -o test.exe

    Why would I want to make an exe?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    well, you are making an executable, but by convention, you don't include an extension for it. what he is showing off though, is that -o <filename> is how you specify the output file, because without it, the executables output name will simply be a.out
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    using namespace Trooper; St0rmTroop3er's Avatar
    Join Date
    Sep 2003
    Posts
    77
    oooah ok. what would I put there for linux app?

  7. #7
    xhi
    xhi is offline
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    the original code you posted compiles fine with just
    Code:
    g++ file.cpp
    i suggest you read the man page on g++ to check out all the options you can use

    >> oooah ok. what would I put there for linux app?
    you dont have to put anything specific for a linux app. linux does not see files in terms of extensions, but rather permissions. so if you have a file named

    foo

    it will execute the same as foo as it would as

    foo.bar
    foobar
    foo.exe
    a.out

    names dont matter.

    so just put somthing descriptive for the '-o' option or leave it blank

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 12:40 PM
  2. Wireless Network Linux & C Testbed
    By james457 in forum Networking/Device Communication
    Replies: 3
    Last Post: 06-11-2009, 12:03 PM
  3. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 05:17 PM
  4. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21