奇怪的posix消息队列链接问题-有时无法正确链接 [英] Weird posix message queue linking issue - sometimes it doesn't link correctly

查看:92
本文介绍了奇怪的posix消息队列链接问题-有时无法正确链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我构建以下代码时,它可以正常运行.如果我使用相同的命令行更改代码以注释掉"while",则它不会生成(请参见下文)

When I build the following code, it builds fine. If I change the code to comment out the "while", using the same command line, it does not build (see below)

#include <stdio.h>
#include <mqueue.h>

int main(int argc, char *argv[]) {
   while (1) { }

   mq_open("/YouSUCK", O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);

   return 0;
}

dada@thud:~/RaspberryPI$ gcc -g -Wall -lrt -o mqtest mqtest.c

dada@thud:~/RaspberryPI$ 

#include <stdio.h>
#include <mqueue.h>

int main(int argc, char *argv[]) {
//   while (1) { }

   mq_open("/YouSUCK", O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG, NULL);

   return 0;
}

dada@thud:~/RaspberryPI$ gcc -g -Wall -lrt -o mqtest mqtest.c

/tmp/cccw376u.o: In function `main':

/home/dada/RaspberryPI/mqtest.c:7: undefined reference to `mq_open'

collect2: ld returned 1 exit status

dada@thud:~/RaspberryPI$

有什么想法吗?

dada@thud:~/RaspberryPI$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

更新:

看起来像编译器版本问题,我在不同的盒子上构建了相同的代码,并且可以使用上述任何一个文件正确构建.所以我想我需要一个不同的编译器.

Looks like a compiler version issue, I built the same code on a different box, and it builds properly with either of the above files. So I guess I need a different compiler.

dada@JoesPi ~ $ gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/arm-linux-gnueabihf/4.6/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.3-14+rpi1' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-sjlj-exceptions --with-arch=armv6 --with-fpu=vfp --with-float=hard --enable-checking=release --build=arm-linux-gnueabihf --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf
Thread model: posix
gcc version 4.6.3 (Debian 4.6.3-14+rpi1)

推荐答案

对mq_open的未定义引用" collect2:

'undefined reference to mq_open' collect2:

链接反对者librt.例如. -lrt

Link agaist librt. E.G. -lrt

这篇关于奇怪的posix消息队列链接问题-有时无法正确链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆