Click to See Complete Forum and Search --> : basic programming problem


Jazzalex
05-26-2004, 12:35 PM
Hi ! I have a very simple code that throws me some linking errors when I try to run it - filename : geruest.cpp :

#include <iostream>
using namespace std;
int main()
{
int c;

while ( (c = cin.get ()) != EOF)
{
cout << (char)c;
}

return 0;
}

When doing a gcc geruest.cpp -o test
I get this

[root@localhost root]# gcc -o geruest.cpp test
gcc: test: No such file or directory
gcc: no input files
[root@localhost root]# gcc geruest.cpp -o test
/tmp/ccH5UqmO.o(.text+0x14): In function `main':
: undefined reference to `std::cin'
/tmp/ccH5UqmO.o(.text+0x19): In function `main':
: undefined reference to `std::basic_istream<char, std::char_traints<char> >::get()'
/tmp/ccH5UqmO.o(.text+0x34): In function `main':
: undefined reference to `std::cout'
/tmp/ccH5UqmO.o(.text+0x39): 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)'
/tmp/ccH5UqmO.o(.text+0x68): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccH5UqmO.o(.text+0x97): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccH5UqmO.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

I guess I have to include the right lib but don't know which and how.

Thanks

-- A l e x

eslackey
05-26-2004, 02:03 PM
I don't know if it makes a difference but you might want to try:

# g++ geruest.cpp -o test

See if that helps.

eyceguy
05-26-2004, 02:16 PM
i believe your loop should be
while(cin.get(c))

it automatically checks for EOF so there shouldnt be a problem other than that

maccorin
05-26-2004, 06:29 PM
eslackey is correct, you need to use g++ for c++, not gcc the link errors are because if you call it gcc it does not link to libstdc++ automatically (g++ does though)

GaryJones32
05-26-2004, 11:47 PM
Originally posted by Jazzalex


When doing a gcc geruest.cpp -o test
I get this

[root@localhost root]# gcc -o geruest.cpp test
gcc: test: No such file or directory
gcc: no input files

while what everyone said about g++ is correct
the command is still in the wrong order
and you hadn't gotten to the linking problem yet
it should be
g++ -o test geruest.cpp

maccorin
05-27-2004, 12:25 AM
Originally posted by GaryJones32
while what everyone said about g++ is correct
the command is still in the wrong order
and you hadn't gotten to the linking problem yet
it should be
g++ -o test geruest.cpp

doh!
can't believe i missed that

Jazzalex
05-27-2004, 06:33 AM
Thanks to all ! That worked. But why doesn't gcc automatically link to libstdc++ ? Of course this :

gcc -o test geruest.cpp -lstdc++ worked as well.

Thanks

-- A l e x

maccorin
05-27-2004, 02:14 PM
don't count on gcc with -lstdc++ always working. If you call it via g++ (which is usually just a symlink to gcc), it will link the the needed libs for c++, it decides what to automatically link to by the value of argv[0] (the name of the program that you call)

bwkaz
05-27-2004, 06:51 PM
Originally posted by Jazzalex
But why doesn't gcc automatically link to libstdc++? Because gcc is a C compiler, not a C++ compiler. libstdc++ is just extra baggage when you're writing a C program...