我正在尝试在MINIX3上运行一个C/C++应用程序,该应用程序应该使用msgsnd()和msgget()在两个进程之间发送消息
这是我得到的错误:
send.cpp:(.text+0x7f): undefined reference to `msgget'
send.cpp:(.text+0x1c1): undefined reference to `msgsnd'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我正在使用clang++编译代码:
clang++ send.cpp -o send.out
这是send.cpp代码:
#include <lib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MSGSZ 128
/*
* Declare the message structure.
*/
typedef struct msgbufer {
long mtype;
char mtext[MSGSZ];
} message_buf;
int main()
{
int msqid;
int msgflg = IPC_CREAT | 0666;
key_t key;
message_buf sbuf;
size_t buf_length;
/*
* Get the message queue id for the
* "name" 1234, which was created by
* the server.
*/
key = 1234;
(void)fprintf(stderr, "nmsgget: Calling msgget(%i,
%#o)n",
key, msgflg);
if ((msqid = msgget(key, msgflg)) < 0) {
perror("msgget");
exit(1);
}
else
(void)fprintf(stderr, "msgget: msgget succeeded: msqid = %dn", msqid);
/*
* We'll send message type 1
*/
sbuf.mtype = 1;
(void)fprintf(stderr, "msgget: msgget succeeded: msqid = %dn", msqid);
(void)strcpy(sbuf.mtext, "Hello other process 2.");
(void)fprintf(stderr, "msgget: msgget succeeded: msqid = %dn", msqid);
buf_length = strlen(sbuf.mtext) + 1;
/*
* Send a message.
*/
if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) {
printf("%d, %li, %s, %lun", msqid, sbuf.mtype, sbuf.mtext, buf_length);
perror("msgsnd");
exit(1);
}
else
printf("Message: "%s" Sentn", sbuf.mtext);
exit(0);
}
您没有链接到包含msgsnd
和msgget
函数的库,因此链接器步骤失败。我不熟悉Minix,所以我不确定库存储在哪里,也不知道它叫什么。基本上,您需要在链接步骤中添加-l<msg>
标志。其中<msg>
是包含实现的库的名称。