2

I have a variable const char * FILENAME = "a/file/name" defined in two .cpp files, called a.cpp and b.cpp. Nothing includes these files directly, and they only include a.h and b.h respectively into themselves. Both of those header files are then included in main.cpp

My problem is that I'm getting the following linker error:

a.obj : error LNK2005: "char const * const FILENAME" (?FILENAME@@3PBDB) already defined in b.obj

I don't use file scope variables very often, so I thought this would be okay... What am I doing wrong here?

Anne Quinn
  • 12,609
  • 8
  • 54
  • 101
  • Yes this violates the one definition rule. You have two definitions of `FILENAME`. An easy fix is to make it `static const char FILENAME[] = "a/file/name";` – M.M Sep 24 '15 at 07:33
  • 1
    @Morlacke no it isn't. `FILENAME` is not `const` in OP's code. The `const` applies to the things being pointed to. – M.M Sep 24 '15 at 07:37
  • Having said that, the error message shows `char const * const FILENAME` so maybe you made one of them `const` and the other one not `const` – M.M Sep 24 '15 at 07:43

1 Answers1

5

If they represent the same file, then you should declare one of them as extern to reference the definition in the other module.

File1.cpp

const char * FILENAME = "a/file/name";

File2.cpp

extern const char * FILENAME;

If they are two individual variables which happen to have the same name, then you should declare them as static so they are visible only inside the compilation unit.

static const char * FILENAME = "a/file/name";

The reason why this happens is, because the implicit static applies only to const objects. Your pointer is not const though, only the referenced object is const.

To achieve this you would have to write:

const char * const FILENAME = "a/file/name";
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • they are the second. I guess I mistakenly assumed `static` was inplicit when defined at the file scope. Thanks! – Anne Quinn Sep 24 '15 at 07:34
  • In C++ static const and const is the same declaration. http://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c – Morlacke Sep 24 '15 at 07:35
  • Personally I would discourage the usage of global variables when not really neccessary, but that depends on the usage of course. – Devolus Sep 24 '15 at 07:35
  • 1
    @Morlacke Only for top-level `const` which Devolus's example mistakenly omits. `const char *const FILENAME = "a/file/name";` would do the job. There's no harm in including `static` anyway though. – M.M Sep 24 '15 at 07:36
  • @M.M, can you explain what you mean by top-level const? – Devolus Sep 24 '15 at 07:37
  • @Devolus a `const` which qualifies the variable. `const int x` - top level, `x` cannot be modified. `const int *y;` - not top level, we can write `y = z;` – M.M Sep 24 '15 at 07:37
  • @M.M, OK, I see what you mean. Yes, that's right, because FILENAME is not const, only the content of FILENAME is considered const here. – Devolus Sep 24 '15 at 07:39
  • @Morlacke Oviously not with his compiler/linker, or he wouldn't have gotten the error message. – A.Franzen Sep 24 '15 at 07:39
  • Oh, so that's what it was. Morlacke is right, but I needed to define the pointer as constant as well. So with the definition `const char *const FILENAME` it being static is then implied. I get mixed up by that sometimes – Anne Quinn Sep 24 '15 at 07:41