gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 19:37:26 24 4
gpt4 key购买 nike

首先,我已经阅读了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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com