为什么这个简单的C ++代码不会用clang ++编译?

时间:2014-06-28 02:10:30

标签: c++ macos clang++

这是一个非常简单的C ++程序。

// test.h
class Test {
     public:
         Test();
};

// test.cpp
#include "test.h"
Test::Test()
{
   // do something
}

// main.cpp
#include "test.h"
int main() {
    Test t;
    return 0;
}

我正在尝试使用此命令行在OSX上使用clang ++编译它,我收到此错误:

[test]$ clang++ main.cpp -o main
Undefined symbols for architecture x86_64:
  "Test::Test()", referenced from:
      _main in main-017149.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是使用-v开关的编译:

[test]$ clang++ main.cpp -o main -v
Apple LLVM version 6.0 (clang-600.0.34.4) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
 "/Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.9.0 -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 241 -v -resource-dir /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/ramin/projects/algorithm_examples/test -ferror-limit 19 -fmessage-length 139 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.9.0 -fencode-extended-block-signature -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -vectorize-slp -o /var/folders/z5/hyt30v7d7t7_w1xfpdgqp05w0000gn/T/main-d17898.o -x c++ main.cpp
clang -cc1 version 6.0 based upon LLVM 3.5svn default target x86_64-apple-darwin13.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
 /usr/local/include
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/include
 /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o main /var/folders/z5/hyt30v7d7t7_w1xfpdgqp05w0000gn/T/main-d17898.o -lc++ -lSystem /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "Test::Test()", referenced from:
      _main in main-d17898.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 个答案:

答案 0 :(得分:7)

main.cpp包括test.h;它不包括test.cpp。您可以通过将它们指定为输入来同时编译和链接它们:

[test]$ clang++ main.cpp test.cpp -o main

答案 1 :(得分:2)

您错过了其他cpp文件: clang++ main.cpp test.cpp -o main