Thread: C and namespaces

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    2

    C and namespaces

    I can't remember if I posted this question earlier so I'm sorry if this gets posted twice.

    I was wondering about C and namespaces. If I create a function:

    int findIndex( int i );

    in a.h and in some other file b.h excatly the same signature is used. The function is implemented in a.c and b.c respectively. Either directly or indirectly file a.h includes b.h. Now I have two identical signatures each one refering to a different implentation of the function int findIndex( int i ). In the file a.c I use the funtion findIndex. How does the compiler/linker now know which implentation I refere to? Is there anyway to avoid this problem, if it is a problem? In larger programs it seems to me that it would be impossible to know the names of all declared functions and some function names would proably be used twice. Is this assumption correct?

    I have tried testing this with gcc by declaring and implementing a dummy function strlen with excactly the same signature as the strlen function defined in <string.h>. Suprisingly(?) the program changed which function it used depending on where I implemented the dummy function. If implemented the dummy function before I called it, it used the dummy function. If I implemented it after the call the real strlen was used.

    oyse

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I was wondering about C and namespaces.
    C doesn't have namespaces - at least not in the way C++ has them

    > How does the compiler/linker now know which implentation I refere to?
    It doesn't, so what you get is a "multiply defined symbol" error.

    > Is there anyway to avoid this problem, if it is a problem?
    Choose better names

    > Is this assumption correct?
    Well you can use scope to some extent, by using the static keyword
    Code:
    static int findIndex( int i );
    Now findIndex() cannot be seen outside the source file in which is it declared, so you could have both of them static in a.c and b.c, and all would be well.

    > In larger programs it seems to me that it would be impossible to know the names of
    > all declared functions
    True - but the usual way round that is to adopt a more rigorous naming convention. This is one possible way of doing this.

    If you have two modules foo.c and bar.c (and respective .h files)
    All global symbols would be like
    int Foo_Init ( void );
    void Bar_Count ( int num );

    All local symbols would be like
    static foo_Count ( int num );

    You prefix the symbol name with the name of the module, and you use case to denote scope.

    > I have tried testing this with gcc by declaring and implementing a dummy function strlen
    There's a lot going on here
    1. gcc can inline certain standard library functions
    To disable this, you need to compile with -fno-builtin
    2. strlen is also a library function, which means if you declare yours first, the one in the library will be quietly ignored.
    Libraries of symbols are searched by the linker for matches to unresolved symbols. It is not an error to find a symbol in a library which matches a symbol already linked.
    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.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    2
    Thanks for a good and clear answer. It was just what I was looking for.

    oyse

Popular pages Recent additions subscribe to a feed