Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ld: duplicate symbol

Tags:

c++

xcode

linker

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.

ld output:

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

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

#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.

like image 489
epochwolf Avatar asked Nov 12 '08 01:11

epochwolf


People also ask

What is duplicate symbol?

Duplicate symbols occur when you have both added an implementation file (. cpp) to your project and #included it. This way, the implementation file (.

What does duplicate symbol for architecture x86_ 64?

Well, it means we're trying to link the same symbol name (in our case, a method) from two (or more) different source files. The fix was easy: rename one of the methods by updating the header file, the source file (. c or .


2 Answers

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.o and generics.o.

So it looks like you're including the io_functions.cpp file into the main.cpp and generics.cpp which is a bad idea at the best of times.

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

like image 70
paxdiablo Avatar answered Sep 23 '22 04:09

paxdiablo


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

like image 43
Josh Kelley Avatar answered Sep 20 '22 04:09

Josh Kelley