Unreferenced Libraries?



  • Alright,

    I am having a lot of trouble correctly linking libraries to QT 4. I created a simple library called test_class and here is the code:

    test_class.cpp
    @
    #include "test_class.h"

    int test_me::whatever(void)
    {
    return 5;
    }
    @

    test_class.h
    @
    #ifndef TEST_CLASS_H
    #define TEST_CLASS_H

    class test_me
    {
    public:
    int whatever(void);
    };

    #endif
    @

    I then create that into a .lib and try to call it from my other program in this manner:
    @
    MainWindow.cpp

    test_me abc;
    abc.whatever();
    

    @

    I have the proper include in MainWindow.cpp and I have the library in my .pro file correct. Any ideas? I'm getting this as an error:

    Quoted
    
    mainwindow.cpp:26: error: undefined reference to `test_me::whatever()'
    :-1: error: collect2: ld returned 1 exit status


  • Have you added the library to LIBS? Is it properly listed in the linker command when compiling / linking the project?
    @
    LIBS += -L<path to library> -l<library name>
    @



  • Yes i have added the library to my .pro file
    @
    unix|win32: LIBS += -ltest_code -LC:\VS13_VIEWS\libs
    @

    just tried the syntax you posted and still no go



  • You are not exporting the class.
    If you are using Qt Creator, have a look at the library template.



  • [quote author="JediSpam" date="1313071868"]I then create that into a .lib[/quote]

    Are we talking about a static library or a shared library?



  • [quote author="Lukas Geyer" date="1313072764"][quote author="JediSpam" date="1313071868"]I then create that into a .lib[/quote]

    Are we talking about a static library or a shared library? If so you will have to export symbols as loladiro said.[/quote]

    this is a static library i have created in visual studio 2003. i'm trying to understand the library template stuff



  • You might use the nm tool to see which symbols are exported from your library.



  • [quote]
    this is a static library i have created in visual studio 2003.
    [/quote]
    Are you trying to mix MSVC and MinGW-compiled code? Not a good Idea!!



  • You talking about static library ?



  • [quote author="printingesco" date="1313075382"]You talking about static library ?[/quote]

    yes



  • [quote author="loladiro" date="1313072534"]You are not exporting the class.
    If you are using Qt Creator, have a look at the library template.[/quote]

    I read the qMake Project Files section "Declaring Other Libraries" and don't see what I haven't done correctly.

    [quote author="Lukas Geyer" date="1313072949"]You might use the nm tool to see which symbols are exported from your library.[/quote]

    Is this a QT tool I don't see it anywhere



  • Again, did you compile your library with mingw? It doesn't seem that way because mingw static library files have a .a extension. You cannot mix MSVC and MinGW.



  • [quote author="loladiro" date="1313076573"]Again, did you compile your library with mingw? It doesn't seem that way because mingw static library files have a .a extension. You cannot mix MSVC and MinGW.[/quote]

    ah i see. so i'd have to use the MinGW in visual studio to build a library.



  • You'd have to either build both the library and app with MinGW or both with MSVC.



  • [quote author="JediSpam" date="1313076335"]
    [quote author="Lukas Geyer" date="1313072949"]You might use the nm tool to see which symbols are exported from your library.[/quote]
    Is this a QT tool I don't see it anywhere
    [/quote]

    nm comes with MinGW, which is part of the Qt SDK. Just open a Qt command promt and type nm <binary> to get a list of exported symbols.

    (Although it comes with MinGW it should work with any PE - read MSVC - binaries).



  • Thanks for the help guys!


Log in to reply
 

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