Thread: How to share a private function between multiple .c files?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    How to share a private function between multiple .c files?

    Hi,
    I started making a large project in C, and I realized that I need some useful functions that I made in multiple .c files, but, I don't want the user of my files to be able to use those functions (or accidentally declare a function with the same name, and then use it)
    I understand that if I want a function to be used in only one .c file, then I just make it static. So what do I need to do so that I can select which files can use my function, and which can't?
    I understand that putting the static function in a header file wouldn't be good, because then the compiler would make a new copy of that function in each file that includes it.
    Thanks for the help!
    P.S. In c++ this would be done with namespaces right?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Any C project bigger than the simple training exercises people bring here is going to include multiple source files (.c). You communicate variables and functions across those files using headers (.h) with function prototypes and extern variable names. The compiler/linker does not make multiple copies of each function it makes one copy then converts the prototypes in the headers to the address of the function. It will only be in your executable code once. (Unless you are using some monstrously deformed compiler from the Flintstone days)

    Making functions static is pointless... they are static by default since they exist as a fixed entity within your program.

    Here's how you do this in C....

    main.c...
    Code:
    #include "flub.h"
    
    int main (void)
      { int a = 4, b = 3,c = 2, d;
    
          d = flubstuff(a,b,c);
    
         return 0; }
    flub.c...
    Code:
    // function to flub stuff
    int flubstuff( int x, int y, int z)
      { return (x + y) * z; }
    flub.h ....
    Code:
    #ifndef FLUB_H
    #define FLUB_H
    
    int flubstuff(int x, int y, int z);    //--- note the semicolon here!
    
    #endif //FLUB_H
    So your header file is actually descrbing the function (i.e. prototyping it) and making a promise to the compiler that flubstuff() exists someplace where the linker can find it.
    Last edited by CommonTater; 04-17-2011 at 10:02 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by purestr999
    So what do I need to do so that I can select which files can use my function, and which can't?
    Establish a naming convention, e.g., prepend "private_" or append an underscore.

    Quote Originally Posted by purestr999
    In c++ this would be done with namespaces right?
    Not quite: it would be done by making a member function private. If it is impossible or undesirable for the function to be a private member function, then convention would be used, e.g., a detail namespace. An anonymous namespace could also be used, but it could be akin to declaring the function static.

    Quote Originally Posted by CommonTater
    Making functions static is pointless... they are static by default since they exist as a fixed entity within your program.
    No, a function normally has external linkage, but if it is declared static, then it has internal linkage, hence purestr999's correct observations that "if I want a function to be used in only one .c file, then I just make it static" and "that putting the static function in a header file wouldn't be good, because then the compiler would make a new copy of that function in each file that includes it".
    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

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Hi, thanks for the help!
    @CommonTater - The problem with the example you wrote is that if you used those files inside a bigger project, and someone else wrote his own function called flubstuff, then the compiler/linker would complain and say there were 2 declarations of the same function, because your flubstuff can be used by anyone that just writes its declaration.

    @laserlight - So the only way to make a function "private" for a few .c files is to just add something weird to the name like "private_"? If you had a gigantic project like a operating system or something, all the names would start getting used up :S
    Is there any better way of doing it?

    Not quite: it would be done by making a member function private. If it is impossible or undesirable for the function to be a private member function, then convention would be used, e.g., a detail namespace. An anonymous namespace could also be used, but it could be akin to declaring the function static.
    I know about private functions inside a class, but how do you make a function only seen by 2-3 source files? Is there some way to make it "private" to them? (I just started C++)

    Thanks again for all the help

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by purestr999 View Post
    Hi, thanks for the help!
    @CommonTater - The problem with the example you wrote is that if you used those files inside a bigger project, and someone else wrote his own function called flubstuff, then the compiler/linker would complain and say there were 2 declarations of the same function, because your flubstuff can be used by anyone that just writes its declaration.
    C has no feature to prevent this. This is why people working together have to actually communicate with one another, plan their projects and work this kind of thing out in advance. If you are on the group working on the user interface, you can preface your functions with ui_ the group working on filesystems could use fs_ and so on... You don't just drop 40 people in a room and say "write this...".

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    No, a function normally has external linkage, but if it is declared static, then it has internal linkage, hence purestr999's correct observations that "if I want a function to be used in only one .c file, then I just make it static" and "that putting the static function in a header file wouldn't be good, because then the compiler would make a new copy of that function in each file that includes it".
    Hmmm... live and learn. Thanks Lase...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-09-2008, 02:34 AM
  2. Replies: 16
    Last Post: 03-08-2008, 05:42 AM
  3. Share files between computers
    By Raihana in forum Networking/Device Communication
    Replies: 3
    Last Post: 12-08-2003, 08:05 AM
  4. Can multiple linked lists share the same structure?
    By passy in forum C Programming
    Replies: 10
    Last Post: 08-28-2003, 04:38 PM
  5. How to Access a Function from Multiple Files?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2002, 06:24 PM