5

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)
jww
  • 97,681
  • 90
  • 411
  • 885
Joe Simon
  • 99
  • 1
  • 2
  • 9
  • Looks like a ciompiler verion issues, I built the same code on a different box, – Joe Simon Nov 13 '13 at 21:24
  • Maybe the compiler optimized it out because the `mq_open` will never be executed. – Duck Nov 13 '13 at 21:29
  • @duck It doesn't seem like that would be the case, since if it is optimized away then there should be no undefined symbol since it is in effect no longer there. – Joe Simon Nov 13 '13 at 21:41
  • You said above that it didn't compile once you commented it out but that it had compiled with it there. With the endless while loop maybe the compiler just the chucked `mq_open` out entirely, hence no symbol to choke on. – Duck Nov 13 '13 at 21:44
  • Sorry I understand now... read your comment backwards, thanks I will try a few other things to see if that is the case. – Joe Simon Nov 13 '13 at 21:54
  • Ok, yes when I change from while(1) to while(0) I get the changed behavior, so I think you are correct about the optimizer. now I just need to find out why the compiler does not find mq_open, when I am including -lrt on the command line. – Joe Simon Nov 13 '13 at 21:59
  • From the GCC manual: "It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded." This what your "gcc -g -Wall -lrt -o mqtest mqtest.c" does as I commented below. – Duck Nov 13 '13 at 22:09
  • @Duck - Yes, sorry you pointed that out earlier but I glossed over it with my blinders on. Thanks Again, that seems to have solved the problem, it builds now. (Now to put the app back together and get it to work again) – Joe Simon Nov 13 '13 at 22:16
  • You're welcome Joe. Good luck with the reassembly. – Duck Nov 13 '13 at 22:23
  • put -lrt at end and it will work.. – Alok Prasad Jan 27 '18 at 14:52

3 Answers3

8

'undefined reference to mq_open' collect2:

Link agaist librt. E.G. -lrt

Duck
  • 26,924
  • 5
  • 64
  • 92
  • Thanks, I am-from above... "gcc -g -Wall -lrt -o mqtest mqtest.c" that is what the weird behavior is... the top example builds, the bottom does not, same exact command line... – Joe Simon Nov 13 '13 at 21:14
  • 1
    Sorry, I missed it. Change to `gcc -g -Wall -o mqtest mqtest.c -lrt`. I could not reproduce your problem. Both compiled. – Duck Nov 13 '13 at 21:23
  • Thanks @Duck. Looks like a compiler version issue, the first 2 boxes I tried it on I got the same issue, on the 3rd it worked fine. Going to try to fix the compiler on the bad box. Thanks Again - Joe – Joe Simon Nov 13 '13 at 21:28
  • @Duck I am not able to specify lrt inside pkg-config for g++. It says lrt library is not found. Any ideas? – Ace Jul 18 '19 at 12:24
  • @Duck I have the above issue documented here:https://stackoverflow.com/questions/57094210/makefile-g-lrt-problem-cannot-find-lrt – Ace Jul 18 '19 at 12:24
0

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 )

Reddy
  • 57
  • 12
0

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

Alok Prasad
  • 622
  • 7
  • 12