gcc编译错误,g++则没问题
- gre_linewer
- 帖子: 180
- 注册时间: 2006-11-22 10:57
gcc编译错误,g++则没问题
下面是关于消息队列的程序,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;
}
$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
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
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
- gre_linewer
- 帖子: 180
- 注册时间: 2006-11-22 10:57
在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++对此处理不一样呢
#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
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA
-
- 帖子: 849
- 注册时间: 2006-09-07 22:51
- 联系:
-
- 帖子: 353
- 注册时间: 2007-04-03 9:52
- 联系:
-
- 帖子: 353
- 注册时间: 2007-04-03 9:52
- 联系:
好像是要用这个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
回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
另外,#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
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
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
对上了
#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
IBM R52-18465DC
CPU:PM 1.73Ghz DDR2:768MB 533Mhz
INTEL 915GM/915PM
ATI Mobility Radeon X300 64MB
40G SATA