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?

2
  • @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". Commented Jan 5, 2014 at 17:26
  • I think that's a bit harsh. He clearly found the compiler but had no idea there were two modes. Commented Jan 5, 2014 at 17:43

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

@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.
@H2CO3 This is actually "C++ Primer" which is the top beginner book mentioned here: stackoverflow.com/questions/388242/…
@RandomParentheses That's a sorry case then. You'd be better off reading one of Bjarne Stroustrup's books for sure.
1

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

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.