编译时x86_64的未定义符号 [英] Undefined symbols for x86_64 when compiling

查看:111
本文介绍了编译时x86_64的未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在大学的编程课程中进行入门,在此之前,我要对格式不佳表示抱歉,我不知道如何正确设置格式。但是回到我的问题,所以我们主要使用c ++,但是在编译文件时遇到了一些问题。编译器在大多数情况下都可以工作,它会告诉我何时我的代码中有错误,但是一旦我消除了这些错误,它就会告诉我x86_64体系结构中存在未定义符号。

I尝试查找,发现有些东西说Mac(我正在运行Mavericks)有时具有冲突的32位和64位库。当我第一次进行查找时,我发现有人在其中说要添加链接器 -stdlib = libstdc ++的线程,但这没有用。我在终端使用g ++进行了尝试,但是随后我的信息变得更糟了。我已将大多数代码简化为以最少的代码出现错误的地方。

So I'm in an intro to Programming course in University and before anything else, let me say sorry for the poor formatting, I have no clue how to format this properly. But back to my questions, so we are primarily using c++ but I've been having some issues when I'm compiling my files. The compiler works for the most part and it will tell me when I have errors in my code but once I iron those out, it gives me a message saying that there are Undefined symbols for my x86_64 architecture.
I tried looking that up and found some things saying that Macs (I'm running Mavericks) sometimes have conflicting 32 bit and 64 bit libraries. When I first looked this up, I found some threads where people said to add the linker "-stdlib=libstdc++", but that did not work. I tried this in terminal using g++, but then my message gets worse. I've simplified most of my code to where the error pops up with minimal code.

这是我的main.cpp:

Here is my main.cpp:

    #include <iostream>
    #include "helpers.h"
    #include <string>

    using namespace std;

    int main(){
        int num = 1;
        string y = "hi";
        hello(num, y);
    }

这是我的助手。h

    #ifndef __helpers__
    #define __helpers__

    #include <string>
    #include <iostream>

    using namespace std;

    void hello(int number, string name);

    #endif

最后是我的助手。cpp:

and finally here is my helpers.cpp:

    #include <iostream>

    using namespace std;

    void hello(int number, string name){
    return;
    }

在g ++中进行编译时,会收到以下消息:

When I compile it in g++, I get the following message:


架构x86_64的未定义符号:
hello(int,std :: __ 1 :: basic_string,std :: __ 1 :: allocator> ),引用自:main-88f880.o中的
_main。
ld:找不到架构x86_64
的符号:错误:链接器命令失败,退出代码为1(使用- v以查看调用)

Undefined symbols for architecture x86_64: "hello(int, std::__1::basic_string, std::__1::allocator >)", referenced from: _main in main-88f880.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,当我使用 g ++ main.cpp -stdlib = libstdc ++在g ++中对其进行编译时,相同的消息。我尝试使用clang进行编译,但得到了一条较长的消息:

However, when I compile it in g++ with "g++ main.cpp -stdlib=libstdc++", I get the same message. I tried compiling it with clang, but I got a longer message:


hello(int,std :: string),引用自:main-2806e5.o中的
_main
std :: allocator :: allocator(),引用自:main-2806e5.o中的
_main在
std :: allocator ::〜allocator(),引用自:main-2806e5.o中的
_main
std :: basic_string,std :: allocator> :: basic_string(char const *,std :: allocator const&),引用自:main-2806e5.o中的
_main。
std :: basic_string,std :: allocator> :: basic_string(std :: string const&),引用自:
main 2806e5.o中的$ main
std :: basic_string,std :: allocator> ::〜basic_string(),引用源于:
main-2806e5.o中的$ main $ b std :: ios_base :: Init :: Init(),引用自:main-2806e5.o中的
___cxx_global_var_init。
std :: ios_base :: Init :: ~~ Init() ,引用自:main-2806e5.o中的
___cxx_global_var_init b $ b std :: terminate(),从以下地址引用:
在main-2806e5.o中的___clang_call_terminate $ b ___gxx_personality_v0,引用自:main-2806e5.o中的
_main
main-2806e5.o中的矮度异常展开信息(__eh_frame)
ld:找不到以下符号:架构x86_64
铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)

"hello(int, std::string)", referenced from: _main in main-2806e5.o "std::allocator::allocator()", referenced from: _main in main-2806e5.o "std::allocator::~allocator()", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::basic_string(std::string const&)", referenced from: _main in main-2806e5.o "std::basic_string, std::allocator >::~basic_string()", referenced from: _main in main-2806e5.o "std::ios_base::Init::Init()", referenced from: ___cxx_global_var_init in main-2806e5.o "std::ios_base::Init::~Init()", referenced from: ___cxx_global_var_init in main-2806e5.o "std::terminate()", referenced from: ___clang_call_terminate in main-2806e5.o "___cxa_begin_catch", referenced from: ___clang_call_terminate in main-2806e5.o "___gxx_personality_v0", referenced from: _main in main-2806e5.o Dwarf Exception Unwind Info (__eh_frame) in main-2806e5.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

感谢您的帮助。

推荐答案

如果您只运行 g ++ main.cpp ,您将不编译helpers.cpp。将helpers.cpp添加到命令行。

If you're only running g++ main.cpp, you're not going to compile helpers.cpp. Add helpers.cpp to the command line.

这篇关于编译时x86_64的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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