LinuxSir.cn,穿越时空的Linuxsir!

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

带attribute的程序编译不过去

[复制链接]
发表于 2008-5-27 18:02:13 | 显示全部楼层 |阅读模式
#include <stdio.h>
void print() __attribute__((__noreturn__));
void print()
{
        printf("hello world");
}

struct my_struct __attribute__((__packed__))
{
        char c;
        int  i;
};
int main()
{
        struct my_struct a;
        a.c= 1;
        a.i =2;
        printf("size is %d\n", sizeof(a));
        print();
        return 0;
}

[souldump@localhost ~]$ gcc -Wall -o demo demo.c
demo.c: In function ‘print’:
demo.c:6: 警告:‘noreturn’ 函数确实会返回
demo.c: 在顶层:
demo.c:9: 错误:expected identifier or ‘(’ before ‘{’ token
demo.c: In function ‘main’:
demo.c:15: 错误:‘a’ 的存储大小未知
demo.c:15: 警告:未使用的变量 ‘a’

谁帮个忙说下啊,很久前研究过,可惜全忘记了。
发表于 2008-6-10 23:23:47 | 显示全部楼层
struct my_struct __attribute__((__packed__))
{
    char c;
    int i;
};
应该改为:
struct my_struct
{
        char c;
        int i;
} __attribute__((__packet__));

至于noreturn, 确实不知道哪儿错了.
回复 支持 反对

使用道具 举报

发表于 2008-6-11 08:57:17 | 显示全部楼层
noreturn 没有错吧, warning 而已
回复 支持 反对

使用道具 举报

发表于 2008-6-11 14:58:07 | 显示全部楼层
想消除这个warning,就在函数print里加上exit(1),其实本身也没有错。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-6-13 17:59:14 | 显示全部楼层
多谢了,但是有个问题,会出现段错误:
[duxiaoyu@hnchen ~]$ cat me.c
#include <stdio.h>
void print() __attribute__((__noreturn__));
void print()
{
printf("hello world\n");
}

struct my_struct
{
char c;
int i;
} __attribute__((__packed__));
int main()
{
struct my_struct a;
a.c= 1;
a.i =2;
printf("size is %d\n", sizeof(a));
print();
return 0;
}

[duxiaoyu@hnchen ~]$ gcc -o me me.c
me.c: In function ‘print’:
me.c:6: 警告:‘noreturn’ 函数确实会返回
[duxiaoyu@hnchen ~]$ ./me
size is 5
hello world
段错误
[duxiaoyu@hnchen ~]$


但这样没事:
[duxiaoyu@hnchen ~]$ cat me.c
#include <stdio.h>
void print() __attribute__((__noreturn__));
void print()
{
printf("hello world\n");
}

struct my_struct
{
char c;
int i;
} __attribute__((__packed__));
int main()
{
struct my_struct a;
a.c= 1;
a.i =2;
printf("size is %d\n", sizeof(a));
//print();
printf("hello world\n");
return 0;
}

[duxiaoyu@hnchen ~]$ gcc -o me me.c
me.c: In function ‘print’:
me.c:6: 警告:‘noreturn’ 函数确实会返回
[duxiaoyu@hnchen ~]$ ./me
size is 5
hello world
[duxiaoyu@hnchen ~]$

难道返回就出错了,高手指教了。
回复 支持 反对

使用道具 举报

发表于 2008-6-24 17:09:57 | 显示全部楼层
你的函数明明要返回,为什么要写noreturn属性?编译知道你不返回了,就不考虑返回后的动作,没有生成代码,结果无法预测。
BTW:这里人气很不旺
回复 支持 反对

使用道具 举报

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

本版积分规则

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