sherf 发表于 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的,但不知道为什么不行?

sherf 发表于 2009-05-29 03:35

回复 #1 sherf 的帖子

先顶一下

sherf 发表于 2009-05-29 10:15

回复 #1 sherf 的帖子

有人知道吗?

sherf 发表于 2009-05-30 11:16

回复 #1 sherf 的帖子

等待中.......

sherf 发表于 2009-06-01 13:50

回复 #1 sherf 的帖子

各位,

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

windaoo 发表于 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__ 呢?

sherf 发表于 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;};

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;
struct string empty={0,""};

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

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

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;
{struct string *t = (struct string *)malloc(sizeof(int)+n);
int i;
t->length=n;
for(i=0;i<n;i++) t->chars=s->chars;
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=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_的介绍倒是不少)。因此,想请教一下各位,碰到这个问题,有什么好的解决办法。

windaoo 发表于 2009-06-01 15:25

getchar() 在 stdio.h 里声明为 int getchar(), 这里重新搞一个与它不同的的确会出问题
但不能通过 #undef getchar() 来解决,因为 getchar() 在 stdio.h 里声明为一个函数而不是宏
改个函数名不行吗?比如
Getchar() 之类

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

sherf 发表于 2009-06-01 15:40

回复 #8 windaoo 的帖子

windaoo ,

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

windaoo 发表于 2009-06-01 16:05

非常怀疑这个程序是不是那本书用来测试编译器排错功能的示例程序

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


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


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


怀疑,是不是论坛把 “” 吃了?
页: [1] 2
查看完整版本: 编译问题