Thread: Am I missing something with const?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    127

    Am I missing something with const?

    Is there a case where const is supposed to act like static? I've defined a global constant in one file and try to extern it in another and my linker fails. If I remove the const keyword, my program links.

    Here is an example of the code that fails to link

    File 1
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      extern const int my_const;
      
      printf("hello %d\n", my_const);
      return 0;
    }
    File 2
    Code:
    const int my_const = 7;
    Simply removing the const keyword from both files allows me to link.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Compiles, links, and runs fine for me in gcc. What are you using to build?

    Code:
    gcc -Wall -pedantic -o file1 file1.c file2.c
    Code:
    $ ./file1
    hello 7

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by homer_3 View Post
    Is there a case where const is supposed to act like static?
    "static" has different meanings.

    Anything that you declare globally as const is static by default (has internal linkage, which means that other translation units can't see it). Put your "my_const" in a header file and do not define it in cpp file.
    Code:
    #ifndef HEADER_HPP
    #define HEADER_HPP
    const int my_const = 7; // no "static" or "extern" here
    #endif
    Anything that you declare locally isn't static. Static here has a different meaning. It means that you will have one variable shared between all calls to the same function. So if you want my_const to be visible only in main(), put:
    Code:
    int main(void)
    {
        static const int my_const = 7; // no "extern" here
    }
    Quote Originally Posted by homer_3 View Post
    Simply removing the const keyword from both files allows me to link.
    Removing "const" made "my_const" to have external linkage in the second file.
    Last edited by kmdv; 04-03-2013 at 12:44 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well C and C++ have some different rules regarding this: Linxutopia - Thinking in C++ - 3: The C in C++ - Linkage. And in C++ there is a whole other feature, extern "C" that you are not using as well, just to make a note of it.

    I created an example for you. Typically external linkage works like this, especially if you want the same global constant to be referenced around the program. The only way the linker will be able to resolve all of the references to the same variable is if there is a note about external linkage when the variable is actually defined. Where it is otherwise used, an extern declaration is fine.

    foo.h contains:
    Code:
    #ifndef FOO_H_INCLUDED
    #define FOO_H_INCLUDED
    
    extern const int my_const;
    
    void foo();
    
    #endif
    file1.cpp contains:
    Code:
    #include <stdio.h>
    #include "foo.h"
    
    const int my_const = 7;
    
    int main()
    {
        printf("hello %d\n", my_const);
        foo();
        return 0;
    }
    file2.cpp contains:
    Code:
    #include <stdio.h>
    #include "foo.h"
    
    void foo()
    {
        printf("from foo(), %d\n", my_const);
    }
    And with this you can clearly tell we are referencing the same constant, defined in file1.cpp. It could also be defined in file2.cpp if that makes more logical sense, there is no technical difference.

    static, as it relates to the topic, refers to static linkage.
    Last edited by whiteflags; 04-03-2013 at 01:17 AM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Damn, I was totally thrown by the #include <stdio.h> and printf and compiled as C :/

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Thanks for the clarification. I Googled around for something like that Linxutopia link, but couldn't find anything.

    It seems quite confusing to me that const means static const, when used at global scope, but I guess that's how it is. I also didn't realize defining a variable with extern was legal in C++. I always thought it was only for declarations.

    @Whiteflags, that's how I would normally do it, since it's just easier to use in general. But I thought what I had should work, obviously I was wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Mutable members in const member functions still const
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2002, 08:56 AM