0

I'm trying to get started with c++. I'm on a mac with Mavericks OS installed.

The program I'm trying to compile is:

#include <iostream>

int main()
{
    std::cout << "Enter two numbers:" << std::endl;

    return 0;
}

Here is the version of cc:

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix

However, when I try to compile:

cc test.cc

I get the following error:

...
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.o
  ___clang_call_terminate in test-AeQQK5.o
"___cxa_end_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.o
"___gxx_personality_v0", referenced from:
  std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.o
  std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in test-AeQQK5.o
  Dwarf Exception Unwind Info (__eh_frame) in test-AeQQK5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What am I doing wrong?

ConditionRacer
  • 4,418
  • 6
  • 45
  • 67
  • @TheBuzzSaw The tooltip on the down arrow says it all: "**this question shows no research effort,** it is unclear or **not useful.**" Also, "questions must demonstrate a minimal understanding of the problem being solved". –  Jan 05 '14 at 17:26
  • I think that's a bit harsh. He clearly found the compiler but had no idea there were two modes. – TheBuzzSaw Jan 05 '14 at 17:43

3 Answers3

2

You are compiling it as a C program. Compile it as a C++ program instead:

c++ test.cc

Better throw in a few warning flags too:

c++ -Wall -Wextra -pedantic-errors test.cc

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • @RandomParentheses Throw that book far away (but only after you have burnt it with gasoline). These are the kind of crappy books that will later assert that "arrays are pointers", "references are pointers" and all sort of other popular (and terrible) mischiefs. –  Jan 05 '14 at 17:35
  • @H2CO3 This is actually "C++ Primer" which is the top beginner book mentioned here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – ConditionRacer Jan 05 '14 at 17:38
  • @RandomParentheses That's a sorry case then. You'd be better off reading one of Bjarne Stroustrup's books for sure. –  Jan 05 '14 at 17:57
1

You need to run clang++, not just clang.

TheBuzzSaw
  • 8,648
  • 5
  • 39
  • 58
1

While it is perfectly fine to compile a C++ source *.cc with gcc (it is treating files with that extension as C++ source) the linker needs explicit information to link against the proper c++ library, too.

(Note: I assume clang and gcc are similar regarding this issue)