mercredi 23 août 2017

Multpile definition of a header declared variable

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.

My makefile is:

DEPS = editorLibrary.h OBJ = vi_editor.o editorLibrary.o insertMode.o normalMode.o commandMode.o

CC = g++

CFLAGS = -I -Wall -std=c++11

%.o: %.cpp $(DEPS)

$(CC) -c -o $@ $< $(CFLAGS)

vi_editor: $(OBJ)

$(CC) -o $@ $^ $(CFLAGS)

My error looks like:

editorlib.o:(.bss+0x0): multiple definition of `editor'

vi_editor.o:(.bss+0x0): first defined here

editorlib.o:(.bss+0x48): multiple definition of `TERMINATE'

vi_editor.o:(.bss+0x48): first defined here

insert.o:(.bss+0x0): multiple definition of `editor'

vi_editor.o:(.bss+0x0): first defined here

insert.o:(.bss+0x48): multiple definition of `TERMINATE'

vi_editor.o:(.bss+0x48): first defined here

normal.o:(.bss+0x0): multiple definition of `editor'

vi_editor.o:(.bss+0x0): first defined here

normal.o:(.bss+0x48): multiple definition of `TERMINATE'

vi_editor.o:(.bss+0x48): first defined here

command.o:(.bss+0x0): multiple definition of `editor'

vi_editor.o:(.bss+0x0): first defined here

command.o:(.bss+0x48): multiple definition of `TERMINATE'

vi_editor.o:(.bss+0x48): first defined here

collect2: error: ld returned 1 exit status

Makefile:10: recipe for target 'vi_editor' failed

make: *** [vi_editor] Error 1

Aucun commentaire:

Enregistrer un commentaire