c++ - 如何修复 g++ Undefined symbols for architecture x86_64 错误?

标签 c++ string macos gcc

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

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

main.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);
};

客户.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

当我编写空构造函数而不是这些构造函数时,它编译没有问题。

什么会导致这个问题?

最佳答案

两个问题:

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

关于c++ - 如何修复 g++ Undefined symbols for architecture x86_64 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23496692/

相关文章:

c++ - 将类似涂鸦功能后的客户区保存为图像

c# - 是否有 C# 不区分大小写的等于运算符?

c++ - 交换两个字符串时访问正确的内存时遇到段错误

c# - 在多个分隔符上分隔字符串时,如何知道使用了哪个分隔符? (C#)

c - 段错误: 11 on mac but not on linux while creating an array in C

java - 设置 OS X 属性 apple.awt.UIElement 无效

Python 在 OS X 上的文本文件中捕获击键值

c++ - 如何使用类方法映射 MFC 工具栏按钮事件?

c++ - 如何让 CMake 在 Ubuntu 上识别 pthread?

c++ - 不需要其元素是默认元素和可复制构造的容器