【问题标题】:C program compiling errorC程序编译错误
【发布时间】:2011-06-07 12:47:47
【问题描述】:

我正在尝试编译和安装一个 1997 年创建的程序。我正在使用 gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50) 和 CentOS 版本 5.5 (Final)。在程序的 SOURCE 目录中尝试执行“make”命令时,出现以下错误:

gcc -g -w -I/home/shahw/opinionfinder/software/scol1k/objs -I. -DDEBUG -DUNIX    dump.c  -L/home/shahw/opinionfinder/software/scol1k/objs -lscol -lm -o dump
gcc -g -w -I/home/shahw/opinionfinder/software/scol1k/objs -I. -DDEBUG -DUNIX    ngram.c  -L/home/shahw/opinionfinder/software/scol1k/objs -lscol -lm -o ngram
gcc -g -w -I/home/shahw/opinionfinder/software/scol1k/objs -I. -DDEBUG -DUNIX    reg.c  -L/home/shahw/opinionfinder/software/scol1k/objs -lscol -lm -o reg
gcc -g -w -I/home/shahw/opinionfinder/software/scol1k/objs -I. -DDEBUG -DUNIX    select.c  -L/home/shahw/opinionfinder/software/scol1k/objs -lscol -lm -o select 
select.c: In function ‘select_lines’:
select.c:84: error: invalid lvalue in increment
make[1]: *** [select] Error 1
make[1]: Leaving directory `/home/shahw/opinionfinder/software/scol1k/tools'
make: *** [modules] Error 2

在最初认为这是 c 代码错误后,我尝试在 Mac OSX 10.6.7 上使用 i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1(Apple Inc. build 5664)编译它.这次我在最初的错误之后又出现了一个错误,这意味着肯定与正在运行的 gcc 版本不兼容。这次的错误是:

gcc -g -w -I/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -I. -DDEBUG -DUNIX    dump.c  -L/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -lscol -lm -o dump
gcc -g -w -I/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -I. -DDEBUG -DUNIX    ngram.c  -L/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -lscol -lm -o ngram
gcc -g -w -I/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -I. -DDEBUG -DUNIX    reg.c  -L/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -lscol -lm -o reg
gcc -g -w -I/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -I. -DDEBUG -DUNIX    select.c  -L/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -lscol -lm -o select
gcc -g -w -I/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -I. -DDEBUG -DUNIX    sents.c  -L/Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs -lscol -lm -o sents
ld: duplicate symbol _Bos in /Users/shahjahanwarraich/Desktop/opinionfinderv1.5/software/scol1k/objs/libscol.a(cass.o) 
and /var/folders/F5/F5WuhlFlHcetJlreJ+GlMk+++TI/-Tmp-//ccjhIM0Y.o
collect2: ld returned 1 exit status
make[1]: *** [sents] Error 1
make: *** [modules] Error 2

C 编程和 makefile 对我来说完全陌生,所以我真的不知道从哪里开始。我还可以提供有助于调试此问题的任何其他信息。

select.c中定义的select_lines方法如下:

select_lines (int type, void *lines, int n, FILE *infile, FILE *outfile)
{
    char line[1024];
    int i, current = 0, target;
    struct entry *e;
    LinesA = make_aarray(SelectAlloc, sizeof(struct entry));

    /**  Scan in the lines  **/
    switch (type) {

      case LINESFILE:
    while (scan_int(target, lines) != EOF) {
        new_line(target);
    }
    break;

      case LINESLIST:
    for (; n > 0; n--) {
        target = *((int *)lines)++;
        new_line(target);
    }
    break;

      case LINESRANGE:
    for (target = ((int *)lines)[0]; target <= ((int *)lines)[1]; target++) {
        new_line(target);
    }
    break;

      default: error("select_lines: Bad type");
    }

    Lines = (struct entry *) LinesA->contents;

    /**  Sort by txt sequence  **/
    qsort(Lines, NLines, sizeof(struct entry), txtcmp);

    /**  Extract lines  **/
    current = -1;
    for (i = 0; i < NLines; i++) {
    target = Lines[i].txt;
    if (target < current) error("sort failed");
    if (current < target) {  /* careful: it's possible to select the same line twice */
        while (++current < target) {
        skip_line(infile);
        }
        if (scan_line(line, 1024, infile) == EOF) {
        fprintf(stderr, "Premature end of text file");
        exit(1);
        }
    }
    Lines[i].line = copy_string(line);
    }

    /**  Resort by smp sequence  **/
    qsort(Lines, NLines, sizeof(struct entry), smpcmp);

    /**  Output  **/
    for (i = 0; i < NLines; i++) {
    fprintf(outfile, "%s\n", Lines[i].line);
    }
}

【问题讨论】:

  • @William Pursell。问题已使用 select_lines 函数进行了更新。谢谢。

标签: gcc linker compiler-errors incompatibility


【解决方案1】:

函数 select_lines 中的第 84 行有错误

select.c:在函数“select_lines”中: select.c:84: 错误:增量中的左值无效

GCC 不再允许在左侧进行强制转换。 C 语言不允许这样做,而且 gcc 对遵循 C 规范变得更加严格。

这就是产生这个左值错误的原因。如果存在,您必须删除任何左侧演员表。

也许,target = *((int *)lines)++; htis 包含错误。

这样做,

a1=(int *)lines;
target=*a1++;

【讨论】:

    【解决方案2】:

    唯一包含可疑增量的行是:

    target = *((int *)lines)++;
    

    您可以将其简化为以下代码:

    void select_lines(void *lines)
    {
        int target;
        target =  *((int *)lines) ++;  // Error/warning
        target = (*((int *)lines))++;  // Clean
    }
    

    第二个赋值编译 - 并正确递增 void 指针 lines 指向的整数值,假设 lines 已适当初始化。

    双重定义符号_Bos 表示源代码中有两个文件定义了名为Bos 的东西。一个是库libscol.a 中的文件cass.o。另一个可能是sents.c。假设他们做同样的事情,你将不得不使一个或另一个静态。或者两者都是静态的,除非有其他文件使用该符号。或者您可能只需将一个声明更改为extern。这在一定程度上取决于 Bos 是什么 - 变量或函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 1970-01-01
      相关资源
      最近更新 更多