C++ Primer (5th edition) beginner's quandary

c++, standard-library, std

Solution

Yes, you should use `g++` to compile C++ instead of `gcc`, but it's probably worth elaborating on the answer since you say you're new to using compilers.

GCC stands for the GNU Compiler Collection and is a collection of programs that can be used to compile source code for various languages. The command `gcc` provides the external interface for users to compile their programs. The `gcc` command will attempt to determine the language you want to compile from the file extensions on the source files. For example, a file named `hello.c` will compile as C, a file named `foo.cpp` will compile as C++, and a file named `bar.m` will compile as Objective-C.

You can give the `-x` option to explicitly specify which language to compile, regardless of file extension. For example, `gcc -x c++ main.c` will compile `main.c` as a C++ file, despite having the `.c` extension.

However, even though `gcc` will determine that your files are C++, it still won't link to the C++ standard library by default. This library must be linked to to compile anything but the most simple C++ files. You can link to the GCC implementation of the standard library by adding `-lstdc++` to your options. So `gcc -lstdc++ prog2.cpp` should work fine.

However, for convenience, another command is provided for C++ programmers. Namely, `g++`. The `g++` command will automatically link to the C++ standard library, so you don't have to do it explicitly. It also causes `.c`, `.h` and `.i` files to be treated as C++ files.

If you consistently stick with `gcc` for compiling C and `g++`, you should have no problems.

Problem

Possible Duplicate: GCC linker can’t find standard library? I am trying to mess with this C++ book I got for the holidays, I am coming from a limited understanding of python so this stuff is real strange to me. I typed out this code from one of the first lessons into my text editor and saved it as a .cpp file. ``` #include <iostream> int main() { std::cout << "Enter two numbers:" << std::endl; int v1 = 0, v2 = 0; std::cin >> v1 >> v2; std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0; } ``` but my terminal gives this crazy output when i try to compile it, whats going on? ``` Raymond-Weisss-MacBook-Pro:c++ Raylug$ gcc prog2.cpp Undefined symbols for architecture x86_64: "std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from: _main in cckdLEun.o "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from: _main in cckdLEun.o "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from: _main in cckdLEun.o "std::ios_base::Init::Init()", referenced from: __static_initialization_and_destruction_0(int, int)in cckdLEun.o "std::ios_base::Init::~Init()", referenced from: ___tcf_0 in cckdLEun.o "std::cin", referenced from: _main in cckdLEun.o "std::cout", referenced from: _main in cckdLEun.o "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from: _main in cckdLEun.o "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from: _main in cckdLEun.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status Raymond-Weisss-MacBook-Pro:c++ Raylug$ ```

Original source

Related problems