c - 如何在 makefile 中使用 POSIX 消息队列名称

标签 c linux makefile

我有一个与操作系统类(class)相关的作业。在本作业中,将实现两个程序:一个名为“get”的客户端程序和一个名为“iserv”的服务器程序。服务器将是一个多进程程序。 POSIX 消息队列将用于进程间通信。服务器将按如下方式启动: 伊塞夫 这里, 是服务器要创建的消息队列的名称。是包含整数的文本文件的名称。

我的iserv.c程序:(未完成)

#include <linux/types.h>
#include <linux/unistd.h>
#include <stdio.h>
#include <mqueue.h>
#include <stdlib.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/errno.h>
#include <linux/wait.h>
#include <msg.h>

int main(int argc , char *argv[])
{
pid_t apid1;
FILE *fp;
mqd_t mq;
const char *msgqueue = "/serverqueue";
int oflag = (O_RDWR|O_CREAT);
mode_t mode = (S_IRUSR|S_IWUSR);
struct mq_attr *attr = NULL;

if(argc != 1)
{
    printf("wrong number of arguments");
    exit(1);
}
//create message queue
mq = mq_open(msgqueue ,oflag , mode , attr);
if(mq==-1)
{
    perror("can not open msg queue\n");
    exit(1);
}
printf("mq opened , mq id = %d\n" , (int) mq);

//create child process
apid1 = fork();
if(apid1 < 0)
{
    perror("main():");
    exit(1);
}
if(apid1 == 0)
{
    printf("this is the first child, pid = %d\n", (int) getpid());
    fflush(stdout);
}
}

正如代码所示,消息队列是使用名称“serverqueue”创建的。然后,我创建了我的 makefile:

iserv:iserv.c
     gcc -o iserv serverqueue infile.txt -lrt
clean:
     rm -r *.o iserv

当我使用 make 命令运行此 makefile 时,出现 serverqueue: No such file or directory 错误。 服务器将在哪里以及如何使用 iserv 行启动?哪里错了?

请帮帮我,谢谢!

最佳答案

你的 makefile 对我来说毫无意义。 -o 指定编译输出文件的名称。 如果您的源文件名为 iserv.c 并且您希望将可执行文件命名为 serverqueue,请编译

gcc -o serverqueue iserv.c -lrt

在 makefile 中,您忘记了源文件的 .c 扩展名。

我不明白您包含 infile.txt 的原因。

根据您的评论进行编辑:

In homework , it is wanted that server will be started as follows: iserv . servq is the name of a message queueto be created by the server and it is created as server queue in the code if I am true besides that inputfile is the name of a text file that will contain a large number of positive integers.

不,声明 iserv <servq> <inputfile>与编译无关。这就是程序编译后应如何使用该程序。该程序的名称是 iserv,因此您必须像这样编译代码

gcc -o iserv iserv.c -lrt

要启动您的程序,请输入

./iserv <servq> <inputfile>

哪里<servq>是你的服务器队列和 <inputfile>将是 infile.txt。

关于c - 如何在 makefile 中使用 POSIX 消息队列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453326/

相关文章:

linux - rm 最后有 & 符号和没有

c - 使用 makefile 链接 C 程序时出错

c - 如何在window mingw中使用CoreFoundation.dll编译我的程序

linux - 如何查看ELF文件中包含的函数名和参数?

c - 用作计数器的整数值的意外输出

c - 结构程序中的结构无法启动

c - 在 fwrite 之前修剪 buffer[] 中未使用的项目

C 可变参数函数 : How to specify which type to give to va_arg

python - 在 linux mint 上制作安装脚本

ruby - 执行 HoneyC 时出错 - `require' : cannot load such file