Results 1 to 4 of 4

Thread: Exporting function under a namespace always lead to unresolve reference when linking

  1. #1
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default Exporting function under a namespace always lead to unresolve reference when linking

    In a shared lib, I have the following:
    Qt Code:
    1. #ifndef SHAREDLIB_H
    2. #define SHAREDLIB_H
    3. #include "SharedLib_global.h"
    4.  
    5. namespace Fx{
    6. SHAREDLIBSHARED_EXPORT int FunctionUnderNamespace();
    7. }
    8.  
    9. SHAREDLIBSHARED_EXPORT int FunctionOutsideNamespace();
    10. #endif // SHAREDLIB_H
    To copy to clipboard, switch view to plain text mode 

    This will compile without problem, but in the app source file:
    Qt Code:
    1. #include <SharedLib.h>
    2. #include <QDebug>
    3. int main()
    4. {
    5. //int v = Fx::FunctionUnderNamespace(); // link error
    6. int v = FunctionOutsideNamespace(); // works fine
    7. qDebug() << v;
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Calling the function under a namespace will not even be compiled.
    Do I actually need to export the namespace?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,329
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Exporting function under a namespace always lead to unresolve reference when link

    Your SHAREDLIBSHARED_EXPORT macro is probably not including the namespace when it declares the function for export. The linker is looking for Fx::FunctionUnderNamespace() and your macro apparently is exporting the global function ::FunctionUnderNamespace() instead.

    Try doing this instead:

    Qt Code:
    1. SHAREDLIBSHARED_EXPORT int Fx::FunctionUnderNamespace();
    To copy to clipboard, switch view to plain text mode 

    and get rid of the namespace Fx {} scope declaration that wraps the export declaration.

  3. The following user says thank you to d_stranz for this useful post:

    grayfox (6th July 2011)

  4. #3
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default Re: Exporting function under a namespace always lead to unresolve reference when link

    Quote Originally Posted by d_stranz View Post
    Your SHAREDLIBSHARED_EXPORT macro is probably not including the namespace when it declares the function for export. The linker is looking for Fx::FunctionUnderNamespace() and your macro apparently is exporting the global function ::FunctionUnderNamespace() instead.

    Try doing this instead:

    Qt Code:
    1. SHAREDLIBSHARED_EXPORT int Fx::FunctionUnderNamespace();
    To copy to clipboard, switch view to plain text mode 

    and get rid of the namespace Fx {} scope declaration that wraps the export declaration.
    I tried your suggestion but the shared lib won't even compile.

  5. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Exporting function under a namespace always lead to unresolve reference when link

    link error means that there is no definition of problematic function!
    The main question is how did you define this symbol in dll? (I assume that macro SHAREDLIBSHARED_EXPORT is properly handled).
    As a safety check use this function in your dll using proper namespace.

Similar Threads

  1. QTableWidgetItem text() function not linking ?
    By marcvanriet in forum Newbie
    Replies: 4
    Last Post: 10th September 2010, 15:45
  2. Replies: 4
    Last Post: 14th October 2009, 04:28
  3. Function arguments - how to get a reference
    By olidem in forum Qt Programming
    Replies: 2
    Last Post: 3rd March 2009, 10:31
  4. Linking error: libQtNetwork.so: undefined reference to `_freeifaddrs'
    By dacla in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 21st April 2008, 22:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.