【发布时间】:2013-06-07 23:05:31
【问题描述】:
我正在使用自动工具构建的 Linux 应用程序中添加一些 pthreads 代码。我收到关于未在 libpthreads 中链接的错误。所以我想在 autotools 中指定 pthreads 依赖和编译器/链接器标志。
我发现some references 说使用ACX_PTHREAD macro。 GNU 提供了一个AX_PTHREAD macro。两者在概念上非常相似。但我都试过了(在 Ubuntu 13.04 64 位上),发现他们在$PTHREAD_CFLAGS 中设置了-pthread,但由于某种原因,他们没有在$PTHREAD_LIBS 中设置-lpthread 链接器标志。
构建失败。当我运行make 时,我得到:
...
/bin/sh ../libtool --tag=CXX --mode=link g++ -g -O2 -o myapp main.o ... -lconfuse -llog4cpp -lnsl -lpopt -lfuse -L/usr/local/lib -lrt
libtool: link: g++ -g -O2 -o .libs/myapp main.o ... -lconfuse -llog4cpp -lnsl /usr/lib/x86_64-linux-gnu/libpopt.so -lfuse -L/usr/local/lib -lrt
/usr/bin/ld: app-fuse.o: undefined reference to symbol 'pthread_kill@@GLIBC_2.2.5'
/usr/bin/ld: note: 'pthread_kill@@GLIBC_2.2.5' is defined in DSO /lib/x86_64-linux-gnu/libpthread.so.0 so try adding it to the linker command line
/lib/x86_64-linux-gnu/libpthread.so.0: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
...
在这种情况下,./configure 步骤显示:
...
checking for the pthreads library -lpthreads... no
checking whether pthreads work without any flags... no
checking whether pthreads work with -Kthread... no
checking whether pthreads work with -kthread... no
checking for the pthreads library -llthread... no
checking whether pthreads work with -pthread... yes
checking for joinable pthread attribute... PTHREAD_CREATE_JOINABLE
checking if more special flags are required for pthreads... no
checking for PTHREAD_PRIO_INHERIT... yes
...
我注意到它会检查-lpthreads,但它不应该检查-lpthread吗?
我发现我可以使用:
AC_CHECK_LIB(pthread, pthread_create, [PTHREAD_LIBS+=-lpthread])
然后构建成功。但我认为这并不是让它在最广泛的平台上工作的最佳方式。
我看到 Ubuntu 也有一个 package libpthread-stubs0-dev。但我不确定它是干什么用的。
在 autotools 中使用 pthread 的“正确方法”是什么?
【问题讨论】:
-
ACX_宏于 2009 年 8 月 6 日在 GNU Autoconf 存档中重命名为AX_ -
我发现 ACX_PTHREAD 在我的系统上使用 -lpthreads 正确的库名称是 -lpthread 没有 S。检查您的 config.log 中的 /usr/bin/ld: 找不到 -lpthreads
-
ax_pthread.m4 文件做对了