如何修复g ++未定义的符号架构x86_64错误? [英] How can I fix g++ Undefined symbols for architecture x86_64 error?

查看:237
本文介绍了如何修复g ++未定义的符号架构x86_64错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已经阅读了关于这个问题的Stackoverflow的相关内容,但我还是不能解决它。我已尽可能简化我的代码。



我只有一个自定义类与.h和.cpp文件,但我在尝试创建一个实例时出错



  #includeCustomer.h
using namespace std;

int main(){
Customer a(string);

return 0;
}

Customer.h

 使用namespace std; 

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

Customer.cpp

  #includeCustomer.h
using namespace std;

Customer :: Customer(string input){
}

我得到的错误信息如下?

  gcc * .cpp -ok 


架构x86_64的未定义符号:
std :: __ 1 :: basic_string< char,std :: __ 1 :: char_traits< char> ;, std :: __ 1 :: allocator< char> char const *,unsigned long),引用自:
_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& ),引用自:
_main in main-40340f.o
std :: __ 1 :: basic_string< char,std :: __ 1 :: char_traits< char>,std :: __ 1 :: allocator< ; char>> ::〜basic_string(),引用自:
_main in main-40340f.o
std :: terminate(),引用自:
___clang_call_terminate in main -40340f.o
___cxa_begin_catch,引用自:
___clang_call_terminate in main-40340f.o
___gxx_personality_v0,引用自:
_main in main-40340f.o
Dwarf Exception Unwind Info(__eh_frame)in main-40340f.o
ld:没有为架构x86_64找到符号
clang:error:linker命令失败,退出代码为1(使用-v参见调用)

clang:error:linker命令失败,退出代码1(使用-v查看调用)

我运行Mac OS X 10.9和Sublime Text 3. gcc -v提供以下内容:

 配置:--prefix = / Library / Developer / CommandLineTools / usr --with-gxx-include-dir = / usr / include / c ++ / 4.2.1 
Apple LLVM版本5.1(clang-503.0.40 )(基于LLVM 3.4svn)
目标:x86_64-apple-darwin13.1.0
线程模型:posix

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



什么可能导致这个问题?

解决方案

两个问题:


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


First of all, I've read the related content on Stackoverflow on this problem but I still can't solve it. I've simplified my code as much as possible.

I've only a custom class with .h and .cpp files but I get error while trying to create an instance of this class from 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) {
}

The error message I get is the following?

  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)

I run Mac OS X 10.9 and Sublime Text 3. gcc -v gives the following:

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

It compiles without problem when I write empty constructors instead of these.

What could cause this problem?

解决方案

Two problems:

  1. You should #include <string>.
  2. To compile C++ code on OS X, use clang++ or g++.

这篇关于如何修复g ++未定义的符号架构x86_64错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆