Cra*_*een 5 pthreads autotools
我在我的Linux应用程序中添加了一些pthreads代码,我正在使用autotools构建.我收到一个关于不在libpthreads中链接的错误.所以我想在autotools中指定pthreads依赖项和编译器/链接器标志.
我发现一些引用说使用ACX_PTHREAD宏.GNU提供了一个AX_PTHREAD宏.两者在概念上非常相似.但我都试过(在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
...
Run Code Online (Sandbox Code Playgroud)
在这种情况下,该./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
...
Run Code Online (Sandbox Code Playgroud)
我注意到它检查-lpthreads,但不应该检查-lpthread?
我发现我可以使用:
AC_CHECK_LIB(pthread, pthread_create, [PTHREAD_LIBS+=-lpthread])
Run Code Online (Sandbox Code Playgroud)
然后构建成功.但我认为这不是使其在最广泛的平台上运行的最佳方式.
我看到Ubuntu也有一个包libpthread-stubs0-dev.但我不确定它的用途.
将pthreads与autotools一起使用的"正确方法"是什么?
感谢 Peter Simons 在 autoconf 邮件列表上提出的问题,我们得到了一个有点官方的答案:
编译器标志和链接器标志不是互斥的集,尤其是因为链接通常是通过编译器前端 (cc) 完成的,而不是直接调用链接器 (ld) 完成的。您可以在编译步骤中使用的任何标志(例如 -O2、-DFOO、-I/tmp/include)通常都会在链接步骤中被接受,即使它当时不适用。(反之亦然,例如 -lfoo。)
鉴于此,在链接时使用 PTHREAD_CFLAGS (和其他 CFLAGS 变量)比将适用的标志复制到 PTHREAD_LIBS/LDFLAGS/等中更不容易出错。变量并且不使用任何 CFLAGS 变量。
因此,您的链接器也可以使用 PTHREAD_CFLAGS 。