如何为架构x86_64错误修复g ++未定义符号?

时间:2014-05-06 13:53:40

标签: 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);
};

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

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

什么可能导致这个问题?

5 个答案:

答案 0 :(得分:14)

两个问题:

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

答案 1 :(得分:4)

使用g++代替gcc

$ g++ *.cpp -o k

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

$ clang++ *.cpp -o k

答案 2 :(得分:3)

使用&#34; g ++&#34;代替&#34; gcc&#34;编译和链接您的应用程序。它自动了解需要包含的C ++库。

答案 3 :(得分:0)

您需要安装命令行工具。 here

答案 4 :(得分:-2)

g ++ main.cpp(或Customer.cpp)-o k是不够的。你应该同时编译它们。 &#34; g ++ main.cpp Customer.cpp(如果你有其他* .cpp(s))-o target&#34;  会很好。

相关问题