ellysisland ellysisland - 7 months ago 33
C++ Question

C++ multiple definition linking error with const declared in .h

I am using some constants defined in


#ifndef __CONSTANTS_H__
#define __CONSTANTS_H__

namespace A{
namespace B{

const int FIRST = 1;
const int SECOND = 2;

I am including this file from 2 different .cpp (A.cpp and B.cpp)

gcc version 3.2.3 is giving me "multiple definition" linking errors

I solve the problem only by including another "File.h" which includes "Constants.h" and declares the same constants as "extern"

Can someone explain why? I supposed this was an issue in C, not C++


With g++ version 4.3.4:

constants.h:5: error: ISO C++ forbids declaration of `FIRST' with no type

after adding type int, it works.

So maybe you should use a newer compiler? Use g++ instead of gcc?