1 From 7a912823158a4113256c3113a34c38d6b241d275 Mon Sep 17 00:00:00 2001
2 From: Alexey Brodkin <abrodkin@synopsys.com>
3 Date: Wed, 13 Jan 2016 20:15:36 +0300
4 Subject: [PATCH] Fix library inclusion order when building statically
6 When building application statically it's important to keep
7 libraries we're linking against in order. Otherwise if libA depends on
8 libB but it is mentioned after libB in linker command line
9 there will be unresolved symbols.
11 Consider real example - configuration of Qt with glib for static build.
12 Initially reported by Buildroot autobuilder here:
13 http://autobuild.buildroot.net/results/174/174c6e47eb761f9897275b6fedff742ace2f3081
17 [1] Qt's configuration script tries to build glib test app
18 (in config.tests/unix/glib)
20 [2] For that it first asks which libs to use during linkage this way:
21 QT_LIBS_GLIB=`$PKG_CONFIG --libs glib-2.0 gthread-2.0 2>/dev/null`
23 In our case we're getting something like this:
24 -L/.../sysroot/usr/lib -lintl -lgthread-2.0 -pthread -lglib-2.0 \
25 -lintl -pthread -lintl
27 Note "-lintl" is mentioned 3 times because libgthread depends on
28 libthread and both of them plus libglib all depend on libintl - so
29 we're getting "lintl" for each separate library mentioned above.
31 [3] Now we execute "compileTest" for real heavy lifting this way:
32 compileTest unix/glib "Glib" $QT_CFLAGS_GLIB $QT_LIBS_GLIB ...
34 [4] compileTest (the one for unix) parses command-line passed to it
35 groups all entries with "-l" prefix and puts them in LFLAGS
36 variable. And finally executes qmake passing it that kind of
38 $OUTDIR/bin/qmake ..."LIBS*=$LFLAGS"
40 [5] When qmake sees construction "MYVAR*=MYVAL" it populates MYVAR with
41 unique values from MYVAL string.
43 [6] As a result qmake generated Makefile with the following:
44 LIBS = $(SUBLIBS) -pthread -L/.../sysroot/usr/lib -lintl -lgthread-2.0 \
47 [7] And essentially on attempt to link glib test app a failure happens
48 because libglib needs libintl, i.e. "-lintl" must follow "-lglib-2.0":
49 -------------------->8------------------
52 g++ -static -Wl,-O1 -o glib glib.o -pthread -L/.../sysroot/usr/lib \
53 -lintl -lgthread-2.0 -lglib-2.0
55 /.../sysroot/usr/lib/libglib-2.0.a(libglib_2_0_la-ggettext.o): In function '_g_dgettext_should_translate':
56 ggettext.c:(.text+0x28): undefined reference to `libintl_textdomain'
57 ggettext.c:(.text+0x36): undefined reference to `libintl_gettext'
58 /.../sysroot/usr/lib/libglib-2.0.a(libglib_2_0_la-ggettext.o): In function `ensure_gettext_initialized':
59 ggettext.c:(.text+0xe6): undefined reference to `libintl_bindtextdomain'
60 ggettext.c:(.text+0xf6): undefined reference to `libintl_bind_textdomain_codeset'
61 /.../sysroot/usr/lib/libglib-2.0.a(libglib_2_0_la-ggettext.o): In function `g_dgettext':
62 ggettext.c:(.text+0x148): undefined reference to `libintl_dgettext'
63 /.../sysroot/usr/lib/libglib-2.0.a(libglib_2_0_la-ggettext.o): In function `g_dcgettext':
64 ggettext.c:(.text+0x2dc): undefined reference to `libintl_dcgettext'
65 /.../sysroot/usr/lib/libglib-2.0.a(libglib_2_0_la-ggettext.o): In function `g_dngettext':
66 ggettext.c:(.text+0x32a): undefined reference to `libintl_dngettext'
67 collect2: error: ld returned 1 exit status
68 Makefile:99: recipe for target 'glib' failed
69 make: *** [glib] Error 1
72 Glib support cannot be enabled due to functionality tests!
73 Turn on verbose messaging (-v) to ./configure to see the final report.
74 If you believe this message is in error you may use the continue
75 switch (-continue) to ./configure to continue.
76 -------------------->8------------------
78 Solution to this problem is simple we have to pass all libraries exactly
79 in order of their initial mention by upper layers.
81 Change-Id: I7ff00901031a8eb85b4fbd7889b0e0c02be806bb
83 This fix was sent to Qt Gerrit for review here:
84 https://codereview.qt-project.org/#/c/145967/
87 config.tests/unix/compile.test | 2 +-
88 1 file changed, 1 insertion(+), 1 deletion(-)
90 diff --git a/config.tests/unix/compile.test b/config.tests/unix/compile.test
91 index f484f03..dac0a4f 100755
92 --- a/config.tests/unix/compile.test
93 +++ b/config.tests/unix/compile.test
94 @@ -73,7 +73,7 @@ test -r Makefile && $MAKE distclean >/dev/null 2>&1
95 rm -f "$EXE" "${EXE}.exe"
97 echo "QT_BUILD_TREE = $OUTDIR" > "$OUTDIR/$TEST/.qmake.cache"
98 -"$OUTDIR/bin/qmake" -spec "$QMKSPEC" "CONFIG+=$QMAKE_CONFIG" "CONFIG-=debug_and_release" "LIBS*=$LFLAGS" "LIBS+=$MAC_ARCH_LFLAGS" "INCLUDEPATH*=$INCLUDEPATH" "QMAKE_CXXFLAGS*=$CXXFLAGS" "QMAKE_CXXFLAGS+=$MAC_ARCH_CXXFLAGS" "$SRCDIR/$TEST/$EXE.pro" -o "$OUTDIR/$TEST/Makefile"
99 +"$OUTDIR/bin/qmake" -spec "$QMKSPEC" "CONFIG+=$QMAKE_CONFIG" "CONFIG-=debug_and_release" "LIBS=$LFLAGS" "LIBS+=$MAC_ARCH_LFLAGS" "INCLUDEPATH*=$INCLUDEPATH" "QMAKE_CXXFLAGS*=$CXXFLAGS" "QMAKE_CXXFLAGS+=$MAC_ARCH_CXXFLAGS" "$SRCDIR/$TEST/$EXE.pro" -o "$OUTDIR/$TEST/Makefile"
101 if [ "$VERBOSE" = "yes" ]; then