"duplicate symbols for architecture x86_64"

I've been googling this compiling error for hours now, trying every little thing I've come across—and so far nothing's working.

My header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//quadquiz.h
class QuadQuiz{
    public:
		void title();
        void process();

	private:
        int numCorrect;
        int myProblems;
        bool isCorrect;
        double percentCorrect;

        bool checkCorrect(int index, int sum[], int product[]) const;
        void displayFactors(int index, int factor1[], int factor2[]) const;
        void displayProblem(int index, int sum[], int product[]) const;
        int getUniqueNum(bool picked[]) const;
};


My implementation file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//quadquizimp.cpp

#include "QuadQuiz.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <time.h>
#include <ctime>
#include <string>
using namespace std;

ofstream fout("QuadQuizOut.txt");

void QuadQuiz::title(){
	cout << "==============   Q U A D R A T I C   F A C T O R I N G   Q U I Z   ==============\n\n";
	fout << "==============   Q U A D R A T I C   F A C T O R I N G   Q U I Z   ==============\n\n";
	cout << "                                   by\n";
	fout << "                                   by\n";
	cout << "                            Robert VanCleave\n";
	fout << "                            Robert VanCleave\n";
	cout << "=================================================================================\n";
	fout << "=================================================================================\n";
	cout << "You will be given a quadratic expression, for example (x^2 - 17x + 72), to factor.\n";
	fout << "You will be given a quadratic expression, for example (x^2 - 17x + 72), to factor.\n";
	cout << "To continue with this example, in this expression the factors are (x - 9) (x - 8)\n";
	fout << "To continue with this example, in this expression the factors are (x - 9) (x - 8)\n";
	cout << "You would enter the constants, separated by a space: -9 -8 (in either order)";
	fout << "You would enter the constants, separated by a space: -9 -8 (in either order)";
	cout << "=================================================================================\n\n";
	fout << "=================================================================================\n\n";
    cout << "How many problems are we solving today? ";
    fout << "How many problems are we solving today? ";
    cin >> myProblems;
    fout << myProblems << endl;
    cout << "=================================================================================\n\n";
    fout << "=================================================================================\n\n";
}
void QuadQuiz::process(){
	int factor1[] = {-2, -2,  -7,   5, -6, -25,  7,  -2, -1,  -9,  -4, -3};
	int factor2[] = {-5, -3,  -3,  -3, -2,   4, 11,  10,  7,  -2,   4,  3};
	int product[] = {10,  6,  21, -15, 12,-100, 77, -20, -7,  18, -16, -9};
	int sum[]     = {-7, -5, -10,   2, -8, -21, 18,   8,  6, -11,   0,  0};
	bool picked[] = { 0,  0,   0,   0,  0,   0,  0,   0,  0,   0,   0,  0};
	
	time_t seconds;
	time(&seconds);
    srand((unsigned int) seconds);
	
	numCorrect=0;
			
	for (int i=0; i<myProblems; i++) {
		int index=getUniqueNum(picked);
		displayProblem(index, sum, product);
		
		if (checkCorrect(index, sum, product)) {
			cout << right << setw(25) << "Correct!";
			displayFactors(index,factor1,factor2);
			numCorrect++;
		}
		if (!checkCorrect(index, sum, product)) {
			cout << right << setw(25) << "Incorrect, sorry.";
			displayFactors(index,factor1,factor2);
		}
	}
	cout << "\n";
	fout << "\n";
	cout << "You got " << numCorrect << " questions correct out of a total of " << myProblems << " questions.\n";
	fout << "You got " << numCorrect << " questions correct out of a total of " << myProblems << " questions.\n";
}

bool QuadQuiz::checkCorrect(int index, int sum[], int product[]) const {
	
	int answerOne;
	int answerTwo;
	
	cin >> answerOne >> answerTwo;
	fout<< answerOne << " " << answerTwo << endl;
	
	if (answerOne+answerTwo==sum[index] && answerOne*answerTwo==product[index]) {
		return true;
	}
	else {
		return false;
	}
}

void QuadQuiz::displayFactors(int index, int factor1[], int factor2[]) const {
	
	if (factor1[index] >= 0 && factor2[index] >= 0) {
        cout << left << setw(20) << "  (x + " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
        fout << left << setw(20) << "  (x + " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
    }
    if (factor1[index] >= 0 && factor2[index] < 0) {
        cout << left << setw(20) << "  (x + " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
        fout << left << setw(20) << "  (x + " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
    }
    if (factor1[index] < 0 && factor2[index] >= 0) {
        cout << left << setw(20) << "  (x - " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
        fout << left << setw(20) << "  (x - " << abs (factor1[index]) << ") (x + " << abs (factor2[index]) << ")\n";
    }
    if (factor1[index] < 0 && factor2[index] < 0) {
        cout << left << setw(20) << "  (x - " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
        fout << left << setw(20) << "  (x - " << abs (factor1[index]) << ") (x - " << abs (factor2[index]) << ")\n";
    }
}

void QuadQuiz::displayProblem(int index, int sum[], int product[]) const {
	
	cout << right << setw(25) << "Quadratic";
	fout << right << setw(25) << "Quadratic";
	
     if (sum[index] >= 0 && product[index] >= 0) {
        cout << left << setw(20) << "  (x^2 + " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
        fout << left << setw(20) << "  (x^2 + " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
     }
     if (sum[index] < 0 && product[index] < 0) {
        cout << left << setw(20) << "  (x^2 - " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
        fout << left << setw(20) << "  (x^2 - " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
     }
     if (sum[index] >= 0 && product[index] < 0) {
        cout << left << setw(20) << "  (x^2 + " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
        fout << left << setw(20) << "  (x^2 + " << abs (sum[index]) << "x - " << abs (product[index]) << ") ";
     }
     if (sum[index] < 0 && product[index] >= 0) {
        cout << left << setw(20) << "  (x^2 - " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
        fout << left << setw(20) << "  (x^2 - " << abs (sum[index]) << "x + " << abs (product[index]) << ") ";
     }
     
     cout << left << setw(16) << "Factors a & b: ";
     fout << left << setw(16) << "Factors a & b: ";
}

int QuadQuiz::getUniqueNum(bool picked[]) const {
    int index = -1;
    bool done = false; 
    while(!done){
        index = (rand() % 12);
        
        if(picked[index] == false){
            picked[index] = true;
            done = 1; 
        }
    }
    return index;
}


and main file
1
2
3
4
5
6
7
8
9
//quadquiz.cpp
#include "QuadQuizImp.cpp"

int main(){
    QuadQuiz q;
    q.title();
    q.process();
    return 0;
}


complete error message
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Building target: QuadQuiz Assignment
Invoking: MacOS X C++ Linker
g++  -o "QuadQuiz Assignment"  ./QuadQuiz.o ./QuadQuizImp.o   
duplicate symbol __ZN8QuadQuiz7processEv in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol __ZN8QuadQuiz5titleEv in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol _fout in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol __ZNK8QuadQuiz12getUniqueNumEPb in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol __ZNK8QuadQuiz12checkCorrectEiPiS0_ in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol __ZNK8QuadQuiz14displayFactorsEiPiS0_ in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
duplicate symbol __ZNK8QuadQuiz14displayProblemEiPiS0_ in:
    ./QuadQuiz.o
    ./QuadQuizImp.o
ld: 7 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [QuadQuiz Assignment] Error 1
Based on that error I thought include guarding my headers would work, but it doesn't make a difference if i do that or not. Without that working, I'm at a complete loss here.

Working with Eclipse c++ for mac here, and i've read that a big issue is that it is building everything simultaneously rather than only building my main file. I've right-clicked to build only that file, but it still won't let me RUN that file without building all three.

Also, the assignment requires that I definite my functions in the header file and call those functions in the other two cpp files. I've heard this can trigger this error, but I have no choice but to find a workaround that still allows me to do this.
Last edited on
It looks like the compiler finds the libraries for both architectures.

A first step could be removing the libraries for one architecture and leaving just the other and see what happen.
1
2
3
4
5
6
7
8
9
//quadquiz.cpp
#include "QuadQuizImp.cpp"  // read this line very carefully

int main(){
    QuadQuiz q;
    q.title();
    q.process();
    return 0;
}
Only one of the files (the imp file) even has libraries inside it at all, so if I remove them none of the files will. I've tried moving all the libraries to the header and the main file at separate occasions, and the compiler doesn't understand the imp file when i do so.

And I've checked and rechecked that line, I'm not sure what was wrong with it (the actual file names are camelcase just like the #include, if that's what you were pointing out).
Last edited on
And I've checked and rechecked that line,

What keskiverto is trying to point out is that you should NEVER #include a .cpp file.
.cpp files should be part of your project and be compiled individually by your IDE.
Part of my assignment requirement is to #include that specific .cpp implementation file AS a .cpp file.


When I tried the exact same code on bloodshed and codeblocks, it runs fine. Appears to only have a problem on Eclipse c++ for mac. I don't know if it's because Eclipse builds everything in the project to run it (and if that is the case, how to get it to not do that), or if it's some other Eclipse or mac specific error. But since 98% of the time that is the only thing I'm going to have access to, I need to figure out how to get this working in that context.
Last edited on
Indeed.

The #include is like a on-the-fly cut-n-paste that the compiler does.

Your build commands
1. Create an object file from QuadQuizImp.cpp.
2. Create an object file from QuadQuiz.cpp which contains all the text of QuadQuizImp.cpp too via include.
3. Link together the object code from the two object files that each have an independent copy of "symbols" created from the source code in QuadQuizImp.cpp. Error.
I've understood that process, my problem is what I'm supposed to do with that information. Include guarding #ifndef statements don't do anything to prevent the build from creating copies of those files into the object file, and I haven't been able to find an alternative to try.
Last edited on
Foo.h
1
2
3
4
5
6
7
8
9
10
#ifndef FOO_H
#define FOO_H
// definition of custom type "Foo"
class Foo {
  int x;
public:
  Foo();
  int bar();
};
#endif 


Foo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
// This file implements something that
// needs to know the type Foo
// hence the include
#include "Foo.h"

Foo::Foo()
 : x( 42 )
{}

int Foo::bar() {
  return x;
}



test.cpp
1
2
3
4
5
6
7
8
#include <iostream> // defines std::cout
#include "Foo.h"    // defines Foo

int main() {
  Foo obj;
  std::cout << obj.bar();
  return 0;
}
Topic archived. No new replies allowed.