Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BobXRoberts

macrumors newbie
Original poster
Mar 29, 2006
6
0
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> >::eek:perator>>(int&)
std::basic_ostream<char, std::char_traits<char> >::eek:perator<<(int)
std::ios_base::Init::Init()
std::ios_base::Init::~Init()
std::cin
std::cout
std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <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

macrumors 603
Oct 16, 2003
6,301
1
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

macrumors newbie
Original poster
Mar 29, 2006
6
0
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

macrumors 603
Oct 16, 2003
6,301
1
BobXRoberts said:
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.

man gcc said:
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

macrumors 6502a
Jun 17, 2003
873
3
gekko513 said:
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

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
mrichmon said:
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. :)
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
bousozoku said:
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

macrumors 68000
Jun 6, 2003
1,950
0
District of Columbia
BobXRoberts said:
/usr/bin/ld: Undefined symbols:
std::basic_istream<char, std::char_traits<char> >::eek:perator>>(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)
 

BobXRoberts

macrumors newbie
Original poster
Mar 29, 2006
6
0
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

macrumors 68000
Feb 1, 2005
1,887
1
BobXRoberts said:
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 http://www.opengl.org , you can find the the very early versions of Blue Book (reference of OpenGL) and 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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.