c++ - gcc dynamic linking dependency missing library? -
main.cc
#include <stdio.h> #include <thread> int output() { printf("thread\n"); } int keke(); int main() { //keke(); std::thread th(output); th.join(); return 0; }
keke.cc
#include <unistd.h> #include <pthread.h> int keke() { pthread_attr_t attr; return pthread_attr_init(&attr); }
makefile
haha: libkeke.so g++ -std=c++11 -o haha main.cc -l. -lkeke -pthread libkeke.so: keke.cc g++ -o libkeke.so -fpic -shared keke.cc -pthread clean: rm libkeke.so haha
make
, result haha not load pthread.so,
(link)$ ldd haha linux-vdso.so.1 => (0x00007fff65972000) libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f9dee04a000) libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9dede34000) libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9deda6f000) libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f9ded769000) /lib64/ld-linux-x86-64.so.2 (0x00007f9dee34e000)
ldd
shows executable not depends on pthread library , crash. if uncomment call keke
main.cc
, program works well.
in conclusion, gcc not write dependency library(pthread
) dynamic section if previous shared library(libkeke.so
) depend on it. however, if executable not refer symbol in specific library(does not call keke), ld not write library(libkeke.so
) dynamic section either. in consequence, ld.so
not load dependency library(pthread
) , executable(haha
) crash.
the problem why not gcc write depend in dynamic section no matter whether included in previous depends msvc does? there drawback this?
Comments
Post a Comment