I'm attempting to compile an example C++ program in OS X on the terminal, however I'm coming up against an error that I don't understand.
My source code looks like this:
#include <iostream>
using namespace std;
const int NUMBER = 12;
int main ()
{
int firstNum;
int secondNum;
firstNum = 18;
cout << "Line 9; firstNum = " << firstNum << endl;
cout << "Line 10: Enter an integer: ";
cin >> secondNum;
cout << endl;
cout << "Line 13: secondNum = " << secondNum << endl;
firstNum = firstNum + NUMBER + 2 * secondNum;
cout << "Line 15: The new value of " << "firstNum = " << firstNum << endl;
return 0;
}
and I build it like this:
$ cc -Wall -v -o example example.cpp
Here's the error I'm getting:
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.10.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name example.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241.9 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -cxx-isystem /usr/include/c++/4.2.1 -stdlib=libc++ -Wall -fdeprecated-macro -fdebug-compilation-dir /Users/mattandrews/projects/cpp-codes -ferror-limit 19 -fmessage-length 161 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.10.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/7g/mxwtz9zn2f797v601x7s12zm0000gn/T/example-568d29.o -x c++ example.cpp
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin14.1.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.2.1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.10.0 -o example /var/folders/7g/mxwtz9zn2f797v601x7s12zm0000gn/T/example-568d29.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
"std::istream::operator>>(int&)", referenced from:
_main in example-568d29.o
"std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
_main in example-568d29.o
"std::ostream::operator<<(int)", referenced from:
_main in example-568d29.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in example-568d29.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in example-568d29.o
"std::cin", referenced from:
_main in example-568d29.o
"std::cout", referenced from:
_main in example-568d29.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 example-568d29.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 example-568d29.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm using cc
to compile, but it doesn't seem to be able to pick up the C++ standard library. I set CPLUS_INCLUDE_PATH
to /usr/include/c++/4.2.1
which is where the library resides, but the error persists.
I tried to compile in Xcode and it worked fine, but I want to be able to compile at the command line.
How do I correctly link the C++ standard library at the command line?