2014-06-28 93 views
0

這是一個非常簡單的C++程序。爲什麼不能用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上,我得到這個錯誤:

 
[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) 

我在做什麼錯?

+3

'鐺++的main.cpp -o main'你不鏈接其他文件。 – yizzlez

+0

它看起來像編譯得很好。鏈接是你的問題。 –

回答

6

main.cpp包括test.h;它不包括test.cpp。您可以編譯並通過指定他們既作爲輸入,同時將它們鏈接:

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

有道理。編譯器無法知道在哪裏可以找到頭文件的實現。謝謝。 – user3785066

2

你錯過了其他的cpp文件: clang++ main.cpp test.cpp -o main

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 – indivisible

+3

作者問他做錯了什麼。我回答說編譯命令中缺少其他cpp文件。我在這裏是新來的,所以也許我錯過了其他的東西,但爲什麼它沒有回答這個問題?請確認。 – nochkin

相關問題