Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Linking in a static library
Forum Updated to NodeBB v4.3 + New Features

[Solved] Linking in a static library

Scheduled Pinned Locked Moved General and Desktop
13 Posts 3 Posters 32.4k Views 2 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.
  • A Offline
    A Offline
    andre
    wrote on 17 Aug 2012, 10:19 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      franku
      wrote on 17 Aug 2012, 14:14 last edited by
      #2

      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.

      This, Jen, is the internet.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franku
        wrote on 17 Aug 2012, 14:17 last edited by
        #3

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

        This, Jen, is the internet.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 17 Aug 2012, 14:24 last edited by
          #4

          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?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 17 Aug 2012, 14:38 last edited by
            #5

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

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franku
              wrote on 17 Aug 2012, 14:39 last edited by
              #6

              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.

              This, Jen, is the internet.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                franku
                wrote on 17 Aug 2012, 14:39 last edited by
                #7

                Sorry, this was a typo. I ment -static

                This, Jen, is the internet.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on 17 Aug 2012, 14:55 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    franku
                    wrote on 17 Aug 2012, 15:49 last edited by
                    #9

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

                    This, Jen, is the internet.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      franku
                      wrote on 17 Aug 2012, 15:54 last edited by
                      #10

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

                      Uups, linked. With icecream in one hand.

                      This, Jen, is the internet.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on 23 Aug 2012, 10:06 last edited by
                        #11

                        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!

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          franku
                          wrote on 23 Aug 2012, 19:52 last edited by
                          #12

                          Glad having helped to [solve]
                          :-)

                          This, Jen, is the internet.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            kumararajas
                            wrote on 30 Dec 2015, 07:27 last edited by
                            #13

                            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

                            --Kumar

                            1 Reply Last reply
                            0

                            • Login

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