高分求解:GCC编译问题:谁之过?

cstyphoon 2005-10-05 10:27:17
K&R 的一个例子程序,Linux gcc 环境,在/workbench目录下,写好之后无论如何编译通不过。报错信息如下所附。把代码检查了几十次,和原文一字一字地对照还是不行。于是把源文件拷到另一个目录下(/root)编译就不报错,顺利通过并按预期运行了,这是怎么回事啊?真能把人给逼疯了!是我出了问题还是编译器出了问题啊??????
...全文
1305 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cstyphoon 2005-10-07
  • 打赏
  • 举报
回复
没错,愿意是想执行命令gcc calculator -o caltor 的。只顾了代码,却没有注意在shell里的命令。因为我是用的历史命令,用方向键找出最近一个打头是gcc的命令然后回车,压根没注意是在哪一次起把命令给输错了!

我又那样去编了一次,同样的结果。去编译gcc输出的可执行文件,当然要死了。糊涂啊!

谢谢兄弟们哈!
K 2005-10-06
  • 打赏
  • 举报
回复
参数错误
yzx1983 2005-10-05
  • 打赏
  • 举报
回复
我弄了半天编译通过了,结果被楼上的抢先说完了……
呵呵,楼上说的很有道理
我这样编译:
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

算是重现了问题吧^_^
yzx1983 2005-10-05
  • 打赏
  • 举报
回复
楼主,gcc calculator.c caltor
这个后面的caltor是什么东东?
antijpn 2005-10-05
  • 打赏
  • 举报
回复
你的命令什么意思?gcc calculator.c caltor里面的caltor是什么?表面上看来已经是已经链接好的执行文件,所以里面会有那么多的符号,这样的话,应该caltor可以执行才对……

猜测按照你的意思caltor应该是输出的执行文件,如果我猜得没错,实际上应该使用的命令是:
gcc -o caltor calculator.c
cstyphoon 2005-10-05
  • 打赏
  • 举报
回复
这里是源程序
#include <stdio.h>
#include <stdlib.h> /* for atof() */

#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */

int getop(char []);
void push(double);
double pop(void);

/* reverse Polish calculator */
main()
{
int type;
double op2;
char s[MAXOP];

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的一模一样,可是... ...天啊!
cstyphoon 2005-10-05
  • 打赏
  • 举报
回复
这是报错:
[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

到这里我就该晕了!

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧