[Solved] Linking in a static library



  • I have no clue what goes wrong here. I am adding a new (static) lib to an existing project, that already links to some static libs. Everything builds without any errors or warnings. However, when I try to run the application, it immediately bails out with this error:
    [quote]The program has unexpectedly finished.
    C:\Development\cassini\Build\debug\Cassinid.exe exited with code -1073741515[/quote]

    When inspecting the .exe file using Dependency Walker, I immediately spot a missing dependency: Widgets.dll
    Widgets is the name of the library I am trying to add. The library is added to the cassini.pro file using this:
    @
    CONFIG( Debug ) {
    LIBS += ../Libraries/Widgets/debug/libWidgets.a
    PRE_TARGETDEPS += ../Libraries/Widgets/debug/libWidgets.a
    } else {
    LIBS += ../Libraries/Widgets/release/libWidgets.a
    PRE_TARGETDEPS += ../Libraries/Widgets/release/libWidgets.a
    }

    INCLUDEPATH += ../Libraries/Widgets
    @

    Though I also tried the standard (Qt Creator generated):
    @
    win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Libraries/Widgets/release/ -lWidgets
    else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Libraries/Widgets/debug/ -lWidgets
    else:symbian: LIBS += -lWidgets
    else:unix: LIBS += -L$$OUT_PWD/../Libraries/Widgets/ -lWidgets

    INCLUDEPATH += $$PWD/../Libraries/Widgets
    DEPENDPATH += $$PWD/../Libraries/Widgets

    win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Libraries/Widgets/release/libWidgets.a
    else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Libraries/Widgets/debug/libWidgets.a
    else:unix:!symbian: PRE_TARGETDEPS += $$OUT_PWD/../Libraries/Widgets/libWidgets.a
    @

    (though I had to change the last lines to refer to libWidgets.a instead of Widgets.lib)

    The result is the same though.

    The Widgets library itself is quite simple. The .pro file is just:
    @
    CONFIG += debug_and_release
    UI_DIR = build
    MOC_DIR = build
    RCC_DIR = build

    TEMPLATE = lib
    CONFIG += staticlib
    TARGET = Widgets

    QT = core gui

    win32 {
    CONFIG(release, release|debug) {
    DESTDIR = release/
    OBJECTS_DIR = release/
    }
    else {
    DESTDIR = debug/
    OBJECTS_DIR = debug/
    }
    }

    HEADERS +=
    dateparser.h
    datewidget.h

    SOURCES +=
    dateparser.cpp
    datewidget.cpp

    @

    Of course, I tried to completely clean my build try, re-run qmake to rebuild, but the issue remains, and Dependency Walker keeps identifying Widgets.dll as a (missing) dependency.

    Any tips on how to convince the toolchain that Widgets is a static library and needs to be linked in as such?



  • Hi Andre, open the libWidgets.a with a texteditor. If it is an import lib for a dll, then the dll name will be listet in it, otherwise not.

    Second use the -static command-line option to force "static linking ":http://pages.cs.wisc.edu/~thomas/X/static-linking.html of the lib. I remember dynamic linking is the default.



  • Other ideas, for the link-section try to use this:

    @
    CONFIG( Debug ) {
    LIBS += -shared -L../Libraries/Widgets/debug -lWidgets
    PRE_TARGETDEPS += ../Libraries/Widgets/debug/libWidgets.a
    } else {
    @



  • Hmmm... weird. It does contain Widgets_dll in several places. Does that mean it was build wrong?

    Adding -static to the LIBS variable did not help. Or did I misunderstand your second solution?



  • [quote author="franku" date="1345213035"]Other ideas, for the link-section try to use this:

    @
    CONFIG( Debug ) {
    LIBS += -shared -L../Libraries/Widgets/debug -lWidgets
    PRE_TARGETDEPS += ../Libraries/Widgets/debug/libWidgets.a
    } else {
    @[/quote]

    I don't understand this. I don't want a shared library, but a static one.



  • Ok. If the libwidgets.a lib was built as static lib, then it should not contain references to any dll (I looked for it in some static lib i have for sure and there wasn't). If the -static solution does not static link then in my opinion the build of libwidgets.a went wrong. I do not know yet where or when static and dll libs are used in common.

    However, the order of -static and -lwidgets is important. You may have read in the linked comment above. But in my opinion, maybe I am false, the lib is built wrong.



  • Sorry, this was a typo. I ment -static



  • Thank you for your help so far.

    I think so you are right something goes wrong in the build, but I for the life of me can't figure out what it might me. I have pasted my "build output":http://pastebin.com/cmxjEV42 online. While inspecting the contents of the CONFIG variable, I did find a "shared" there. The documentation lists "dll" as the keyword that triggers building a shared library, but I tried removing it explicitly anyway. That did not help.

    I had already tried putting the -static in the LIBS (before the lib itself), without result.

    Other suggestions are welcome.



  • Ok, I will have a look in the build process. Maybe I find out what happens.
    rgds, .. frank



  • Do you have the output for your application being lonked, too?

    Uups, linked. With icecream in one hand.



  • Sorry for the absense.

    I managed to link properly! I noticed from the output that the ar tool was used, and that gave me a good feeling. I deleted the Makefiles and all object files from the libary, and the makefiles and produced binary from the application itself. I then build the library again, and no more .dll strings appeared in the result. Then, I build and linked the application, and it just worked.

    It seems that, even after cleaning, manually running qmake and doing a complete rebuild, I still had some stale stuff that messed up my build.

    Thanks for your help!



  • Glad having helped to [solve]
    :-)



  • Hi Andre,

    I am facing the similar problem.

    I am unable to link my static library to my application.

    My application is unable to see the functions inside the library.

    I get 'undefined reference to' when I access the function.

    Any thoughts on this?

    Thanks,
    Kumar


Log in to reply
 

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