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$
Yes, you should use
g++to compile C++ instead ofgcc, 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
gccprovides the external interface for users to compile their programs. Thegcccommand will attempt to determine the language you want to compile from the file extensions on the source files. For example, a file namedhello.cwill compile as C, a file namedfoo.cppwill compile as C++, and a file namedbar.mwill compile as Objective-C.You can give the
-xoption to explicitly specify which language to compile, regardless of file extension. For example,gcc -x c++ main.cwill compilemain.cas a C++ file, despite having the.cextension.However, even though
gccwill 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. Sogcc -lstdc++ prog2.cppshould work fine.However, for convenience, another command is provided for C++ programmers. Namely,
g++. Theg++command will automatically link to the C++ standard library, so you don’t have to do it explicitly. It also causes.c,.hand.ifiles to be treated as C++ files.If you consistently stick with
gccfor compiling C andg++, you should have no problems.