12

I am reading "C++ primer plus". In Chapter 9, it talks about the difference between C++ and C when dealing with const modifier:

"In C++ (but not C), the const modifier alters the default storage classes slightly. Whereas a global variable has external linkage by default, a const global variable has internal linkage by default.

...

If global const declaration had external linkage as regular variable do, this would be an error because you can define a global variable in one file only. That is, only one file can contain the proceding declaration, and the other files have to provide reference declarations using the extern keywords."

I tried to test this claim with the following program:

file.h:

using namespace std;

const char *constant = "Magic";

file1.cpp

#include <iostream>
#include "file.h"
extern void file2();
int main(){
  cout << "constant = " << constant << endl;
  file2();
}

file2.cpp

#include <iostream>
#include "file.h"

void file2(){
  cout << "file2 constant = " << constant << endl;
}

Makefile:

CFLAGS = -Wall -g
INCLUDE = file.h
src = file2.cpp file1.cpp

all: $(src) $(INCLUDE)
  g++ $(CFLAGS) -o file $(src)

clean:
  rm -f file

When I do make, i get the following error message:

g++ -Wall -g -o file file2.cpp file1.cpp
/tmp/ccdl16Tw.o:(.data+0x0): multiple definition of `constant'
/tmp/ccA3ZEHa.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

gcc version 4.8.2

UPDATE:

If I do

 char* const constant = "Magic";

Then make will give me this warning:

g++ -Wall -g -o file file2.cpp file1.cpp

In file included from file2.cpp:2:0:
file.h:3:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
 char* const constant = "Magic";
                        ^
In file included from file1.cpp:2:0:
file.h:3:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
 char* const constant = "Magic";
9
  • Did you try extern const char *constant; in the header, and e.g const char *constant = "Magic"; in one of the translation units? Globals are still different when used outside class/struct declaratons. Apr 4, 2015 at 19:05
  • @πάνταῥεῖ I think he means that the const should have internal linkage, so the linker should not complain about duplicate symbols.
    – vsoftco
    Apr 4, 2015 at 19:08
  • @vsoftco "I think he means that the const should have internal linkage", which is obviously wrong, unless specified along a struct or class also static has it's meanings in c++, that differ from c. Apr 4, 2015 at 19:11
  • @πάνταῥεῖ, I agree with vsoftco. This is my understanding from the text in the book. BTW, I forgot to call file2 in my main(), updated.
    – drdot
    Apr 4, 2015 at 19:12
  • @πάνταῥεῖ no, I don't think it's wrong. const seems to have internal linkage by default (no need for static), I don't have the standard in front of me, but I tried OP's program with a const constant = 10; and the linker doesn't complain about duplicates anymore. The problem here is that the pointer is not const, as pointed by @user2079303 answer
    – vsoftco
    Apr 4, 2015 at 19:13

1 Answer 1

14

const char *constant is not const. It is a non-const pointer to const char. Being non-const variable in namespace scope, it has external linkage by default and thus you get multiple definition error.

const char * const constant is const and would behave as you are expecting.

Update:

char* const constant on the other hand would be a const pointer to char. Being const it does have internal linkage by default in namespace scope.

However, you shouldn't initialize it with a string literal (as the compiler warning points out) because that's not allowed by the standard (such conversion is illegal in c++11 and was deprecated before that). String literals are allowed to be stored in read only memory and you aren't allowed to modify them runtime. That is why pointing to a string literal with a pointer to non-const char is dangerous.

9
  • Also, It's worth noting that const qualifies whatever is on its left, except when used as the first qualifier, so the original definition is the same as char const * constant - the char is const, but not the pointer. Apr 4, 2015 at 19:17
  • in const char* const constant, the first const tells that the char pointed by the pointer is constant, and the second const tells that the pointer is pointing at constant address, correct? What does it mean if I have char* const constant = "Magic"? I have updated the behavior I get from my program. I will take your answer, but if you can answer this one extra question, that is really helpful!
    – drdot
    Apr 4, 2015 at 19:22
  • char* const constant = "Magic" means the pointer is const (i.e., you cannot reassign the pointer to another C-style string/string literal) and points to char. Furthermore, C++ deprecates using a pointer-to-char to point to a string literal, it has to be a pointer to const char. In other words, char* const constant = "Magic" produces warning: deprecated conversion from string constant to 'char*'.
    – vsoftco
    Apr 4, 2015 at 19:26
  • @vsoftco, that explains a lot! Just a note: my compiler gives a warning instead of an error when I use char* const constant = "Magic". but i agree it is not a good programming practice
    – drdot
    Apr 4, 2015 at 19:30
  • @dannycrane yes, indeed, edited my comment after I realized it also.
    – vsoftco
    Apr 4, 2015 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.