Click to See Complete Forum and Search --> : /usr/bin/ld: cannot find -lpthread
karthik
04-22-2004, 12:12 PM
I am writing a multithreaded C++ program using pthreads. When i compile the program as a dynamic binary it works fine. But when i try to compile it as a static binary it gives me this error:
/usr/bin/ld: cannot find -lpthread
i have the path of libpthreads.so.0 in my /etc/ld.conf and re-ran ldconfig also. The program compiles and runs fine when i compile it as a dynamic binary.
This is on a linux machine running g++ 3.3.1. Thanks for the help
Karthik
tecknophreak
04-22-2004, 02:15 PM
Originally posted by karthik
i have the path of libpthreads.so.0
Is that a typo on your part? It should be libpthread, if not, that could be your error.
karthik
04-22-2004, 04:47 PM
Sorry, that was a typo error on my part. The pthread library is libpthread.so.0.
thanks
bwkaz
04-22-2004, 06:42 PM
Since you're statically compiling, you need to have libpthread.a, not libpthread.so. It's in /usr/lib on my machine (you need libpthread.so to be in /lib because you need it at boot time before /usr is mounted if /usr is a separate filesystem, but /usr is always mounted during development which is the only time that .a files are required).
This file is most likely part of glibc-devel or libc-dev (depending on your distro).
Also, the contents of /etc/ld.so.cache (which are created after modifying /etc/ld.so.conf and running /sbin/ldconfig, like you did) are not used at compile time. They're only used at runtime. At compile time, you need to pass -L arguments to g++ (but not for /usr/lib, since the default search path for gcc and g++ is /lib and /usr/lib, and possibly also /usr/local/lib depending on the way you configured gcc/g++).
karthik
04-22-2004, 07:01 PM
thanks bwkaz, i searched my system for libpthread.a but could not find it. I think it is not installed. Inorder to get it do i need to compile glibc-devel package ?
bwkaz
04-23-2004, 07:23 PM
Which distro?
You shouldn't have to compile anything, unless this is a source-based distro (in which case, it should have been installed when you compiled and installed glibc the first time). For Red Hat derived distros, you should be able to just install the glibc-devel RPM. For Debian, I think it's the libc-dev package. For others, I'm not really sure; maybe searching for libpthread.a on your distro's site would help?
karthik
04-24-2004, 01:17 PM
sorry abt the late reply,
I am using Mandrake 9.0. I will try searching for the libpthread.a library file on their site and see if i could land up with anything. I am afraid to go the RPM way coz dont know what else it could mess up :) . If i could'nt find the .a file then maybe i would do that.
thanx for all the help
karthik
bwkaz
04-24-2004, 01:59 PM
If you're only installing a -devel RPM, it shouldn't mess anything up. Just make sure you get the RPM for Mandrake 9.0 (and not Red Hat 9 or Fedora or something like that, because other distros' glibc-devel RPMs will depend on the glibc that that distro provides, which almost assuredly is different than the one your distro provides).
After some research at rpmfind.net, it seems that the exact package that you need is glibc-static-devel, which requires glibc-devel, which requires glibc. Look for glibc-devel and glibc-static-devel on your Mandrake CDs, as those versions will be compatible with the glibc that you already have installed (and you don't want to try to replace the glibc that you already have installed! ;)).
karthik
04-26-2004, 05:36 PM
got a little further but stuck again.
Actually my system was mandrake 9.2 (reported incorrectly as 9.0 previously). Anyways i installed glibc-static-devel rpm and it installed a few *.a files in /usr/lib
Now g++ complains that it could not find lstdc++ (libstdc++.a) which is not present on my system. The libraries my program depends upon are:
[root@testing1 glibc-2.3.2]# ldd /root/test_thread/new.dyn
libpthread.so.0 => /lib/i686/libpthread.so.0 (0x40021000)
libstdc++.so.5 => /usr/lib/libstdc++.so.5 (0x40071000)
libm.so.6 => /lib/i686/libm.so.6 (0x4012a000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x4014d000)
libc.so.6 => /lib/i686/libc.so.6 (0x40157000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
[root@testing1 glibc-2.3.2]#
Out of these libgcc_s also does'nt contain a libgcc_s.a file but i found out after some googling that it takes libgcc.a which is currently present. All the other libraries have their corresponding .a files except libstdc++.
I found on some mailing lists that gcc 3.x constains libstdc++ within itself and the static-devel package should contain the .a file, but the RPM that i installed from the installation disks themselves did not contain it.
related links for libstdc++ are:
GNU page (http://www.doubleand.tv/doc/cpp-3.1/libstdc++/html/17_intro/RELEASE-NOTES)
libstdc++ home page (http://www.opensource.apple.com/darwinsource/10.3/gccfast-1614/libstdc++-v3/docs/html/install.)
Also the libstdc++-devel packages that i found on rpmfind.net are all for redhat and i could not find anything for Mandrake. Should i be download the source from libstdc++ home page and compile the static library package ??
thanks for the help
karthik
bwkaz
04-26-2004, 09:09 PM
That's because the package you're looking for is libstdc++5-static-devel, not just libstdc++. :p (or at least, that's what rpmfind says when searching for "libstdc++" with a filter of "mandrake".)
Make sure you get it off your Mandrake CDs, if you can find it there. If you can't, then grab the version off rpmfind that matches your gcc version (gcc -dumpversion -- they've got one version for gcc 3.3.1-4mdk, one for 3.3.1-2mdk, and one for 3.2.2-3mdk). You may also need the libstdc++5-devel package (for the same gcc version).
If your gcc isn't any of those versions, then maybe one of the Mandrake mirror sites (or the Mandrake FTP site) would have a copy of the right package.
karthik
04-27-2004, 12:04 PM
thanks bwkaz, got it resolved finally
karthik
karthik
04-27-2004, 01:50 PM
unrelated question to my above problems,
I am trying to create some threads in /sbin/init program and pthread_create loops forever and does not return at all. I could not find any technical description about why it is so and the reason behind not allowing init to spawn threads !! from the comp.threads.programming newsgroup
does anybody have any pointers abt this topic ??
thanks
karthik
bwkaz
04-27-2004, 06:54 PM
Why do you need /sbin/init to be statically linked? Does it work (that is, pthread_create(...)) when you dynamically link the program? You will have access to libraries in /lib when init runs, since the kernel will mount the root filesystem for you...
But no, I haven't ever seen anything about using threads in /sbin/init.
karthik
04-28-2004, 06:20 PM
pthread_create() does not return the freezes the program when used in /sbin/init. There are some postings in the usenet news groups also regarding this issue, but nobody has responded with an explanation of why it is so.
Thats why i was trying to compile init statically to include the pthread library inside and check if it works
Even then it did'nt work :( , so basically no explanation of why init is being prevented from spawning threads
nyways thanks for all the help
karthik