LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 11671|回复: 4

碰到“dereferencing pointer to incomplete type”错误

[复制链接]
发表于 2005-4-24 20:09:04 | 显示全部楼层 |阅读模式
编译《linux程序设计权威》指南时,碰到“dereferencing pointer to incomplete type”错误提示。是少包含了头文件吗?请指点。谢谢!

编译方法:gcc -o private private.c
源码为:(private.c)

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

int main(int argc, char* argv[])
{
        int queue_id;
        struct msgbuf *msg;
        struct msgbuf *recv_msg;
        int rc;

        //建立消息队列
        queue_id = msgget(IPC_PRIVATE, IPC_CREAT | 0600);
        if (queue_id == -1) {
                perror("main: msgget");
                exit(1);
        }
        printf("message queue created, queue id '%d'.\n", queue_id);

        //创建消息结构
        msg = (struct msgbuf*)malloc(sizeof(struct msgbuf *)
                +strlen("hello world"));
        msg->mtype = 1;  //<-------------这里出错

        strcpy(msg->mtext, "hello world");//<-------------这里出错

        //发送消息
        rc = msgsnd(queue_id, msg, strlen(msg->mtext)+1, 0);//<-------------这里出错
        if (rc == -1) {
                perror("main: msgsnd");
                exit(1);
        }
        free(msg);
        printf("message placed on the queue successfully.\n");

        //接收消息
        recv_msg = (struct msgbuf*)malloc(sizeof(struct msgbuf)
                +strlen("hello world"));
        rc = msgrcv(queue_id, recv_msg, strlen("hello world")+1, 0, 0);
        if (rc == -1) {
                perror("main: msgrcv");
                exit(1);
        }
        printf("msgrcv: received message: mtype '%d'; mtext '%s'\n",
                recv_msg->mtype, recv_msg->mtext);      //<-------------这里出错

        return 0;
}
发表于 2005-4-24 20:28:30 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2005-4-25 23:29:36 | 显示全部楼层


链接指向的贴子的回复是错的!绝对不应该:
#define __USE_GNU
而应该是:
#define _GNU_SOURCE
具体请查看/usr/include/features.h文件。双下划线开头的那些宏应该程序是不应该去define的,只能define单下划线开头的那些,由系统的头文件(如features.h)去扩展。
回复 支持 反对

使用道具 举报

发表于 2005-4-26 21:45:29 | 显示全部楼层
Post by Tetris
链接指向的贴子的回复是错的!绝对不应该:
#define __USE_GNU
而应该是:
#define _GNU_SOURCE
具体请查看/usr/include/features.h文件。双下划线开头的那些宏应该程序是不应该去define的,只能define单下划线开头的那些,由系统的头文件(如features.h)去扩展。

呵呵,我还不知道呢,多谢指教!
回复 支持 反对

使用道具 举报

发表于 2009-3-29 19:05:26 | 显示全部楼层

-----

3楼说的对极了,我刚才验证是这样的。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表