-2

I have a header file config.h where I'm simply defining a string with a value using

namespace configuration {
    const char* name = "test";
}

And I access it from a .cpp file as configuration::name. But then I'm getting a compiler error:

error: ld returned 1 exit status

If I change it to const string name = "test"; it works. Why? And how do I fix this to be able to use a const char* instead?

Walter
  • 44,150
  • 20
  • 113
  • 196

1 Answers1

1

The difference between the two strings

namespace config {
  const char* test1 = "test1";
  const std::string test2 = "test2";
}

is that the second is const and hence has internal linkage, while the first is not (it's merely a pointer to a const) and requires external linkage, i.e. you must provide a definition in some .cpp file, failing of which causes your linker error.

Walter
  • 44,150
  • 20
  • 113
  • 196