"Private to subproject" functions and variables

Discussion in 'C Programming' started by metiu, Sep 19, 2006.

  1. metiu

    metiu Guest

    Hi all,
    maybe it's an old question, but I couldn't find an answer...

    let's say I have this kind of directory structure for project foo:

    /foo/includes/mod1.h (exported by mod1)
    /foo/includes/mod2.h (exported by mod2)

    /foo/mod1/file1.c
    /foo/mod1/file1.h (exported by file1)
    /foo/mod1/file2.c
    /foo/mod1/file2.h (exported by file2)

    /foo/mod2/file1.c
    /foo/mod2/file1.h (exported by file1)
    /foo/mod2/file2.c
    /foo/mod2/file2.h (exported by file2)

    ...... I think you got the picture

    How can I make function "bar()" exported by /foo/mod1/file1.c only to
    other files in /foo/mod1, without making it visible to mod2?

    Just omitting it from mod1.h won't work: someone could declare it
    extern in his/her file and use it. I'd like to make it foolproof.

    One solution that comes up to me is making a mod1.c file which is
    #include "file1.c"
    #include "file2.c"

    and using static functions/variables.

    Is there a more elegant way?

    Thanks
    metiu, Sep 19, 2006
    #1
    1. Advertising

  2. metiu

    Jack Klein Guest

    On 19 Sep 2006 05:07:25 -0700, "metiu" <> wrote
    in comp.lang.c:

    > Hi all,
    > maybe it's an old question, but I couldn't find an answer...
    >
    > let's say I have this kind of directory structure for project foo:
    >
    > /foo/includes/mod1.h (exported by mod1)
    > /foo/includes/mod2.h (exported by mod2)
    >
    > /foo/mod1/file1.c
    > /foo/mod1/file1.h (exported by file1)
    > /foo/mod1/file2.c
    > /foo/mod1/file2.h (exported by file2)
    >
    > /foo/mod2/file1.c
    > /foo/mod2/file1.h (exported by file1)
    > /foo/mod2/file2.c
    > /foo/mod2/file2.h (exported by file2)
    >
    > ..... I think you got the picture
    >
    > How can I make function "bar()" exported by /foo/mod1/file1.c only to
    > other files in /foo/mod1, without making it visible to mod2?
    >
    > Just omitting it from mod1.h won't work: someone could declare it
    > extern in his/her file and use it. I'd like to make it foolproof.
    >
    > One solution that comes up to me is making a mod1.c file which is
    > #include "file1.c"
    > #include "file2.c"
    >
    > and using static functions/variables.
    >
    > Is there a more elegant way?
    >
    > Thanks


    There is absolutely no way to prevent deliberate abuse, and indeed
    that is not the purpose of the C language. But there is a way to
    prevent accidental clashes, if that is what you are trying to do.

    Simply use name spaces. What are name spaces in C? A very simple
    concept. Everything in a "module" (or "task", or whatever you choose
    to call your code breakdown) that has external linkage starts with a
    prefix derived from the name of the module (task, whatever).

    So all the functions, external objects, and types declared or defined
    in mod1.h start with a specific prefix, such as m1_.

    struct m1_data_t
    {
    /* members */
    };

    enum
    {
    M1_OK,
    M1_MEMORY_ERROR,
    M1_FILE_NOT_FOUND,
    /* etc */
    } m1_result_t;

    m1_result_t m1_regurtitate_data(int index,
    struct m1_data_t *data_ptr);

    ....and so on.

    And of course everything in mod2.h is prefixed with m2_, or mod2_, or
    whatever.

    As for symbols with external linkage within the source files of a
    module itself, they follow exactly the same naming convention, and are
    in headers used only within the source files of the module itself.

    On the other hand, if you are looking for a way to prevent a
    programmer writing code for mod2 from looking into an internal header
    for mod1 that declares:

    extern struct m1_data_t *m1_head;

    ....and copying the declaration to his own file so he can access
    m1_head directly, C cannot prevent that.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Sep 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  2. Adel
    Replies:
    3
    Views:
    307
    Jack Klein
    Mar 17, 2005
  3. John Goche
    Replies:
    10
    Views:
    740
    Marcus Kwok
    Dec 8, 2006
  4. Replies:
    9
    Views:
    936
  5. mafiQ
    Replies:
    0
    Views:
    471
    mafiQ
    Apr 22, 2009
Loading...

Share This Page