C/C++静态链接pthread库的坑【-static -pthread】


参考:
https://blog.csdn.net/Kajima_/article/details/111415651

在静态编译tig时,遇到如下问题:

LDFLAGS=--static ./configure --prefix=`pwd`/install
make V=1

log如下:

...
gcc -g -O2  -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DHAVE_CONFIG_H '-DTIG_VERSION="2.5.5"' '-DSYSCONFDIR="/home/pengdl/work/Third_Part/tig/install/etc"'  --static src/tig.o src/types.o src/string.o src/util.o src/map.o src/argv.o src/io.o src/refdb.o src/builtin-config.o src/request.o src/line.o src/keys.o src/repo.o src/options.o src/draw.o src/prompt.o src/display.o src/view.o src/search.o src/parse.o src/watch.o src/pager.o src/log.o src/reflog.o src/diff.o src/help.o src/tree.o src/blob.o src/blame.o src/refs.o src/status.o src/stage.o src/main.o src/stash.o src/grep.o src/ui.o src/apps.o src/graph.o src/graph-v1.o src/graph-v2.o compat/hashtab.o compat/utf8proc.o -lpcreposix -lpcre -Wl,-Bsymbolic-functions -lncursesw -ltinfo -o src/tig
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libpcre.a(libpcre_la-pcre_jit_compile.o): in function `sljit_generate_code':
(.text+0x986): undefined reference to `pthread_mutex_lock'
/usr/bin/ld: (.text+0xa0a): undefined reference to `pthread_mutex_unlock'
/usr/bin/ld: (.text+0xe6c): undefined reference to `pthread_mutex_unlock'
/usr/bin/ld: (.text+0xe8f): undefined reference to `pthread_mutex_unlock'

从最后的链接看,没有pthread,然后修改配置:

LIBS=-lpthread LDFLAGS=--static ./configure --prefix=`pwd`/install
make V=1

log如下:

gcc -g -O2  -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DHAVE_CONFIG_H '-DTIG_VERSION="2.5.5"' '-DSYSCONFDIR="/home/pengdl/work/Third_Part/tig/install/etc"'  --static src/tig.o src/types.o src/string.o src/util.o src/map.o src/argv.o src/io.o src/refdb.o src/builtin-config.o src/request.o src/line.o src/keys.o src/repo.o src/options.o src/draw.o src/prompt.o src/display.o src/view.o src/search.o src/parse.o src/watch.o src/pager.o src/log.o src/reflog.o src/diff.o src/help.o src/tree.o src/blob.o src/blame.o src/refs.o src/status.o src/stage.o src/main.o src/stash.o src/grep.o src/ui.o src/apps.o src/graph.o src/graph-v1.o src/graph-v2.o compat/hashtab.o compat/utf8proc.o -lpthread -lpcreposix -lpcre -Wl,-Bsymbolic-functions -lncursesw -ltinfo -o src/tig
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libpcre.a(libpcre_la-pcre_jit_compile.o): in function `sljit_generate_code':
(.text+0x986): undefined reference to `pthread_mutex_lock'
/usr/bin/ld: (.text+0xa0a): undefined reference to `pthread_mutex_unlock'
/usr/bin/ld: (.text+0xe6c): undefined reference to `pthread_mutex_unlock'
/usr/bin/ld: (.text+0xe8f): undefined reference to `pthread_mutex_unlock'
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/l

错误一样。

通过询问度娘,然后修改配置:

LIBS=-pthread LDFLAGS=--static ./configure --prefix=`pwd`/install
make V=1

log如下:

config.status: config.h is unchanged
gcc -g -O2  -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 -DHAVE_CONFIG_H '-DTIG_VERSION="2.5.5"' '-DSYSCONFDIR="/home/pengdl/work/Third_Part/tig/install/etc"'  --static src/tig.o src/types.o src/string.o src/util.o src/map.o src/argv.o src/io.o src/refdb.o src/builtin-config.o src/request.o src/line.o src/keys.o src/repo.o src/options.o src/draw.o src/prompt.o src/display.o src/view.o src/search.o src/parse.o src/watch.o src/pager.o src/log.o src/reflog.o src/diff.o src/help.o src/tree.o src/blob.o src/blame.o src/refs.o src/status.o src/stage.o src/main.o src/stash.o src/grep.o src/ui.o src/apps.o src/graph.o src/graph-v1.o src/graph-v2.o compat/hashtab.o compat/utf8proc.o -pthread -lpcreposix -lpcre -Wl,-Bsymbolic-functions -lncursesw -ltinfo -o src/tig

完美编过。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM