PDA

View Full Version : Problems with static linking -->segmentation fault



titoaii
18th July 2010, 13:47
Hello everybody,

I've done a little GUI application with Qt and I would like to link all its libraries statically so I can bring it to another PC without installing anything else.

I've downloaded Qt 4.5.3 and installed it statically. (./configure -static......)

I've added the -static and -static-libgcc option in the LFLAGS line in the Makefile and using pkg-config --static --libs "lib" I've added the dependencies of some libraries to the option LIBS. This is the result of those:
LFLAGS = -static -static-libgcc -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.5.3/lib
LIBS = $(SUBLIBS) -L/usr/local/Trolltech/Qt-4.5.3/lib -lQt3Support -L/usr/local/Trolltech/Qt-4.5.3/lib -lQtSql -pthread -lQtXml -L/usr/X11R6/lib -pthread -lQtNetwork -lQtGui -lpng -lfreetype -lSM -lICE -lXrender -lXext -lX11 -lxcb -lXau -lXdmcp -lQtCore -lz -lm -lgthread-2.0 -lrt -lglib-2.0 -ldl -lfontconfig -lexpat -lfreetype -lz


When executing make I obtain some warnings about glibc dependencies.

alberto@alberto-laptop:~/Escritorio/vhdlide copy$ make
g++ -static -static-libgcc -Wl,-O1 -Wl,-rpath,/usr/local/Trolltech/Qt-4.5.3/lib -o bin/vhdlide build/vhdlada.o build/main.o build/vEdicion.o build/filedialogextendido.o build/proyecto.o build/dlgBuscar.o build/acercade.o build/nuevoFichero.o build/opcionesProyecto.o build/highlighter.o build/ventanaContenido.o build/moc_vhdlada.o build/moc_vEdicion.o build/moc_dlgBuscar.o build/moc_acercade.o build/moc_nuevoFichero.o build/moc_opcionesProyecto.o build/moc_highlighter.o build/moc_ventanaContenido.o build/qrc_icons.o -L/usr/local/Trolltech/Qt-4.5.3/lib -lQt3Support -L/usr/local/Trolltech/Qt-4.5.3/lib -lQtSql -pthread -lQtXml -L/usr/X11R6/lib -pthread -lQtNetwork -lQtGui -lpng -lfreetype -lSM -lICE -lXrender -lXext -lX11 -lxcb -lXau -lXdmcp -lQtCore -lz -lm -lgthread-2.0 -lrt -lglib-2.0 -ldl -lfontconfig -lexpat -lfreetype -lz
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libX11.a(CrGlCur.o): In function `open_library':
(.text+0x3b): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/Trolltech/Qt-4.5.3/lib/libQtCore.a(qfsfileengine_unix.o): In function `QFSFileEngine::owner(QAbstractFileEngine::FileOwn er) const':
qfsfileengine_unix.cpp:(.text+0xba9): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libglib-2.0.a(gutils.o): In function `g_get_any_init_do':
(.text+0xf89): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libglib-2.0.a(gutils.o): In function `g_get_any_init_do':
(.text+0xf7c): warning: Using 'setpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libglib-2.0.a(gutils.o): In function `g_get_any_init_do':
(.text+0xf94): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/Trolltech/Qt-4.5.3/lib/libQt3Support.a(q3filedialog.o): In function `Q3FileDialog::setDir(QString const&)':
q3filedialog.cpp:(.text+0x14f00): warning: Using 'getpwnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/local/Trolltech/Qt-4.5.3/lib/libQtGui.a(qapplication_x11.o): In function `sm_performSaveYourself(QSessionManagerPrivate*)':
qapplication_x11.cpp:(.text+0x195d8): warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libxcb.a(xcb_util.o): In function `xcb_connect_to_display_with_auth_info':
(.text+0x3b4): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libICE.a(icetrans.o): In function `_IceTransGetPeerNetworkId':
(.text+0x1d66): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libICE.a(icetrans.o): In function `_IceTransSocketINETConnect':
(.text+0x2a5e): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/libICE.a(icetrans.o): In function `_IceTransSocketINETCreateListener':
(.text+0x265b): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking



Despite this warnings I obtain the statically linked application. You can see it here:

alberto@alberto-laptop:~/Escritorio/vhdlide copy$ file ./bin/vhdlide
./bin/vhdlide: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, for GNU/Linux 2.6.15, not stripped


The biggest problem is that when I try to execute the program there is a segmentation fault and I don't know why.

I include here the last lines of the trace of the program, I think that it can be something about librt but I don't know what.

access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/libpthread.so.0", O_RDONLY) = 9
read(9, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\24 0H\0\0004\0\0\0"..., 512) = 512
fstat64(9, {st_mode=S_IFREG|0755, st_size=112548, ...}) = 0
mmap2(NULL, 94696, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 9, 0) = 0x974000
mmap2(0x988000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 9, 0x13) = 0x988000
mmap2(0x98a000, 4584, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x98a000
close(9) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/librt.so.1", O_RDONLY) = 9
read(9, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\30 0\30\0\0004\0\0\0"..., 512) = 512
fstat64(9, {st_mode=S_IFREG|0644, st_size=30684, ...}) = 0
mmap2(NULL, 33364, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 9, 0) = 0x2b2000
mmap2(0x2b9000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 9, 0x6) = 0x2b9000
close(9) = 0
mprotect(0x2b9000, 4096, PROT_READ) = 0
mprotect(0x988000, 4096, PROT_READ) = 0
mprotect(0x414000, 4096, PROT_READ) = 0
mprotect(0x38f000, 4096, PROT_READ) = 0
mprotect(0xf82000, 4096, PROT_READ) = 0
mprotect(0x4ee000, 32768, PROT_READ) = 0
mprotect(0x4a3000, 4096, PROT_READ) = 0
mprotect(0x2af000, 4096, PROT_READ) = 0
set_tid_address(0xad508a8) = 3399
set_robust_list(0xad508b0, 0xc) = 0
futex(0xbff5b554, FUTEX_WAKE_PRIVATE, 1) = 0
futex(0xbff5b554, 0x189 /* FUTEX_??? */, 1, NULL, bff5b564) = -1 EAGAIN (Resource temporarily unavailable)
rt_sigaction(SIGRTMIN, {0x9782e0, [], SA_RESTORER|SA_SIGINFO, 0x981d20}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {0x978780, [], SA_RESTORER|SA_RESTART|SA_SIGINFO, 0x981d20}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++


I hope that somebody can help me because I'm going crazy, I can't find the error and I've tried lots of things.

Thank you very much in advance,

Alberto

franz
18th July 2010, 20:29
Wrong glibc version? It's actually kind of warning about the fact that although you statically link your Qt libraries (and possibly it's dependencies), you will still need glibc shared objects of a specific version on the target pc. Since these libraries are very likely available on the target pc, you should in my opinion use dynamic linking, since this makes your application much easier to install on other linux boxes.

titoaii
18th July 2010, 21:54
Thank you for your reply, I need to make it static because it has been asked to me, it's not my decision :(.
So if you know something about how to fix it i'll be very pleased. Every idea will be welcome :)

squidge
18th July 2010, 22:28
Then you need to ensure you use the exact same libraries to statically link your app as are present on the target machine.

and make sure you comply with the Qt license for statically compiled applications - it's not as flexible as the one for dynamically linked apps.

titoaii
18th July 2010, 23:11
Yes, I know I need exactly the same libraries, but as I see in the trace, there is something strange happening and I don't know what. That's what I'm asking for.
Thank you!

squidge
19th July 2010, 00:01
Well, static linking with pthread is known to segfault (I'm not saying its the fault here, but it might be. It's known to cause people problems). You could try rebuilding gcc to allow for proper thread local storage in your libstdc++ library, but you will then have to update the libraries on the target to match with your recompiled libraries on the host.

Much easier to just dynamically link :)

HarishVictory
10th December 2016, 07:00
The solution is to surround -lpthread by two more switches i.e.,
-Wl,--whole-archive -lpthread -Wl,--no-whole-archive

Ref:
http://stackoverflow.com/questions/9002264/starting-a-stdthread-with-static-linking-causes-segmentation-fault