gpt4 book ai didi

c - 将函数移动到自己的文件时出现多个错误

转载 作者:太空宇宙 更新时间:2023-11-04 03:33:50 25 4
gpt4 key购买 nike

所以我有这个主要功能,bank1.c:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<signal.h>
#include<sys/stat.h>
#include"receive_message.h"

int desks = 10;
int N = 2;
int running = 0;
pthread_t tid[10];
int queue[10];
int balance = 20;
pthread_mutex_t lock;

void* getMessage(void *arg)
{
int i;
char* message = malloc(sizeof(char)*30);
char** m_point = &message;
key_t key;
pthread_mutex_lock(&lock);
pthread_t id = pthread_self();
pid_t pid;
int j = 10;

for(i = 0; i < N; i++)
{
if(pthread_equal(id, tid[i]))
{
key = 10 + i * 5;
printf("\n Thread %d processing\n", i);
m_point = receive_message(key, m_point);
printf(" Message received: %s\n", *m_point);
}
}
free(message);

pthread_mutex_unlock(&lock);
running = running - 1;
return NULL;

}

int main(void)
{
int i = 0;
int err;

if (pthread_mutex_init(&lock, NULL) != 0)
{
printf("\n mutex init failed\n");
return 1;
}

while(i < N)
{
running = running + 1;
pthread_create(&(tid[i]), NULL, &getMessage, NULL);
i++;

}


while(running != 0)
{
//sleep(2);
}
printf(" RUN OK\n");
return 0;
}

我意识到我需要在其他文件中使用函数“receive_message”,所以我尝试从中创建一个单独的文件 receive_message.c:

#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<signal.h>
#include<sys/stat.h>

#define MSGSZ 128

struct msgbuf {
long mtype;
char mtext[200];
};

char** receive_message(key_t key, char** ret)
{

int msqid;
msqid = msgget((key_t) key, 0600 | IPC_CREAT);
struct msqid_ds buf;
struct msgbuf m_buf;

int rc = msgctl(msqid, IPC_STAT, &buf);
int msg = (uint)(buf.msg_qnum);


if (msg != 0){

if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */
perror("msgget");
return ret;
}

if (msgrcv(msqid, &m_buf, sizeof m_buf.mtext, 0, 0) == -1) {
perror("msgrcv");
return ret;
}
}

strcpy(*ret, m_buf.mtext);
return ret;

}

有了这个头文件,receive_message.h:

#ifndef RECEIVE_MESSAGE_H_INCLUDED
#define RECEIVE_MESSAGE_H_INCLUDED

struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[200]; /* message data */
};

char** receive_message(key_t key, char** ret)

#endif

现在,当我尝试将其编译为 gcc bank1.c receive_message.c -o bank1 -lpthread 时,我遇到了这些错误,我无法从中得出正面或反面:

bank1.c: In function ‘receive_message’:
bank1.c:15:1: error: parameter ‘desks’ is initialized
int desks = 10;
^
bank1.c:16:1: error: parameter ‘N’ is initialized
int N = 2;
^
bank1.c:17:1: error: parameter ‘running’ is initialized
int running = 0;
^
bank1.c:20:1: error: parameter ‘balance’ is initialized
int balance = 20;
^
bank1.c:24:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
bank1.c:55:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
{
^
In file included from bank1.c:11:0:
receive_message.h:9:8: error: old-style parameter declarations in prototyped function definition
char** receive_message(key_t key, char** ret)
^
bank1.c:87:1: error: expected ‘{’ at end of input
}
^

我假设错误出在头文件中,但我不确定。当来自 receive_message.c 的代码在 bank1.c 中时,文件按预期运行

最佳答案

很遗憾,我无法发表评论,所以这里是对 Michiel 已经指出的内容的补充:

确保只定义一次struct msgbuf。现在它在 header 和您的 .c 文件中定义。此外,您似乎无论如何都需要在 .c 文件中使用它,因此您可以在 .c 文件中将其声明为 static 而不是在头文件中公开!

关于c - 将函数移动到自己的文件时出现多个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34093439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com