-1

The following Hello World program compiles inside Xcode, but not when compiled with via clang in the Terminal:

#include <iostream>
int main()
{
  std::cout << "Hello World!";
}

specifically, the command i'm using is:

clang c++test.cpp

where c++test is the name of the file. This produces a bunch of gibberish errors like:

(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*,     
char const*, std::__1::ios_base&, char) in c++test-497cf6.o

As well as this:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I checked and the c++ libs appear present on the system, so I'm pretty sure I'm doing something work. Is there a -framework I need to link to?

PopKernel
  • 4,110
  • 5
  • 29
  • 51
  • not the reason, but add `return 0` to main and try to change the name of the file (remove the `++` symbols) – bolov Apr 19 '14 at 18:14
  • Your errors are gibberish because your not reading the whole thing. And you haven't bothered to even post the whole error. – Dan Apr 19 '14 at 18:16
  • @bolov: Re:`return 0`, no, that isn't needed at all, see e.g. https://stackoverflow.com/questions/22239/why-does-int-main-compile – Benjamin Bannier Apr 19 '14 at 18:37
  • @rubenvb: Yes it's a duplicate. My apologies. I sifted through several questions with similar answers, but "My code won't compile" is a very common question. – PopKernel Apr 19 '14 at 19:01
  • @Dan: I didn't post the errors because there were a hundred lines of em. And I didn't think, (and I was right) that that portion of the error was relevant to the answer. – PopKernel Apr 19 '14 at 19:02

1 Answers1

1

If you compile/link C++, use (clan)g++. This will ensure the C++ standard library is also linked in.

Alternatively, add -lstdc++ or in your case -lc++ to the link command. I would just call clang++ though.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Yes, the reason I am using the command line instead of the terminal is because I'm making an applescript droplet to pass the file to the CL. As such, I will probably just append "-lstdc++" to the shell script. Many thanks! – PopKernel Apr 19 '14 at 19:06
  • @rubenvb btw., while you're here, i (think i) am aware that you long ago added some exception support to clang for windows. do you know how it is with the official releases now? is all in place or still some way to go? – Cheers and hth. - Alf Apr 19 '14 at 19:07
  • @Cheersandhth.-Alf The dw2 (32-bit) exception handling was fixed a long time ago. Work is now underway for the seh (64-bit) implementation to match GCC's seh implementation. See [here](http://reviews.llvm.org/D3419). This is the clang part of the patch, the LLVM part is linked within. I haven't tested these patches. My "eureka" in the Lounge came too soon, I didn't get anything working. I believe there is one more big step that needs to be fixed (see [here](http://llvm.org/bugs/show_bug.cgi?id=16779)). I think this is MinGW specific, I frankly don't know how this relates to MSVC compatibility. – rubenvb Apr 25 '14 at 12:41