-4

I have a header file where I've defined a structure and its variable 'editor' and am using it in multiple files. I have not declared it anywhere else, yet it gives me multiple declaration error. I've also tried using #ifndef but to no avail.

This is my header file:

editorlib.h

#ifndef ED_LIB
#define ED_LIB

//#include files

struct terminalProperties {
    int rows,cols;
    struct termios origTerm;
    int mode;
}editor;
int TERMINATE=0;

//function definitions

#endif

My editorlib.cpp

#ifndef EDITORLIBRARY_H
#define EDITORLIBRARY_H
#include "editorLibrary.h"

getAttr() {
    tcgetattr(STDIN_FILENO, TCSAFLUSH, &editor.origterm);
}
//further code
#endif

Driver file vi_editor.cpp:

#ifndef MAIN_H
#define MAIN_H

#include "editorLibrary.h"
int main(){

    //some code

    while(1){
        //some code
        if(TERMINATE)
            break;
    }
    //some code
    return 0;
}

#endif

My normal.cpp:

#ifndef NORMALMODE_H
#define NORMALMODE_H
#include "editorLibrary.h"

void normalMode() {
    editor.mode = 1;
    //further code
}
#endif

My command.cpp:

#ifndef COMMANDMODE_H
#define COMMANDMODE_H
#include "editorLibrary.h"

void commandMode() {
    editor.mode = 2;
    //further code
}
#endif

Similarly I have a few other files where I'm not declaring the editor variable but just using it in a similar manner as above. I can't seem to find why it tell me about multiple declaration.

telekineser
  • 88
  • 1
  • 1
  • 9

1 Answers1

0

Multpile definition of a header declared variable

This is already a violation of the One Definition Rule. The simple answer is "don't". You can only declare in a header, via extern, and define in a single .cpp file.

user207421
  • 305,947
  • 44
  • 307
  • 483