Thread: Understanding Linkage

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Understanding Linkage

    The following is my self-adopted policy on each translation unit not implementing a class:
    Code:
    #include /*...*/
     
    /* -- declarations ---------- */
     
    void f(); 
    void b();
     
    namespace {
      void foo();
      void bar();
    }
     
    extern void faz();
    extern void baz();
     
    /* -- implementation -------- */
     
    void f() {/*...*/}
    void b() {/*...*/}
     
    namespace {
      void foo() {/*...*/}
      int bar() {/*...*/}
    }
     
    /* -- EOF ------------------- */
    - f() and b() are entry points to this unit. They are called by some other unit(s) and defined here.
    - foo() and bar() are only called within this unit (possibly from f() or b()).
    - faz() and baz() are called in this unit, but defined elsewhere.

    This is basically how I structure most of my files. Every function is declared and I make sure everything that can be declared local to the unit, is made so. However I have a few questions:

    - If, for some reason, I want to move those declarations to a header file I will have to keep the unnamed namespace in the header file, otherwise the linker will complain. However, I shouldn't be using unnamed namespaces in header files. Is it ok to keep an unnamed namespace in a header file when I know that header file is not going to be included by any other module?

    - As I understand, my usage of the extern keyword above is unnecessary. Functions have external linkage by default. However I find it has a documenting quality to it. At just a glance I know those two functions aren't defined in this unit. Is there any downside to this approach, or can I keep it?

    - I'm being told some modern(!) debugers depend tremendously on external symbols. I don't see that happening so far on my implementation (mingw with gdb) when I replace the unnamed namespaces by the deprecated static declarations. But still, do unnamed namespaces circumvent this problem on debugers that may experience it?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Mario F. View Post
    - If, for some reason, I want to move those declarations to a header file I will have to keep the unnamed namespace in the header file, otherwise the linker will complain. However, I shouldn't be using unnamed namespaces in header files. Is it ok to keep an unnamed namespace in a header file when I know that header file is not going to be included by any other module?
    I am always wary of "I know that header file is not going to be included" kind of statements. You never know what will happen in the future. why can't you just leave the unnamed namespace declarations in the source file? If they are local to a particular translation unit, why bother exposing them at all?
    Quote Originally Posted by Mario F. View Post
    - As I understand, my usage of the extern keyword above is unnecessary. Functions have external linkage by default. However I find it has a documenting quality to it. At just a glance I know those two functions aren't defined in this unit. Is there any downside to this approach, or can I keep it?
    There isn't any downside (apart from 7 extra keystrokes ) that I can see.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by Mario F. View Post
    - faz() and baz() are called in this unit, but defined elsewhere.
    This part is a bad idea. There should be a header associated with the "elsewhere" which contains the symbols meant for external use. Include this header and forget about the private declarations.

    This also gets rid of the case you're documenting with the redundant extern keyword.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Okies. Thanks for the input. Will do as suggested; as I understand it, external declarations going into a header file and local ones staying with the cpp file.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linkage question
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2009, 07:56 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM