Joe Simon
Joe Simon

Reputation: 99

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)

Upvotes: 5

Views: 9109

Answers (3)

Alok Prasad
Alok Prasad

Reputation: 700

Acutally if you use -lrt before c file there is error

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

/tmp/ccR93VIp.o: In function tfunc': test.c:(.text+0x31): undefined reference tomq_getattr' test.c:(.text+0x8f): undefined reference to mq_receive' /tmp/ccR93VIp.o: In functionmain': test.c:(.text+0x145): undefined reference to mq_open' test.c:(.text+0x194): undefined reference tomq_notify' collect2: error: ld returned 1 exit status

This works

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

Upvotes: 0

Reddy
Reddy

Reputation: 67

You can compile it using $gcc -g -Wall -o mqtest mqtest.c -lrt to make it work without compilation errors as undefined reference for posix message queue apia. (as mentioned in the comments )

Upvotes: 0

Duck
Duck

Reputation: 27572

'undefined reference to mq_open' collect2:

Link agaist librt. E.G. -lrt

Upvotes: 8

Related Questions