我弄了半天编译通过了,结果被楼上的抢先说完了……
呵呵,楼上说的很有道理
我这样编译:
gcc calculator.c -o calculator
通过并得到可执行文件calculator
然后学楼主
gcc calculator.c calculator
注意此时已经有了一个可执行文件calculator在,不然会提示文件找不到
就出现了一堆提示:
collect2: ld terminated with signal 11 [¶Î´íÎó]
test(.text+0x31e): In function `getop':
: multiple definition of `getop'
/tmp/cc6erU6r.o(.text+0x26e): first defined here
test(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.rodata+0x0): first defined here
test(.data+0x4): In function `__data_start':
: multiple definition of `__dso_handle'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/crtbegin.o(.data+0x0): first defined here
test(.init+0x0): In function `_init':
: multiple definition of `_init'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crti.o(.init+0x0): first defined here
test(.text+0x43e): In function `getch':
: multiple definition of `getch'
/tmp/cc6erU6r.o(.text+0x38e): first defined here
test(.text+0x0): In function `_start':
: multiple definition of `_start'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.text+0x0): first defined here
test(.text+0x2d4): In function `pop':
: multiple definition of `pop'
/tmp/cc6erU6r.o(.text+0x224): first defined here
test(.bss+0x8): multiple definition of `bufp'
/tmp/cc6erU6r.o(.bss+0x4): first defined here
test(.text+0xb0): In function `main':
: multiple definition of `main'
/tmp/cc6erU6r.o(.text+0x0): first defined here
test(.text+0x472): In function `ungetch':
: multiple definition of `ungetch'
/tmp/cc6erU6r.o(.text+0x3c2): first defined here
test(.fini+0x0): In function `_fini':
: multiple definition of `_fini'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crti.o(.fini+0x0): first defined here
test(.bss+0x4): multiple definition of `sp'
/tmp/cc6erU6r.o(.bss+0x0): first defined here
test(.got+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.got.plt+0x0): first defined here
test(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.rodata+0x4): first defined here
test(.data+0x0): In function `__data_start':
: multiple definition of `__data_start'
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.data+0x0): first defined here
test(.text+0x27e): In function `push':
: multiple definition of `push'
/tmp/cc6erU6r.o(.text+0x1ce): first defined here
/usr/lib/gcc-lib/i386-redhat-linux/3.3.2/../../../crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
test(.dynamic+0x0): first defined here
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
return 0;
}
#define MAXVAL 100 /* maximum depth of val stack */
int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */
/* push: push f onto value stack */
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
}
/* pop: pop and return top value from stack */
double pop(void)
{
if (sp > 0)
return val[--sp];
else {
printf("error: stack empty\n");
return 0.0;
}
}
#include <ctype.h>
int getch(void);
void ungetch(int);
/* getop: get next character or numeric operand */
int getop(char s[])
{
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
i = 0;
if (isdigit(c)) /* collect integer part */
while (isdigit(s[++i] = c = getch()))
;
if (c == '.') /* collect fraction part */
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
我把它除注释外改得和K&R的一模一样,可是... ...天啊!
这是报错:
[root@localhost workbench]# gcc calculator.c caltor
caltor(.text+0x2b8): In function `getop':
: multiple definition of `getop'
/tmp/ccQVfz90.o(.text+0x23a): first defined here
/usr/bin/ld: Warning: size of symbol `getop' changed from 292 to 285
in caltor
caltor(.rodata+0x0): multiple definition of `_fp_hw'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.rodata+0x0):../sysdeps/i386/elf/start.S:47: first defined here
caltor(.data+0x4): In function `__data_start':
: multiple definition of `__dso_handle'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/crtbegin.o(.data+0x0): first defined here
caltor(.init+0x0): In function `_init':
/usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:35: multiple definition of `_init'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o(.init+0x0):/usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:12: first defined here
caltor(.text+0x3d5): In function `getch':
: multiple definition of `getch'
/tmp/ccQVfz90.o(.text+0x35e): first defined here
caltor(.text+0x0): In function `_start':
../sysdeps/i386/elf/start.S:47: multiple definition of `_start'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.text+0x0):../sysdeps/i386/elf/start.S:47: first defined here
caltor(.text+0x26e): In function `pop':
: multiple definition of `pop'
/tmp/ccQVfz90.o(.text+0x1f0): first defined here
caltor(.data+0x10): In function `__data_start':
: multiple definition of `bufp'
/tmp/ccQVfz90.o(.data+0x4): first defined here
caltor(.text+0xb0): In function `main':
: multiple definition of `main'
/tmp/ccQVfz90.o(.text+0x0): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 410 to 360 in caltor
caltor(.text+0x409): In function `ungetch':
: multiple definition of `ungetch'
/tmp/ccQVfz90.o(.text+0x392): first defined here
caltor(.fini+0x0): In function `_fini':
/usr/src/build/231499-i386/BUILD/glibc-2.3.2-20030313/build-i386-linux/csu/crti.S:51: multiple definition of `_fini'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crti.o(.fini+0x0):
first defined here
caltor(.data+0xc): In function `__data_start':
: multiple definition of `sp'
/tmp/ccQVfz90.o(.data+0x0): first defined here
caltor(.got+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.got.plt+0x0):../sysdeps/i386/elf/start.S:47: first defined here
caltor(.rodata+0x4): multiple definition of `_IO_stdin_used'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.rodata+0x4):../sysdeps/i386/elf/start.S:53: first defined here
caltor(.data+0x0): In function `__data_start':
: multiple definition of `__data_start'
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.data+0x0):../sysdeps/i386/elf/start.S:47: first defined here
caltor(.text+0x218): In function `push':
: multiple definition of `push'
/tmp/ccQVfz90.o(.text+0x19a): first defined here
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/../../../crt1.o(.dynamic+0x0):../sysdeps/i386/elf/start.S:47: multiple definition of `_DYNAMIC'
caltor(.dynamic+0x0): first defined here
collect2: ld returned 1 exit status