Namespaces

This is a discussion on Namespaces within the C Programming forums, part of the General Programming Boards category; Is it possible in some way to have separate namespaces between separate C files and have only those declarations that ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    42

    Namespaces

    Is it possible in some way to have separate namespaces between separate C files and have only those declarations that are in the header files be "public". For instance, the following example is how I would like it to work (but it doesn't).
    Code:
    /* foo.h */
    
    void say_hello(void);
    Code:
    /* foo.c */
    
    #include <stdio.h>
    #include "foo.h"
    
    void print_hello(void);
    void print_world(void);
    
    void say_hello(void)
    {
    	print_hello();
    	print_world();
    }
    
    void print_hello(void)
    {
    	printf("hello");
    }
    
    void print_world(void)
    {
    	puts(" world");
    }
    Code:
    /* main.c */
    
    #include <stdio.h>
    #include "foo.h"
    
    /* print_hello and print_world have not been
       declared as far as main.c can see */
    
    int main(void)
    {
    	say_hello();
    	return 0;
    }
    In main the functions print_hello and print_world cannot be used because they have not been declared. But main also cannot define functions of the same name of its own without raising the error "multiple definition" (when main.c and foo.c and compiled together, which they have to be, right?).

    If anyone else has some ideas for how this might be accomplished please let me know. Or am I just stuck with a global namespace?
    Last edited by the pooper; 01-21-2005 at 05:38 AM.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The static keyword causes a function or global variable to be only visible in the current translation unit.

    Code:
    /* static function can only be called by name in this translation unit. */
    static void print_hello(void)
    {
    	printf("hello");
    }
    Static functions

    Static functions can only be accessed from other translation units via function pointers.
    Last edited by anonytmouse; 01-21-2005 at 06:16 AM.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    main.c shouldn't be able to call print_hello() or print_world().
    i dont see what the problem is.

    [edit]I just re read your post. If you create another print_hello() or print_world() function in main.c, the linker still has to put it all together. Since they have the same names the linker will spit it back at you.
    Last edited by sand_man; 01-21-2005 at 06:33 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Since they have the same names the linker will spit it back at you.
    Yes. The idea was to figure out how to make it not spit it back at me, which anonytmouse answered for me. Thank you.

    anonytmouse, please check me to make sure I'm understanding storage class specifiers correctly.

    auto -> no linkage
    static -> internal linkage
    extern -> external linkage

    Also, when would it be appropriate to use the keyword auto? I can't think of a situation that would require it.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    auto -> external linkage

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This page on storage class specifiers provides an overview.

    auto is the default storage specifier for a block level variable. It has the scope and lifetime of the current block. Because it is the default, there is no reason to explicitly use the auto keyword. I've never used it. An auto variable has no linkage.
    Code:
    void demo(void)
    {
       int i; /* An automatic variable. */
       auto int j; /* Another automatic variable. */
    }
    The static keyword can be confusing because it has slightly different meanings depending on whether it is used at file level or in a function. When used at file level it means the variable or function has internal (to the current translation unit) linkage. When used in a function, it means a variable has a global lifetime, but, like an auto, no linkage.

    The extern keyword means a function or variable has external linkage. This is the default for functions and variable declared at file level. Although rare, it can also be used inside a function to declare an external variable that only has the scope of the current block.
    Code:
    /* Declares an int with external linkage. Definition is elsewhere. */
    extern int i;
    
    /* Declaration and definition of a variable with external linkage. */
    int j;
    
    void demo(void)
    {
        /* Same as i, except that k is only in scope in this block. */
        extern int k;
    }
    If I've got something wrong, I'm sure someone will provide corrections.
    Last edited by anonytmouse; 01-21-2005 at 07:59 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    auto -> external linkage
    If that's true then a couple things confuse me.

    First is that both C89 and C99 state that "an object declared with no linkage and without the storage-class specifier static has automatic storage duration."

    Also, the following example code
    Code:
    /* foo.c */
    
    int i = 10;
    Code:
    /* main.c */
    
    #include <stdio.h>
    
    int main(void)
    {
    	extern int i;
    	printf("%d\n", i);
    	return 0;
    }
    In this case, the int i in main obviously has external linkage. And as a result the output is 10. However,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	auto int i;
    	printf("%d\n", i);
    	return 0;
    }
    will give the warning main.c:7: warning: `i' might be used uninitialized in this function. And if I compile this without warnings and run it then the output is some random value.

    I don't see how auto has external linkage.
    Last edited by the pooper; 01-21-2005 at 08:03 AM.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I was thinking auto as in automatic since you were talking about functions. I wasn't thinking of the auto keyword for variables

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    42
    Thanks anonytmouse. You've been a big help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help wiht using classes...namespaces...
    By hockey97 in forum C++ Programming
    Replies: 9
    Last Post: 09-02-2008, 02:22 PM
  2. namespaces
    By Ancient Dragon in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2005, 06:04 PM
  3. Are namespaces a good idea?
    By Crilston in forum Game Programming
    Replies: 6
    Last Post: 06-24-2005, 06:30 PM
  4. C and namespaces
    By oyse in forum C Programming
    Replies: 2
    Last Post: 05-05-2004, 09:39 AM
  5. namespaces and friends
    By okinrus in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2004, 07:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21