How can I avoid the LNK2005 linker error for variables defined in a header file?

后端 未结 7 1480
情话喂你
情话喂你 2020-12-19 09:28

I have 3 cpp files that look like this

#include \"Variables.h\"
void AppMain() {
    //Stuff...
}

They all use the same variables inside th

7条回答
  •  难免孤独
    2020-12-19 09:58

    Because "int slider" is already defined in another file? Check that you have header guards...

    #ifndef _VARIABLES_H_
    #define _VARIABLES_H_
    
    int slider;
    
    #endif
    

    If it is across multiple translation units, and you do want the variables to be different (ie not global), then maybe declare them in an anonymous namespace:

    namespace {
        int slider;
    }
    

    If you do want them global, look to James' solution.

提交回复
热议问题