【问题标题】:Make file error制作文件错误
【发布时间】:2011-03-25 12:35:45
【问题描述】:

-> 表示“包含”

list.c -> list.h

matrix.c -> 矩阵.h

smatrix.c -> smatrix.h

smatrix h 文件 -> list.h 和 matrix.h 文件

test.c -> test.h

test.h -> list.h、matrix.h 和 smatrix.g 文件

现在我有了这个makefile

all: run

run: test.o list.o matrix.o smatrix.o
    gcc test.o list.o matrix.o smatrix.o -o matrix-mul

list.o: list.c list.h
    gcc -g list.c 

matrix.o: matrix.c matrix.h
    gcc -g matrix.c 

smatrix.o: smatrix.c smatrix.h
    gcc -g smatrix.c 

test.o: test.c test.h
    gcc -g test.c 

现在我得到了

Undefined symbols for architecture x86_64:
  "_make_matrix", referenced from: //make_matrix defines in matrix.h
      _main in cctU8RoB.o
  "_print_matrix", referenced from://print_matrix defines in list.h
      _main in cctU8RoB.o
  "_make_smatrix", referenced from://make_smatrix defines in smatrix.h
      _main in cctU8RoB.o
  "_multiply_by_vector", referenced from://muliply_by_vector defines in smatrix.h
      _main in cctU8RoB.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [test.o] Error 1

我正在调用那些在 test.c 中的其他文件中定义的函数,而 test.h 包括在 test.c 文件中调用的函数的所有头文件。所有头文件都包含 gard something liet filename

我该如何解决这个问题??

-编辑- 我用 gcc -c 而不是 gcc -o 更改了 *o 文件,现在我得到了

matrix.c:33: error: ‘for’ loop initial declaration used outside C99 mode
matrix.c:38: error: ‘for’ loop initial declaration used outside C99 mode
matrix.c:44: error: ‘for’ loop initial declaration used outside C99 mode
matrix.c: In function ‘make_matrix_k’:
matrix.c:58: error: ‘for’ loop initial declaration used outside C99 mode
matrix.c: In function ‘print_matrix’:
matrix.c:73: error: ‘for’ loop initial declaration used outside C99 mode
matrix.c:74: error: ‘for’ loop initial declaration used outside C99 mode

//for loop in matrix.c file
for(size_t i=0; i < height; i++){
        //m->data[i] = malloc(sizeof(int)*width);
        m->data[i] = calloc(width, sizeof(int));

        if(m->data[i] == NULL){
            for(size_t j = 0; j < i; j++) free(m->data[j]);
            free(m->data);
            free(m);
            return 0;
        }
        if(opt == nonzero_matrix_k_off){
            for(size_t j = 0; j < width; j++){
                m->data[i][j] = 2;
            }
        }
    }

在 xcode4 上运行良好...如何解决这个问题?

【问题讨论】:

  • 对于 *.o 文件,您需要使用gcc -c -g ...
  • 如果您尝试手动运行 makefile 中的命令会发生什么?第一个错误发生在哪里?你到底输入了什么,抛出了什么错误?

标签: c


【解决方案1】:

您需要添加-c 选项来让gcc 生成目标文件,并添加-o name 来命名它。示例:

smatrix.o: smatrix.c smatrix.h
    gcc -g -c -o smatrix.o smatrix.c

为解决第二个问题,将-std=c99 开关添加到gcc 参数列表中。而且,正如 Evan Mulawski 所说,您不需要 -o 开关。

smatrix.o: smatrix.c smatrix.h
    gcc -g -std=c99 -c smatrix.c

【讨论】:

  • 尝试编译输出文件时不能使用输出文件,即在自己的目标文件生成中使用smatrix.o。
  • @Evan Mulawski:我不使用该文件。 -o smatrix.o 开关表示“输出文件的名称是 smatrix.o”
  • 很好用!但是你能解释一下为什么我需要在 gcc -g -c -o ... 行中添加 smartix.o 吗?
  • 你不需要它,这是我的错。链接整个程序时需要它:告诉编译器输出文件的名称(默认为a.out)。当您使用-c 选项创建目标文件时,gcc 使用相同的名称。通过传递-o smatrix.o,您只需告诉编译器它已经知道什么。
【解决方案2】:

正如我的评论所说,要生成目标文件,您需要在调用 GCC 时使用-c 选项:

list.o:  list.c list.h
         gcc -c -g list.c

请注意,在编译最终程序之前不要使用-o 选项。

您可以选择添加-ansi -pedantic -Wall,我觉得这非常有用。

【讨论】:

    【解决方案3】:

    对于新问题,gcc 默认使用它自己的标准,而不是 c99。

    如果您希望您的代码符合 c99,请添加:

    -std=c99

    否则你需要在循环中使用它之前定义循环变量。

    void function(size_t height)
    {
        size_t i;
    
        for(i=0;i<height;i++)
        {
         ...
        };
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2014-12-22
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多