gcc编译错误,g++则没问题

软件和网站开发以及相关技术探讨
回复
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

gcc编译错误,g++则没问题

#1

帖子 gre_linewer » 2007-05-13 9:58

下面是关于消息队列的程序,gcc编译错误,g++则没问题.
$gcc private.c
private.c: In function 'main':
private.c:30: error: invalid application of 'sizeof' to incomplete type 'struct msgbuf'
private.c:31: error: dereferencing pointer to incomplete type
private.c:32: error: dereferencing pointer to incomplete type
private.c:35: error: dereferencing pointer to incomplete type
private.c:45: error: invalid application of 'sizeof' to incomplete type 'struct msgbuf'
private.c:52: error: dereferencing pointer to incomplete type
private.c:52: error: dereferencing pointer to incomplete type
怎么会是这样?msg.h里定义了msgbuf的。。。。。。。。。。。。。。。。。。。
////////////////////////////////////////////////////以下是源程序///////////////////////////////////////////////////////////////////
/* File: private.c */
#include <sys/msg.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
// #include <unistd.h>
// #include <ctype.h>
// #include <netinet/in.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;
}
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#2

帖子 wkt » 2007-05-13 10:12

自己定义一个跟 struct msgbuf
一样的结构就行了!!
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#3

帖子 gre_linewer » 2007-05-13 11:08

这样当然可以。
不过,msg.h里不是定义过了么?
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#4

帖子 wkt » 2007-05-13 12:30

gre_linewer 写了:这样当然可以。
不过,msg.h里不是定义过了么?
msg.h
不有
#ifndef xxxx
something
#endif
之类的吗?
谁知道最终是什么??
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#5

帖子 gre_linewer » 2007-05-13 17:10

在msg.h有
#ifdef __USE_GNU
/* Template for struct to be used as argument for `msgsnd' and `msgrcv'. */
struct msgbuf
{
long int mtype; /* type of received/sent message */
char mtext[1]; /* text of the message */
};
#endif
为啥gcc和g++对此处理不一样呢
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
wkt
帖子: 849
注册时间: 2006-09-07 22:51
联系:

#6

帖子 wkt » 2007-05-13 17:30

gre_linewer 写了:在msg.h有
#ifdef __USE_GNU
/* Template for struct to be used as argument for `msgsnd' and `msgrcv'. */
struct msgbuf
{
long int mtype; /* type of received/sent message */
char mtext[1]; /* text of the message */
};
#endif
为啥gcc和g++对此处理不一样呢
这我就不知道了
头像
titainium
帖子: 689
注册时间: 2006-12-02 12:25

#7

帖子 titainium » 2007-05-14 8:58

你需要在你的源代码里面定义__USE_GNU。
Titainium
antonym55
帖子: 353
注册时间: 2007-04-03 9:52
联系:

#8

帖子 antonym55 » 2007-05-14 10:27

antonym55
帖子: 353
注册时间: 2007-04-03 9:52
联系:

#9

帖子 antonym55 » 2007-05-14 12:37

titainium 写了:你需要在你的源代码里面定义__USE_GNU。
好像是要用这个

代码: 全选

#define _GNU_SOURCE
有种说法是:
/usr/include/features.h文件中双下划线开头的那些宏应该程序是不应该去define的,

只能define单下划线开头的那些,由系统的头文件(如features.h)去扩展。

GNU 的文档, 中建议用 _GNU_SOURCE,

http://www.gnu.org/software/libc/manual ... acros.html

目前gcc 与g++的差异还没找到
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#10

帖子 gre_linewer » 2007-05-15 8:48

回7楼的,我刚开始就试过了,不行.
另外,#define _GNU_SOURCE要在#include<sys/msg.h>之前才行.
增加了之后,
dolphin9@debian-gre:~/Program/Linux程序设计权威指南/system/message$ gcc -o 1 private.c
dolphin9@debian-gre:~/Program/Linux程序设计权威指南/system/message$ g++ -o 2 private.c
private.c:4:1: warning: "_GNU_SOURCE" redefined
<command line>:1:1: warning: this is the location of the previous definition
MS g++已经帮define _GNU_SOURCE了:不加没警告,家了反而有了
谢谢各位.尤其是ls的兄弟^ ^P
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
头像
gre_linewer
帖子: 180
注册时间: 2006-11-22 10:57

#11

帖子 gre_linewer » 2007-05-15 9:16

features.h中有:
#ifdef _GNU_SOURCE
# define __USE_GNU 1
#endif
这样和msg.h中
#ifdef __USE_GNU
/* Template for struct to be used as argument for `msgsnd' and `msgrcv'. */
struct msgbuf
{
long int mtype; /* type of received/sent message */
char mtext[1]; /* text of the message */
};
#endif
对上了
Ubuntu 8.04
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
回复