C++ ld:重复符号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/282700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:20:03  来源:igfitidea点击:

ld: duplicate symbol

c++xcodelinker

提问by epochwolf

I'm working on a school project and I'm getting some weird errors from Xcode. I'm using TextMate's Command+R function to compile the project. Compilation seems to work okay but linking fails with an error message I don't understand.

我正在做一个学校项目,我从 Xcode 收到一些奇怪的错误。我正在使用 TextMate 的 Command+R 函数来编译项目。编译似乎工作正常,但链接失败并显示我不明白的错误消息。

ld output:

ld输出:

ld: duplicate symbol text_field(std::basic_istream >&)in /path/final/build/final.build/Release/final.build/Objects-normal/ppc/generics.o and /path/final/build/final.build/Release/final.build/Objects-normal/ppc/main.o collect2: ld returned 1 exit status

ld:/path/final/build/final.build/Release/final.build/Objects-normal/ppc/generics.o 和 /path/final/build/final 中的重复符号 text_field(std::basic_istream >&)。 build/Release/final.build/Objects-normal/ppc/main.o collect2: ld 返回 1 退出状态

Below is my file io_functions.cpp This is the only declaration of text_field in the entire project.

下面是我的文件 io_functions.cpp 这是整个项目中唯一的 text_field 声明。

#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

#ifndef ENDF
#define ENDF '|'
#define ENDR '\n'

/**
reads one field from a given input stream
Usage: var = text_field(in)
*/
string text_field(istream &in){
    string s;
    getline(in, s, ENDF);
    return s; 
}

long long_field(istream &in){
    return atol(text_field(in).c_str()); 
}

int int_field(istream &in){
    return atoi(text_field(in).c_str()); 
}

double double_field(istream &in){
    return atof(text_field(in).c_str()); 
}

#endif

What is going wrong? For a number of reasons I don't want to post my project's entire source.

出了什么问题?出于多种原因,我不想发布我项目的整个源代码。

回答by paxdiablo

My first thought was that you're including it twice on the linker command but it appears to be complaining about having the same function in main.oand generics.o.

我的第一个想法是您在链接器命令中包含两次它,但它似乎抱怨在main.o和 中具有相同的功能generics.o

So it looks like you're including the io_functions.cppfile into the main.cppand generics.cppwhich is a bad idea at the best of times.

因此,看起来您将io_functions.cpp文件包含在 中main.cppgenerics.cpp这在最好的情况下是个坏主意。

You should have a header file (e.g., io_functions.h) that specifies everything contained in io_functions.cppand include that headerfile into the other two.

您应该有一个头文件(例如io_functions.h),它指定包含在其中的所有内容io_functions.cpp并将该文件包含在其他两个文件中。

回答by Josh Kelley

It sounds like io_functions.cpp is being included twice (once by generics.cpp, once by main.cpp).

听起来 io_functions.cpp 被包含两次(一次是 generics.cpp,一次是 main.cpp)。

回答by Snow Albert

Use "inline" keyword decorate duplicate method

使用“inline”关键字装饰重复方法

inline string text_field(istream &in)

Inline functions are actual functions whose copy of the function body are injected directly into each place the function is called.

内联函数是实际函数,其函数体的副本直接注入到调用函数的每个位置。

For details, please see the article

详情请看文章