2

我决定尝试一下 Clang,因为我听说过关于它的好消息,并且我想尝试 C++11 的一些特性。所以,像往常一样,我从一个简单的 hello world 程序开始。

#include <iostream>
using namespace std;

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

使用 GCC 编译得很好的东西不能在 Clang 中编译。相反,我得到了这个错误。

Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
 "/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.8.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.cpp -pic-level 1 -mdisable-fp-elim -relaxed-aliasing -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 134.9 -v -resource-dir /usr/bin/../lib/clang/4.1 -fmodule-cache-path /var/folders/js/j_8fpm_j0vl1d_2r_lsxlddm0000gn/T/clang-module-cache -fdeprecated-macro -fdebug-compilation-dir /Users/alexanderstein/Desktop/build -ferror-limit 19 -fmessage-length 80 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime-has-arc -fobjc-runtime-has-weak -fobjc-dispatch-method=mixed -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/js/j_8fpm_j0vl1d_2r_lsxlddm0000gn/T/test-4P1Ij1.o -x c++ /Users/alexanderstein/Desktop/Programming/Sandbox/C_C++/test.cpp
clang -cc1 version 4.1 based upon LLVM 3.1svn default target x86_64-apple-darwin12.2.0
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin10/x86_64"
ignoring nonexistent directory "/usr/include/c++/4.0.0"
ignoring nonexistent directory "/usr/include/c++/4.0.0/i686-apple-darwin8/"
ignoring nonexistent directory "/usr/include/c++/4.0.0/backward"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/4.2.1
 /usr/include/c++/4.2.1/backward
 /usr/local/include
 /usr/bin/../lib/clang/4.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.8.0 -o a.out /var/folders/js/j_8fpm_j0vl1d_2r_lsxlddm0000gn/T/test-4P1Ij1.o -lSystem /usr/bin/../lib/clang/4.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
      _main in test-4P1Ij1.o
  "std::ios_base::Init::Init()", referenced from:
      ___cxx_global_var_init in test-4P1Ij1.o
  "std::ios_base::Init::~Init()", referenced from:
      ___cxx_global_var_init in test-4P1Ij1.o
  "std::cout", referenced from:
      _main in test-4P1Ij1.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 test-4P1Ij1.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 test-4P1Ij1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
4

1 回答 1

5

根据评论中的要求:

您需要使用clang++,就像clangC 编译器一样。

于 2012-10-31T22:02:55.187 回答