codereviewanskquestions
codereviewanskquestions

Reputation: 13978

Make file error

-> represents "include"

list.c -> list.h

matrix.c -> matrix.h

smatrix.c -> smatrix.h

smatrix h file -> list.h and matrix.h files

test.c -> test.h

test.h -> list.h, matrix.h, and smatrix.g files

And now I have this 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 

And now I am getting

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

I am calling those functions which are defined in other files in test.c and test.h includes all the header files for the functions called in test.c file. All header files have include gard something liet filename

How can I fix this problem??

-edit- I changed *o file with gcc -c instead of gcc -o and now I am getting

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;
            }
        }
    }

It worked fine with xcode4...How to fix this problem?

Upvotes: 0

Views: 1162

Answers (3)

PAntoine
PAntoine

Reputation: 689

For the new question, gcc defaults to it's own standard and not c99.

If you want your code to conform to c99, add:

-std=c99

else you need to define the loop variable before you use it in the loop.

i.e.

void function(size_t height)
{
    size_t i;

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

Upvotes: 0

Adam Trhon
Adam Trhon

Reputation: 2935

You need to add the -c option to make gcc generate object file and -o name to name it. Example:

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

To solve the second problem, add the -std=c99 switch to gcc parameter list. And, as Evan Mulawski mentioned, you don't need the -o switch.

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

Upvotes: 1

Evan Mulawski
Evan Mulawski

Reputation: 55334

As my comment stated, to generate the object files you need to use the -c option when calling GCC:

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

Note that you DO NOT use the -o option until you compile the final program.

You can optionally add -ansi -pedantic -Wall, which I find extremely useful.

Upvotes: 0

Related Questions