This is relevant: https://stackoverflow.com/questions/45987571/difference-between-constexpr-and-static-constexpr-global-variable
I made the following experiment:
s.h:
constexpr const char* s{"aabbcc"};
a.cc:
#include "s.h"
#include <stdio.h>
void a()
{
printf("a(): %p, %s\n", &s, s);
}
b.cc:
#include "s.h"
#include <stdio.h>
void b()
{
printf("b(): %p, %s\n", &s, s);
}
main.cc:
void a();
void b();
int main(int, char**)
{
a();
b();
return 0;
}
$ clang++-devel -c a.cc -o a.o
$ clang++-devel -c b.cc -o b.o
$ clang++-devel -c main.cc -o main.o
$ clang++-devel a.o b.o main.o -o main
$ ./main
a(): 0x200578, aabbcc
b(): 0x200580, aabbcc
Also checked that the main executable contains the string aabbcc just once, so it is not duplicated. But there are two different pointers pointing to it.
Then I changed the header s.h to:
inline constexpr const char* s{"aabbcc"};
And re-ran:
$ ./main
a(): 0x200578, aabbcc
b(): 0x200578, aabbcc
now it is the same pointer.
So, I added inline to the variables involved.