c++cmakefile

c++ include redeclaration


I have two libraries in my include path:

/usr/local/include/lib1/
/user/include/lib2/

Both define a header file "vec.h" and I get the following error:

/usr/local/include/lib1/vec.h:22: error: redeclared with...

How to handle this?


Solution

  • You do:

    #include "lib1/vec.h"
    #include "lib2/vec.h"
    

    Your makefile should then have include paths up to the lib1 and lib2.

    -I /usr/local/include /user/include
    

    You should ensure that the headers have guards around them to make sure they don't get declared twice. You should see something like:

    #ifndef MYHEADER_H_ab2592zx1__
    #define MYHEADER_H_ab2592zx1__
    
    #include ...
    #include ...
    class ...
    
    #endif
    

    You should have these guards regardless of whether you have two includes of the same name and they should typically end with something fairly random in order to limit/lower the possibility that they conflict with a header guard in another file.