gpt4 book ai didi

pthreads - pthread 的自动工具未设置正确的链接器标志

转载 作者:行者123 更新时间:2023-12-04 20:11:14 25 4
gpt4 key购买 nike

我正在将一些 pthreads 代码添加到我使用 autotools 构建的 Linux 应用程序中。我收到关于未在 libpthreads 中链接的错误。所以我想在 autotools 中指定 pthreads 依赖项和编译器/链接器标志。

我找到了 some references也就是说使用 ACX_PTHREAD macro . GNU 提供了一个 AX_PTHREAD macro .两者在概念上非常相似。但我都试过了(在 Ubuntu 13.04 64 位上),发现它们设置了 -pthread$PTHREAD_CFLAGS ,但出于某种原因,他们没有设置 -lpthread $PTHREAD_LIBS 中的链接器标志.

构建失败。当我跑 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 .但我不确定它是做什么用的。

将 pthreads 与 autotools 一起使用的“正确方法”是什么?

最佳答案

感谢在 autoconf 邮件列表中询问的 Peter Simons,我们有一些 official answer :

Compiler flags and linker flags are not mutually-exclusive sets, not least because linking is typically done via the compiler frontend (cc) and not by invoking the linker (ld) directly. Any flags that you can use in the compile step (e.g. -O2, -DFOO, -I/tmp/include) will generally be accepted in the linking step, even if it's not applicable then. (The reverse may not be true, e.g. -lfoo.)

Given that, it's a lot less error-prone to use PTHREAD_CFLAGS (and other CFLAGS variables) when linking, rather than duplicating the applicable flags into PTHREAD_LIBS/LDFLAGS/etc. variables and not using any CFLAGS variables then.



因此,也只需将 PTHREAD_CFLAGS 用于您的链接器。

关于pthreads - pthread 的自动工具未设置正确的链接器标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17055279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com