Thread: Marking functions as "Local" or "Global"

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    45

    Marking functions as "Local" or "Global"

    Consider that in a file named "priv.c", I have:
    Code:
    int multiply(int n, int m) { return n * m; }
    And in another file named "pub.c", I have:
    Code:
    int multiply(int n, int m);
    
    int square(int n) { return multiply(n, n); }
    Then I go ahead and build a static library using:

    cc -c priv.c pub.c
    ar rcs libsquare.a priv.o pub.o
    This library will contain definitions for both "square" and "multiply". I'm wondering how I might go about hiding the definition for "multiply" in "libsquare.a". Essentially, I'm looking for a way to categorise some functions as "local" and others as "global".

    I kindly welcome all thoughts and suggestions.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Using the keyword static would be a good choice.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    In my library the local functions are called in multiple files, so it seems I can't declare them with internal linkage, lest the library were implemented by one gigantic file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Use a naming convention for the private header such that it is clear that the functions declared within (perhaps with their own naming convention) are implementation detail despite having external linkage.
    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

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    I'll go with that then. Thank you both for taking the time to respond.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I don't think I'd make a function int multiply(int n, int m). Simply use C's multiplication operator.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Hodor View Post
    I don't think I'd make a function int multiply(int n, int m). Simply use C's multiplication operator.
    I suggest the same comment applies to the square() function.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by grumpy View Post
    I suggest the same comment applies to the square() function.
    Perhaps this is better?

    Code:
    #include <stdio.h>
    
    #define MNAME dafilename
    #define MCAT_(a,b) a##b
    #define MCAT(a,b) MCAT_(a,b)
    #define LOCALFUNC(fn) MCAT(MNAME, fn) 
    #define CALLLOCALFUNC(fn) MCAT(MNAME, fn)
    
    static int LOCALFUNC(multiply(int x, int y))
    {
        return x * y;
    }
    
    int main (void)
    {
        printf("5*5==%d\n", CALLLOCALFUNC(multiply(5, 5)));
    
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sorry, I have a furball in my throat, caused by reading that code.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Before putting priv.o and pub.o into the library, you need to perform an incremental link to produce a single combined .o file.
    At this time, you also use the --retain-symbols-file option to list only those symbols you want to keep.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    45
    The multiply and square functions are inconsequential to my post, Hodor. Any other two functions, with one implemented in terms of the other, could have sufficiently illustrated the same case.

    Thanks, Salem. I assumed there had to be an option like that, but it seems I was looking for it in the wrong place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-12-2012, 06:17 AM
  2. Replies: 2
    Last Post: 08-19-2012, 06:15 AM
  3. "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
  4. "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