[C++] clang编译报链接错误

1. 问题

// prog1.cc

#include 

int main()
{
    std::cout << "Hello world" << std::endl;
    return 0;
}

Mac上使用clang编译,

cc prog1.cc

会报错,

Undefined symbols for architecture x86_64:
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:

...

      std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in prog1-18ddb0.o
      Dwarf Exception Unwind Info (__eh_frame) in prog1-18ddb0.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

2. 解决方案

(1)cc -lstdc++

$ cc -lstdc++ prog1.cc
$ ./a.out
Hello world

(2)clang++

$ clang++ prog1.cc

(3)g++

$ g++ prog1.cc

参考

mac gcc 编译错误
Options for Linking

你可能感兴趣的:([C++] clang编译报链接错误)