PDA

View Full Version : What the...!? I thought GCC was better.




BobXRoberts
Mar 29, 2006, 07:20 AM
I'm using the version of gcc installed from the Tiger Developer Tools outfit. I try to compile this simple C++ code:

#include <iostream>
using namespace std;

int main ()
{
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}

with gcc, using no special flags or suchlike. I get the following errors:

/usr/bin/ld: Undefined symbols:
std::basic_istream<char, std::char_traits<char> >::operator>>(int&)
std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
std::ios_base::Init::Init()
std::ios_base::Init::~Init()
std::cin
std::cout
std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
___gxx_personality_v0
collect2: ld returned 1 exit status

What's going on? Does gcc not support ANSI C++? I think not. I am stumped. Any help would be greatly appreciated :D



gekko513
Mar 29, 2006, 07:30 AM
g++ test.cpp -o test
macbook:~/Utvikling audun$ ./test
Please enter an integer value: 4
The value you entered is 4 and its double is 8.


Make sure you use g++. (g++ automatically links in the standard c++ libraries)

BobXRoberts
Mar 29, 2006, 11:37 AM
Cheers mate! I didn't know there were any libraries needed apart from iostream... I'm only just getting into C++, my faith in gcc is restored (after many happy experiences with C!) What _are_ the standard libraries btw?

gekko513
Mar 29, 2006, 11:48 AM
Cheers mate! I didn't know there were any libraries needed apart from iostream... I'm only just getting into C++, my faith in gcc is restored (after many happy experiences with C!) What _are_ the standard libraries btw?
I'm talking about the iostream stuff. If you compile with just gcc, it will compile fine because the header for iostream is included, but it fails on linking because the actual object files with the implementation of those functions aren't linked into the binary.

g++ lets you not worry about that linking stuff. I'm not even sure exactly what it links in because g++ has made me not worry :cool:, but I know you can compile the same thing with gcc if you include the correct options.

However, C++ programs often require class libraries as well as a com-
piler that understands the C++ language---and under some circumstances,
you might want to compile programs or header files from standard input,
or otherwise without a suffix that flags them as C++ programs. You
might also like to precompile a C header file with a .h extension to be
used in C++ compilations. g++ is a program that calls GCC with the
default language set to C++, and automatically specifies linking
against the C++ library. On many systems, g++ is also installed with
the name c++.

mrichmon
Mar 29, 2006, 02:53 PM
g++ lets you not worry about that linking stuff. I'm not even sure exactly what it links in because g++ has made me not worry :cool:, but I know you can compile the same thing with gcc if you include the correct options.

Yup. The "gcc" command links in only the basic C libraries. "g++" links in the basic C++ libraries.

In general, you can just think that "gcc" is a C compiler, and "g++" is a C++ compiler and ignore the finer details.

bousozoku
Mar 29, 2006, 04:21 PM
Yup. The "gcc" command links in only the basic C libraries. "g++" links in the basic C++ libraries.

In general, you can just think that "gcc" is a C compiler, and "g++" is a C++ compiler and ignore the finer details.

Really? The cc in gcc is compiler collection. You should be able to compile FORTRAN or COBOL or anything else with the correct pieces in place.

Generally, though, I build in Xcode and run at the shell prompt. It avoids the mess. If I build at the shell prompt, I use make. :)

zimv20
Mar 29, 2006, 05:19 PM
If I build at the shell prompt, I use make. :)
but what does make call? gcc or g++?

mrichmon
Mar 29, 2006, 05:51 PM
Really? The cc in gcc is compiler collection. You should be able to compile FORTRAN or COBOL or anything else with the correct pieces in place.


Originally "gcc" stood for "GNU C Compiler". "g++" was then the GNU C++ compiler.

GCC forked into EGCS "Enhanced GNU Compiler System". EGCS and GCC developed in parallel for a while. (I certainly was using GCC and EGCS for various reasons around 1995/1996.) In 1999 EGCS was adopted as the official version of GCC and this was when the name was changed from "GNU C Compiler" to "GNU Compiler Collection".

When you install the GNU project Fortran 77 compiler you get the command "g77" (GNU Fortran 77) to compile Fortran code.

When you install the GNU Java compiler you get the command "gcj".

You can certainly use the "gcc" compiler to compile C++ and in a small number of cases where you need to mess about with linking both the C and C++ basic libraries and other related hijinx.

In general, for clarity, and to avoid the occassional obscure build issues giving you grief, you are better off just using the dedicated commands.

savar
Mar 29, 2006, 08:01 PM
/usr/bin/ld: Undefined symbols:
std::basic_istream<char, std::char_traits<char> >::operator>>(int&)

Everybody else gave you good advice, but I thought I should add one more thing: The error you're getting is actually from 'ld', not directly from gcc. ld is the linker, and its telling you it couldnt find a number of symbols referenced from your program. If you want to link programs manually, this is the command you would use. (I imagine this is also how XCode links)

mrichmon
Mar 29, 2006, 10:06 PM
but what does make call? gcc or g++?

make calls whatever the author of the Makefile wrote it to call.

zimv20
Mar 29, 2006, 10:33 PM
make calls whatever the author of the Makefile wrote it to call.
well, yes, of course. i was asking bousozoku what he specified.

BobXRoberts
Mar 30, 2006, 12:44 PM
Ha I seem to have started a right discussion. I suppose I can get away with ignoring the details for now- I just like to know how it works, right down to the wire. That's the silly thing about how we learn programming and comp sci in general today- we focus on individual layers and neglect how they all connect together...

As we're here, does anyone know of a basic (and I mean basic, I'm trying to get into graphics) OS X openGL tutorial? I could go and buy a book. But I'm broke. :d

Soulstorm
Mar 31, 2006, 02:17 AM
Ha I seem to have started a right discussion. I suppose I can get away with ignoring the details for now- I just like to know how it works, right down to the wire. That's the silly thing about how we learn programming and comp sci in general today- we focus on individual layers and neglect how they all connect together...

As we're here, does anyone know of a basic (and I mean basic, I'm trying to get into graphics) OS X openGL tutorial? I could go and buy a book. But I'm broke. :d
I think that if you go to www.opengl.org , you can find the the very early versions of Blue Book (http://www.opengl.org/documentation/blue_book/) (reference of OpenGL) and Red Book (http://www.opengl.org/documentation/red_book/) (Programming Guide) in order to start with OpenGL.

Note that in both books, it is taken for granted the fact that you must know how to set up your environment for OS X. It's no big deal though. Just tell me to help you if you have any problems.

BobXRoberts
Apr 1, 2006, 02:21 PM
Thanks, everyone! I'm having a look around... very interesting it is too.