免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3199 | 回复: 14
打印 上一主题 下一主题

编译问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-29 03:33 |只看该作者 |倒序浏览
各位,请教一下,有关虎书上 runtime.c的编译问题。

我在linux在用 cc -c -g runtime.c 进行编译,但出现错误如下:

runtime.c:1:8 :  warning : undefining  "__STDC__"  
In file included   from  /usr/include/features.h : 291,
                          from /usr/include/stdio.h:28
                          from runtime.c :2:

/usr/include/sys/cdefs.h:31:3: #error  "You need a ISO C conforming compiler t use the glibc headers"


本人用的是ANSI-C的编译器,__STDC__是为1的,按道理应该可以undef的,但不知道为什么不行?

论坛徽章:
0
2 [报告]
发表于 2009-05-29 03:35 |只看该作者

回复 #1 sherf 的帖子

先顶一下

论坛徽章:
0
3 [报告]
发表于 2009-05-29 10:15 |只看该作者

回复 #1 sherf 的帖子

有人知道吗?

论坛徽章:
0
4 [报告]
发表于 2009-05-30 11:16 |只看该作者

回复 #1 sherf 的帖子

等待中.......

论坛徽章:
0
5 [报告]
发表于 2009-06-01 13:50 |只看该作者

回复 #1 sherf 的帖子

各位,

你们在虎书例子调试中都没有碰到这个问题?.............

论坛徽章:
0
6 [报告]
发表于 2009-06-01 14:23 |只看该作者
/usr/include/sys/cdefs.h:31:3: #error  "You need a ISO C conforming compiler t use the glibc headers"


引自 /usr/include/sys/cdefs:
/* The GNU libc does not support any K&R compilers or the traditional mode
   of ISO C compilers anymore.  Check for some of the combinations not
   anymore supported.  */
#if defined __GNUC__ && !defined __STDC__
# error "You need a ISO C conforming compiler to use the glibc headers"
#endif


如果是想用 glibc 但编译器不是 stdc 的或者把 __STDC__ undefine 了就没法继续

还没看这些书,
把 __GNUC__ 也 undef 了试试?
不过为啥要 undef __STDC__ 呢?

论坛徽章:
0
7 [报告]
发表于 2009-06-01 14:47 |只看该作者

回复 #6 windaoo 的帖子

runtime.c 的源程序如下:

#undef __STDC__
#include <stdio.h>


int *initArray(int size, int init)
{int i;
int *a = (int *)malloc(size*sizeof(int));
for(i=0;i<size;i++) a=init;
return a;
}

int *allocRecord(int size)
{int i;
int *p, *a;
p = a = (int *)malloc(size);
for(i=0;i<size;i+=sizeof(int)) *p++ = 0;
return a;
}

struct string {int length; unsigned char chars[1];};

int stringEqual(struct string *s, struct string *t)
{int i;
if (s==t) return 1;
if (s->length!=t->length) return 0;
for(i=0;i<s->length;i++) if (s->chars!=t->chars) return 0;
return 1;
}

void print(struct string *s)
{int i; unsigned char *p=s->chars;
for(i=0;i<s->length;i++,p++) putchar(*p);
}

void flush()
{
fflush(stdout);
}

struct string consts[256];
struct string empty={0,""};

int main()
{int i;
for(i=0;i<256;i++)
   {consts.length=1;
    consts.chars[0]=i;
   }
return tigermain(0 /* static link */);
}

int ord(struct string *s)
{
if (s->length==0) return -1;
else return s->chars[0];
}

struct string *chr(int i)
{
if (i<0 || i>=256)
   {printf("chr(%d) out of range\n",i); exit(1);}
return consts+i;
}

int size(struct string *s)
{
return s->length;
}

struct string *substring(struct string *s, int first, int n)
{
if (first<0 || first+n>s->length)
   {printf("substring([%d],%d,%d) out of range\n",s->length,first,n);
    exit(1);}
if (n==1) return consts+s->chars[first];
{struct string *t = (struct string *)malloc(sizeof(int)+n);
  int i;
  t->length=n;
  for(i=0;i<n;i++) t->chars=s->chars[first+i];
  return t;
}
}

struct string *concat(struct string *a, struct string *b)
{
if (a->length==0) return b;
else if (b->length==0) return a;
else {int i, n=a->length+b->length;
       struct string *t = (struct string *)malloc(sizeof(int)+n);
       t->length=n;
       for (i=0;i<a->length;i++)
         t->chars=a->chars;
       for(i=0;i<b->length;i++)
         t->chars[i+a->length]=b->chars;
       return t;
     }
}

int not(int i)
{ return !i;
}

#undef getchar

struct string *getchar()
{int i=getc(stdin);
if (i==EOF) return &empty;
else return consts+i;
}


========================================

windaoo ,

我曾经尝试将 “#undef __STDC__ ” 这句去掉,然后编译,但结果提示函数 getchar 重复定义。

另外,runtime.c 是虎书里面自带的程序,我是假定它本身没有问题,因此当单独编译这个程序出错时,我感到很意外。同时,我也到网上查询了一下,但有没有太多实际的解决方法(对_STDC_的介绍倒是不少)。因此,想请教一下各位,碰到这个问题,有什么好的解决办法。

论坛徽章:
0
8 [报告]
发表于 2009-06-01 15:25 |只看该作者
getchar() 在 stdio.h 里声明为 int getchar(), 这里重新搞一个与它不同的的确会出问题
但不能通过 #undef getchar() 来解决,因为 getchar() 在 stdio.h 里声明为一个函数而不是宏
改个函数名不行吗?比如
Getchar() 之类

另外这个程序里有不少错误,确定抄对了吗:)

论坛徽章:
0
9 [报告]
发表于 2009-06-01 15:40 |只看该作者

回复 #8 windaoo 的帖子

windaoo ,

经与虎书网站上的程序再次匹对,应该没错......

论坛徽章:
0
10 [报告]
发表于 2009-06-01 16:05 |只看该作者
非常怀疑这个程序是不是那本书用来测试编译器排错功能的示例程序

这段代码中:
initArray:
malloc: 不包含 stdlib.h 编译器会警告
int *a = int init; 不但类型不匹配而且会造成内存泄露


struct string {int length; unsigned char chars[1];};
struct string consts[256];
main():
for(i=0;i<256;i++)
   {consts.length=1;   
    consts.chars[0]=i;  这行和上面的一行不合逻辑而且有语法错误
}


还有其它地方也有错误
不列举了


怀疑,是不是论坛把 “” 吃了?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP