jeudi 4 mai 2017

Make - Multiple definitions errors when compiling C++ project

I have a problem and yes I acknowledge this kind of question has been asked before, but none of them were really helping me, as the context and kind of errors given were not exactly the same.

I have a project I'm trying to compile with a makefile. The compilation process is the following :

game_f.exe : objects/game_f.o objects/game_f_main.o archives/io.a
    @g++ $^ $(ALL_OTHER_NEEDED_LIBS) -o $@ -std=c++11 -pedantic -Wall -g

Now those lines are creating 'multiple definitions' errors. Such as those :

io.cpp:(.text+0x1FDA) : multiple definitions of <some function or variable name> :
archives/io.a(io.o):io.cpp:(.text+0x1FDA) : defined for the first time here

Now this is coming from a header file where I create a namespace. The namespace is created as such :

io.h :

namespace io {
    extern int TermWidth;
    [some other variables, declared with "extern <some type> name;"]
    extern void getTermWidth();
    [some other functions, declared with "extern <some type> name(args)"
}

io.cpp :

namespace io {
    TermWidth = 0;
    [other variables definitions]
    void getTermWidth() { ... }
}

The compiler seems to ignore the extern word when compiling from the makefile. But what is weird is if I type the entire g++ line, without going through the makefile, the compilation works.

This compiles fine on my macOS machine when going through the makefile, by the way, which is even weirder.

Does g++ have a special option for extern definitions ? Is this a known problem of g++ and make ? How could I avoid this error ?

Aucun commentaire:

Enregistrer un commentaire