Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Does the linker care about namespaces?
Forum Updated to NodeBB v4.3 + New Features

Does the linker care about namespaces?

Scheduled Pinned Locked Moved Solved C++ Gurus
5 Posts 4 Posters 1.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on last edited by VRonin
    #1

    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?

    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
    ~Napoleon Bonaparte

    On a crusade to banish setIndexWidget() from the holy land of Qt

    K kshegunovK 3 Replies Last reply
    0
    • VRoninV VRonin

      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?

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @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.

      Vote the answer(s) that helped you to solve your issue(s)

      JonBJ 1 Reply Last reply
      4
      • K koahnig

        @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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0
        • VRoninV VRonin

          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?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @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?

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • VRoninV VRonin

            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?

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @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.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            6

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved