Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird posix message queue linking issue - sometimes it doesn't link correctly

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$

Any ideas ?

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)

Update:

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)
like image 379
Joe Simon Avatar asked Nov 13 '13 20:11

Joe Simon


1 Answers

'undefined reference to mq_open' collect2:

Link agaist librt. E.G. -lrt

like image 129
Duck Avatar answered Sep 25 '22 14:09

Duck