Compilation problem with Posix Mes Q

Hi

#include "training.h"
#include <mqueue.h> // for posix mqs

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

    
    int opt,flag;
    mqd_t msq; // msg q type
    flag=O_RDWR|O_CREAT;

    while((opt =getopt(argc,argv,"e")) != -1)
    {
        switch(opt)
        {
            case 'e':
                flag|=O_EXCL;
                break;
        }
    }
    // option indicator should be one less than what is passed
    if(optind != argc -1)
    {
        fprintf(stderr,"usag: msqcr -e <name>\n");
        return 1;
    }
    msq =mq_open(argv[optind],flag,0666,NULL);
    mq_close(msq);


}

When i tried to compile this code

-bash-3.1$ gcc mqcreate.c
/tmp/cccJjZP4.o: In function `main':
mqcreate.c:(.text+0xc1): undefined reference to `mq_open'
mqcreate.c:(.text+0xcf): undefined reference to `mq_close'
collect2: ld returned 1 exit status
-bash-3.1$

I am having problem in linking i guess.

Please can anyone help me on this

THanks
Kumaran

I'm guressing you are on Solaris -- you need

gcc myprog.c -o myprog -lrt

If you are on another platform the problem may be something else.

Thanks Jim.

But i am using Linux.

You need to specify the path and name of the mq shared library to link to...on linux it maybe something like...

gcc mqcreate.c -L/opt/imq/lib -lmqcrt

I have checked for the library

-bash-3.1$ find . -name *mqcrt* 2>/dev/null
-bash-3.1$ pwd
/
-bash-3.1$

but i couldn't find one... is thery anyway to download and use it.
Please help me on this

---------- Post updated at 03:24 PM ---------- Previous update was at 10:08 AM ----------

I manage to find a way to compile it.

When we use message queue in Linux, it should be compiled by linking runtime library like

gcc -ltr mqcreat.c

Thanks Experts.

1 Like