2011-03-25 102 views
0

- >代表 “包括”Make文件错误

list.c - > list.h

matrix.c - > matrix.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东西liet 文件名

我该如何解决这个问题?

CNC中 我改变* o文件用gcc -c代替gcc的-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 ...如何解决这个问题?

+2

对于* .o文件,您需要使用'gcc -c -g ...'。 – 2011-03-25 12:39:19

+0

如果您尝试手动运行生成文件中的命令,会发生什么情况?第一个错误发生在哪里?你究竟输入了什么,并抛出了什么错误? – 2011-03-25 12:39:58

回答

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 
+0

尝试编译输出文件时不能使用输出文件,即在其自身的目标文件生成中使用smatrix.o。 – 2011-03-25 12:50:34

+0

@Evan Mulawski:我不使用该文件。 '-o smatrix.o'开关的意思是“输出文件的名称是smatrix.o” – 2011-03-25 12:55:16

+0

太棒了!但是,你能否解释为什么我需要在gcc -g -c -o ...行中添加smartix.o? – codereviewanskquestions 2011-03-25 13:02:04

0

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

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

请注意,您不使用-o选项,直到您编译最终方案。

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

0

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

如果您希望您的代码以符合C99,添加:

-std = C99

其他你需要你在循环使用它之前定义循环变量。

void function(size_t height) 
{ 
    size_t i; 

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