【问题标题】:How can I fix g++ Undefined symbols for architecture x86_64 error?如何修复架构 x86_64 错误的 g++ 未定义符号?
【发布时间】:2014-06-23 04:25:37
【问题描述】:

首先,我已经阅读了Stackoverflow上关于这个问题的相关内容,但我仍然无法解决。我已经尽可能地简化了我的代码。

我只有一个包含 .h 和 .cpp 文件的自定义类,但在尝试从 main.cpp 创建此类的实例时出错。

ma​​in.cpp

#include "Customer.h"
using namespace std;

int main() {
    Customer a("string");

    return 0;
}

Customer.h

using namespace std;

class Customer {
public:
    Customer(string input);
};

Customer.cpp

#include "Customer.h"
using namespace std;

Customer::Customer(string input) {
}

我得到的错误信息如下?

  gcc *.cpp -o k


  Undefined symbols for architecture x86_64:
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
        _main in main-40340f.o
    "std::terminate()", referenced from:
        ___clang_call_terminate in main-40340f.o
    "___cxa_begin_catch", referenced from:
        ___clang_call_terminate in main-40340f.o
    "___gxx_personality_v0", referenced from:
        _main in main-40340f.o
        Dwarf Exception Unwind Info (__eh_frame) in main-40340f.o
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)

clang: error: linker command failed with exit code 1 (use -v to see invocation)

我运行 Mac OS X 10.9 和 Sublime Text 3。gcc -v 给出以下结果:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

当我编写空构造函数而不是这些构造函数时,它可以毫无问题地编译。

什么可能导致这个问题?

【问题讨论】:

    标签: c++ string macos gcc


    【解决方案1】:

    两个问题:

    1. 你应该#include &lt;string&gt;
    2. 要在 OS X 上编译 C++ 代码,请使用 clang++g++

    【讨论】:

      【解决方案2】:

      使用g++ 代替gcc

      $ g++ *.cpp -o k
      

      然后编译器驱动程序知道链接 C++ 运行时库。鉴于您使用的实际上是clang,而不是gcc,那么这样更好:

      $ clang++ *.cpp -o k
      

      【讨论】:

      • 虽然在这种情况下没关系,g++clang++ 在 OS X 下是不同的。g++ 将使用 libstdc++clang++ 将使用 libc++。两者都使用 LLVM 前端。
      • @sharth 这很有趣,如果使用 3rd-party 库会产生影响。
      【解决方案3】:

      使用“g++”而不是“gcc”来编译和链接您的应用程序。它会自动知道需要包含的 C++ 库。

      【讨论】:

        【解决方案4】:

        您需要安装命令行工具。 https://developer.apple.com/downloads/

        【讨论】:

          【解决方案5】:

          g++ main.cpp(or Customer.cpp) -o k 是不够的。您应该同时编译它们。 "g++ main.cpp Customer.cpp (和其他 *.cpp(s),如果你有的话) -o target " 会很好用。

          【讨论】:

            猜你喜欢
            • 2017-07-07
            • 2019-12-05
            • 2017-02-15
            • 1970-01-01
            • 1970-01-01
            • 2015-10-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多