【问题标题】:Weird posix message queue linking issue - sometimes it doesn't link correctly奇怪的 posix 消息队列链接问题 - 有时它没有正确链接
【发布时间】:2013-11-26 16:04:18
【问题描述】:

当我构建以下代码时,它构建得很好。如果我更改代码以注释掉“while”,使用相同的命令行,它不会构建(见下文)

#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)

更新:

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

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)

【问题讨论】:

  • 看起来像是 ciompiler 版本的问题,我在不同的盒子上构建了相同的代码,
  • 也许编译器优化了它,因为mq_open 永远不会被执行。
  • @duck 似乎不是这样,因为如果它被优化掉,那么应该没有未定义的符号,因为它不再存在。
  • 您在上面说过,一旦您将其注释掉,它就不会编译,但它已经在那里编译了。在无限循环中,编译器可能只是将mq_open 完全丢弃,因此没有符号可以阻塞。
  • 对不起,我现在明白了...向后阅读您的评论,谢谢,我会尝试其他一些事情,看看是否是这种情况。

标签: c linux gcc posix message-queue


【解决方案1】:

'对 mq_open 的未定义引用'collect2:

链接到 librt。例如。 -lrt

【讨论】:

  • 谢谢,我从上面...“gcc -g -Wall -lrt -o mqtest mqtest.c”这就是奇怪的行为...顶部示例构建,底部没有,完全相同的命令行...
  • 对不起,我错过了。更改为gcc -g -Wall -o mqtest mqtest.c -lrt。我无法重现您的问题。均已编译。
  • 谢谢@Duck。看起来像一个编译器版本问题,我尝试的前 2 个盒子我遇到了同样的问题,第 3 天它运行良好。打算尝试修复坏盒子上的编译器。再次感谢 - 乔
  • @Duck 我无法在 pkg-config 中为 g++ 指定 lrt。它说找不到 lrt 库。有什么想法吗?
  • @Duck 我在此处记录了上述问题:stackoverflow.com/questions/57094210/…
【解决方案2】:

您可以使用 $gcc -g -Wall -o mqtest mqtest.c -lrt 对其进行编译,使其在没有编译错误的情况下工作,作为 posix 消息队列 API 的未定义参考。 (如 cmets 中所述)

【讨论】:

    【解决方案3】:

    确实如果你在c文件前使用-lrt会报错

    root@embsys-VirtualBox:~# gcc -lrt test.c

    /tmp/ccR93VIp.o:在函数tfunc': test.c:(.text+0x31): undefined reference tomq_getattr' test.c:(.text+0x8f): 未定义对mq_receive' /tmp/ccR93VIp.o: In functionmain'的引用: test.c:(.text+0x145): 未定义引用mq_open' test.c:(.text+0x194): undefined reference tomq_notify' collect2:错误:ld 返回 1 个退出状态

    这行得通

    root@embsys-VirtualBox:~# gcc test.c -lrt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-31
      • 1970-01-01
      • 2019-10-20
      • 2013-11-06
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多