-1

I'm having an error I don't know how to fix in my large Operating Systems homework. The error I'm getting is "42 duplicate symbols for architecture x86_64". I presume this is to do with my global variables file "global.h". I have 3 global variables I use and "global.h" is included in an abstract class called "PageReplacementAlgorithm.cpp". I have around 6 classes that are derived from the PageReplacementAlgorithm class and they utilize these global variables. I think the problem comes in when I include all these derived classes in my "main.cpp" as I need to make new instances of them. How can I fix the implementation of the global variables?

Global.h

#include "PageTableEntry.h"
using namespace std;
#ifndef Global_H
#define Global_H

extern PageTableEntry pageTable[64];
extern int* frameTable;
extern int framesCount;

#endif

PageReplacementAlgorithm.h

#include "Global.h"
using namespace std;

#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H

class PageReplacementAlgorithm {
public:
   virtual int selectFrame(PageTableEntry &p) = 0;
};

#endif

Example Derived Class (FIFO)

include "PageReplacementAlgorithm.h"
using namespace std;

#ifndef FIFO_H
#define FIFO_H

class FIFO : public PageReplacementAlgorithm {
public:
    FIFO();
    int selectFrame(PageTableEntry &p);
private:
     int entries;
};

#endif

Main.cpp

 #include "Aging.cpp"
 #include "Clock.cpp"
 #include "FIFO.cpp"
 #include "MMU.cpp"
 #include "NRU.cpp"
 #include "Random.cpp"
 #include "SecondChance.cpp"
Matrix21
  • 115
  • 1
  • 2
  • 10

2 Answers2

2

Why do you include all cpp files in main.cpp? I think they contain same includes, right? Even you have the guards there, you do additional includes before that guards and that is probably the source of problems. The main.cpp could contain just main() function and import headers of your classes, there is no need to include cpp.

Also, you can modify your header files to look like this (for sake of extreme safety):

#ifndef PageReplacementAlgorithm_H
#define PageReplacementAlgorithm_H
#include "Global.h"
using namespace std;

...
#endif

I recommend you to look at answer C++ #include guards

Community
  • 1
  • 1
holmicz
  • 577
  • 4
  • 15
  • 2
    It's an age old technique, called (unimaginatively) [`Single Compilation Unit`](https://en.wikipedia.org/wiki/Single_Compilation_Unit) – StoryTeller - Unslander Monica Nov 27 '16 at 06:26
  • Thanks!! It is now compiling, I'll read up on the "Single Compilation Unit" as well @StoryTeller. Still new to C++. – Matrix21 Nov 27 '16 at 06:28
  • 2
    @StoryTeller Brilliant. Never heard of this before! Matrix21 you'd probably better stick to more normal ways to build code though – Jack Deeth Nov 27 '16 at 06:33
  • @StoryTeller I think, that you could use including cpp file into header for template implementation though. Even this seems little... messy to me. :-) – holmicz Nov 27 '16 at 06:37
  • @OndřejHolman, several standard library implementations use `*.tpp` files to do exactly that ;) But it has certain merits for actual source files as well. You can emulate Java's package access specifier like that, for instance. – StoryTeller - Unslander Monica Nov 27 '16 at 06:37
  • @StoryTeller Well, I haven't used it yet, I do my implementations directly in header file for templates. Are there any other benefits of doing so, except better organization? – holmicz Nov 27 '16 at 06:42
  • @OndřejHolman, Well, with the help of the preprocessor, you can `#include` different `*.tpp` files for different architectures and/or compiler options. That along with the usual meta-programming tricks can produce code that is especially tailored for an application. – StoryTeller - Unslander Monica Nov 27 '16 at 06:44
  • 1
    @StoryTeller Different implementations for different architectures sounds reasonable to me. Meta-programming sounds like magic to me. :-) Thanks for sharing! – holmicz Nov 27 '16 at 06:45
0

If you get rid of #include "(anything).cpp, things should work much better. When you build the project, or run the compiler e.g. g++ main.cpp foo.cpp, that's when those .cpp files get built and linked into your program.

Jack Deeth
  • 3,062
  • 3
  • 24
  • 39