c++namespacesduplicate-symbol

cpp Error Duplicate symbol using namespace


This is going to make me nuts !

First of all I already tried solutions from this thread (C++) Linking with namespaces causes duplicate symbol error with no success (maybe I doing it wrong)

Here is what i am attempted to do:

// ofApp.hpp
#ifndef __ofApp_hpp
#define __ofApp_hpp

namespace vec_color {
    glm::vec4 blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 blueViolet = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    glm::vec4 darkRed = glm::vec4( 200.f, 0.f, 0.f, 255.f );
    glm::vec4 green = glm::vec4( 0.f, 255.f, 0.f, 255.f );
    glm::vec4 grey = glm::vec4( 128.f, 128.f, 128.f, 255.f );
    glm::vec4 lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
    glm::vec4 pink = glm::vec4( 255.f, 192.f, 203.f, 255.f );
    glm::vec4 red = glm::vec4( 255.f, 0.f, 0.f, 255.f );
    glm::vec4 yellow = glm::vec4( 255.f, 255.f, 0.f, 255.f );
    glm::vec4 white = glm::vec4( 255.f, 255.f, 255.f, 255.f );
};

...

#endif

And I then obtain

duplicate symbol 'vec_color::grey' in:
    /Users/[...]/Build/Intermediates.

So I tried

// ofApp.hpp
#ifndef __ofApp_hpp
#define __ofApp_hpp

namespace vec_color {
    extern glm::vec4 blue;
    extern glm::vec4 blueViolet;
    extern glm::vec4 darkRed;
    extern glm::vec4 green;
    extern glm::vec4 grey;
    extern glm::vec4 lightGoldenRodYellow;
    extern glm::vec4 pink;
    extern glm::vec4 red;
    extern glm::vec4 yellow;
    extern glm::vec4 white;
};

...

// ofApp.cpp

vec_color::blue       = glm::vec4( 0.f, 0.f, 255.f, 255.f );
vec_color::blueViolet = glm::vec4( 138.f, 43.f, 226.f, 255.f );
vec_color::darkRed    = glm::vec4( 200.f, 0.f, 0.f, 255.f );
vec_color::green      = glm::vec4( 0.f, 255.f, 0.f, 255.f );
vec_color::grey       = glm::vec4( 128.f, 128.f, 128.f, 255.f );
vec_color::lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
vec_color::pink       = glm::vec4( 255.f, 192.f, 203.f, 255.f );
vec_color::red        = glm::vec4( 255.f, 0.f, 0.f, 255.f );
vec_color::yellow     = glm::vec4( 255.f, 255.f, 0.f, 255.f );
vec_color::white      = glm::vec4( 255.f, 255.f, 255.f, 255.f );

...

C++ requires a type specifier for all declarations

on each lines variable declaration in cpp file.

I'm stuck if someone have a tip please tell me.

Franck.


Solution

  • This way should work:

    ofApp.hpp

    #ifndef __ofApp_hpp
    #define __ofApp_hpp
    
    namespace vec_color {
        extern glm::vec4 blue;
        extern glm::vec4 blueViolet;
        extern glm::vec4 darkRed;
        extern glm::vec4 green;
        extern glm::vec4 grey;
        extern glm::vec4 lightGoldenRodYellow;
        extern glm::vec4 pink;
        extern glm::vec4 red;
        extern glm::vec4 yellow;
        extern glm::vec4 white;
    };
    
    ...
    
    #endif
    

    ofApp.cpp

    #include "ofApp.hpp"
    
    namespace vec_color {
        glm::vec4 blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
        glm::vec4 blueViolet = glm::vec4( 0.f, 0.f, 255.f, 255.f );
        glm::vec4 darkRed = glm::vec4( 200.f, 0.f, 0.f, 255.f );
        glm::vec4 green = glm::vec4( 0.f, 255.f, 0.f, 255.f );
        glm::vec4 grey = glm::vec4( 128.f, 128.f, 128.f, 255.f );
        glm::vec4 lightGoldenRodYellow = glm::vec4( 250.f, 250.f, 210.f, 255.f );
        glm::vec4 pink = glm::vec4( 255.f, 192.f, 203.f, 255.f );
        glm::vec4 red = glm::vec4( 255.f, 0.f, 0.f, 255.f );
        glm::vec4 yellow = glm::vec4( 255.f, 255.f, 0.f, 255.f );
        glm::vec4 white = glm::vec4( 255.f, 255.f, 255.f, 255.f );
    };
    

    or this way: ofApp.cpp

    glm::vec4 vec_color::blue = glm::vec4( 0.f, 0.f, 255.f, 255.f );
    ...
    

    Anyway this way you end with global variables. This is not recommended.