【问题标题】:Clang linking error on Mac OSXMac OSX 上的 Clang 链接错误
【发布时间】:2014-10-10 14:15:16
【问题描述】:

我有一个非常基本的 C++ 程序,我无法在 Mac OSX 10.9.3 上使用 g++ 进行编译。

Main.cpp

#include "MyClass.hpp"

int main() {
  MyClass::MyClass obj = MyClass::MyClass();
  return 0;
}

MyClass.hpp

class MyClass {
public:
  MyClass();
  void method();
private:

};

MyClass.cpp

#include "MyClass.hpp"

using namespace MyClass;

MyClass() {

};

void method() {

};

要编译它,我使用g++

g++ main.cpp -o out

这是我得到的结果:

Undefined symbols for architecture x86_64:
  "MyClass::MyClass()", referenced from:
      _main in main-2750ac.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

然后我运行g++ main.cpp -o out -v 并得到这个:

Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
 "/Applications/Xcode.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 236.3 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1 -stdlib=libc++ -fdeprecated-macro -fdebug-compilation-dir /Users/stan/Documents/Info/Ateliers C++/Ateliers/Drawing -ferror-limit 19 -fmessage-length 120 -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/58/ch14y2yx05v7vy1yvrsmy_3m0000gn/T/main-4d20fb.o -x c++ issue/main.cpp
clang -cc1 version 5.1 based upon LLVM 3.4svn default target x86_64-apple-darwin13.2.0
ignoring nonexistent directory "/usr/include/c++/v1"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.9.0 -o out /var/folders/58/ch14y2yx05v7vy1yvrsmy_3m0000gn/T/main-4d20fb.o -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "MyClass::MyClass()", referenced from:
      _main in main-4d20fb.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我卡在这里了。

【问题讨论】:

  • 您是否感到怀疑,在您的输出中标记为“未找到符号”的函数在源文件 MyClass.cpp 中,您提到 nowhere i> 在你的编译命令中:g++ main.cpp -o out?

标签: c++ macos linker g++ osx-mavericks


【解决方案1】:

你不编译MyClass.cpp。试试这个:

g++ main.cpp MyClass.cpp -o out

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-26
    • 2011-09-12
    • 2012-12-12
    • 2012-04-11
    • 2016-03-31
    • 2014-05-12
    • 2016-04-21
    • 1970-01-01
    相关资源
    最近更新 更多