读书人

急关于posix讯息队列!

发布时间: 2013-09-26 10:32:35 作者: rapoo

急,关于posix消息队列!!!
文件包含<mqueue.h>,为什么用gcc编译显示函数未定义???

/tmp/ccc10ZBt.o: In function `main':
mqcreatel.c:(.text+0x93): undefined reference to `mq_open'
mqcreatel.c:(.text+0xa1): undefined reference to `mq_close'
collect2: ld returned 1 exit status



#include <mqueue.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#define MQ_FILE "/mq_test"
#define BUF_LEN 128

int main(void)
{
mqd_t mqd;
char buf[BUF_LEN];
int por = 0;
int ret = 0;
struct mq_attr attr;
attr.mq_flags = 0;
attr.mq_maxmsg = 3;
attr.mq_msgsize = 50;
attr.mq_curmsgs= 0;
mqd = mq_open(MQ_FILE, O_WRONLY,0666,&attr);
if (-1 == mqd)
{
printf("mq_open error.\n");
return -1;
}

do{
buf[BUF_LEN-1]='\0';
printf("MQ_MSG : ");
scanf("%s", buf);
if(buf[BUF_LEN-1]!= '\0')
{
continue;
}
printf("strlen:%d\nMQ_POR : ",strlen(buf));
scanf("%d", &por);
ret== mq_send(mqd, buf, strlen(buf)+1, por);
if (ret != 0)
{
perror("mq_send error.\n");
}
memset(buf,'\0',BUF_LEN);
}while(strcmp(buf, "quit"));


mq_close(mqd);
mq_unlink(MQ_FILE);

return 0;
}






posix c
[解决办法]
NAME
mq_open - open a message queue

SYNOPSIS
#include <fcntl.h> /* For O_* constants */
#include <sys/stat.h> /* For mode constants */
#include <mqueue.h>

mqd_t mq_open(const char *name, int oflag);
mqd_t mq_open(const char *name, int oflag, mode_t mode,
struct mq_attr *attr);

Link with -lrt.

读书人网 >UNIXLINUX

热点推荐