简体   繁体   English

Clang不工作。 未定义的符号错误。 代码在gcc中编译良好

[英]Clang not working. Undefined symbol error. Code compiles fine in gcc

I decided to go and give Clang a go, since I've heard good things about it, and I wanted to try out some of the C++11 features. 我决定去给Clang一个去,因为我听说过它很好,我想尝试一些C ++ 11的功能。 So, as usual I started out with a simple hello world program. 所以,像往常一样,我开始使用一个简单的hello world程序。

#include <iostream>
using namespace std;

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

What compiled just fine using GCC isn't compiling in Clang. 使用GCC编译好的内容不是在Clang中编译的。 Instead, I get this error. 相反,我得到了这个错误。

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)

As per requested in the comments: 根据评论中的要求:

You need to use clang++ , as clang is the C compiler. 你需要使用clang++ ,因为clang是C编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码用clang编译但不用gcc编译 - Code compiles with clang but not with gcc 为什么这个代码无法用gcc 4.8.5编译,而它用clang编译好 - Why this code fails to compile with gcc 4.8.5 while it compiles fine with clang 为什么下面的代码不能用gcc编译,但用clang编译好 - Why the below code does not compile with gcc but compiles fine with clang 使用stdlibc ++ 4.7启用C ++ 11时,clang错误输出,而gcc编译正常 - When enabling C++11 with stdlibc++ 4.7, clang error out, while gcc compiles fine 错误:clang 没有可行的重载,用 gcc 编译 - error: no viable overloading with clang, compiles with gcc lambda 使用 gcc 捕获错误,使用 clang 编译 - lambda capture error with gcc, compiles with clang Variadic模板代码在GCC 4.6上编译,但不在clang或GCC 4.7上编译 - Variadic template code compiles on GCC 4.6 but not on clang or GCC 4.7 Eclipse显示错误,但gcc编译正常 - Eclipse shows an error but gcc compiles fine const 指针专业化使用 MSVC 生成未解析的符号,并且适用于 GCC/Clang - const pointer specialization produces unresolved symbol with MSVC and is fine with GCC/Clang 未定义对..错误的引用(Linux)-在OSX中可以正常编译 - Undefined reference to .. error (Linux) - Compiles fine in OSX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM