Thread: "External" linkage between some files but "internal" linkage to the outside world

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    10

    "External" linkage between some files but "internal" linkage to the outside world

    Let's say I have a file f1.c with the following:
    Code:
    int two(int a, int b) {
        return a + b;
    }
    And a file f1.h with the following:
    Code:
    int two(int, int);
    And a file f2.c with the following:
    Code:
    #include "f1.h"
    int one(int a, int b, int c) {
        return two(a, b) * c;
    }
    I only want to expose the one function, so the two function should be unlinkable from other files when f1.c and f2.c are compiled into e.g. f12.o (to avoid unexpected errors from happening when somebody who links my code to theirs also has a function named "two").

    However, if I make the definition in f1.c static, then I won't be able to use the two function in f2.c. Is there a portable way to achieve this effect without merging everything into one source file?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,310
    I'd say no: you do have to define two in the same translation unit as one in order for it to be static/have internal linkage and yet called by one. A compromise solution might be to make use of a name prefix, e.g., rename two to erikkonstas_internal_two, and then document that it is internal despite having external linkage so if anyone else tries using it, you may delete it or change its signature at will and the blame is entirely on them when their code breaks.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    10
    Hm, bummer. I was trying to write some test project which has a lot of functions and decided to split them in a few .c files but I guess I'll have to do either of these two. Thanks for answering, though!

    EDIT: Just thought of using a macro which prepends some keyboard mashing in front of the function's name so that if I have to change the prefix for some reason I only change it in one place...
    Last edited by erikkonstas; 04-14-2021 at 07:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. C++ .NET - "inconsistent dll linkage" - What's that?
    By BrianK in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2004, 10:16 AM
  3. Get sense of internal linkage and external linkage
    By gandalf_bar in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 05:57 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread