I have the following "constants" header:
#ifdef __cplusplus extern "C" { #endif #pragma once #ifndef CONSTANTS_H #define CONSTANTS_H const char * kFoo = "foo"; const char * kBar = "bar"; #endif #ifdef __cplusplus } #endif
I #include title of this header in the Xc and Yc files.
Please note that I am not including this in Xh or Yh .
Xc and Yc files are collected in object files, which are stored in a static library called libXY.a .
When I include Xh and Yh in Zh , and when I refer to libXY.a , I cannot compile Zc without errors:
#include "Xh" #include "Yh"
I get the following compilation errors when trying to compile Zc :
/path/to/libXY.a(Xo):(.data+0x0): multiple definition of `kFoo` /path/to/libXY.a(Yo):(.data+0x0): first defined here /path/to/libXY.a(Xo):(.data+0x8): multiple definition of `kBar` /path/to/libXY.a(Yo):(.data+0x8): first defined here
I tried installing kFoo and kBar on extern , but that does not help.
How to resolve multiple definitions when I only include constants once (via the #ifndef CONSTANTS_H header protector)?
source share