Does the linker care about namespaces?


  • Qt Champions 2018

    Noob question.
    If a class/function of a library (static or dynamic) is declared (in a header file that will form part of the "includes") with something like:

    #ifdef USE_NAMESPACE
    namespace MyNameSpace{
    #endif
    /*exported function/class*/
    #ifdef USE_NAMESPACE
    }
    #endif
    

    and USE_NAMESPACE is not defined when I compile the library. Can I then add USE_NAMESPACE to the definition of an application that links to that library?



  • @VRonin

    I am probably more Noob than you are.

    So far I have rated namespace similar as class and struct for compilation and linking. This is meant in view of naming in the objects with C++ compilers. When the namespace adds something to the whole name in the object, you have to use it always the same everywhere.

    You can do :

    void foo ()
    {
         do something;
    }
    
    namespace myspace {
        void foo ()
        {
            do something else;
        }
    }
    
    void rou() 
    {
        ::foo();
        myspace::foo();
    }
    

    both routines do different thing and compiler and linker must distinguish by some naming convention.



  • @koahnig
    @VRonin is asking about how/whether he can access classes/methods in a library from another program he/someone is writing, and be able to do so using different/no namespaces. Your example is to do with how namespaces must be consistent within one compilation & link.



  • @VRonin said in Does the linker care about namespaces?:

    and USE_NAMESPACE is not defined when I compile the library. Can I then add USE_NAMESPACE to the definition of an application that links to that library?

    @JonB
    He plans to use it inconsistently, isn't he?


  • Qt Champions 2017

    @VRonin said in Does the linker care about namespaces?:

    Can I then add USE_NAMESPACE to the definition of an application that links to that library?

    Nope. The linker very much cares about namespaces, to answer your title. The namespace identifier will be part of the fully qualified name of the symbol, so you need to link with code compiled with a compatible header.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.