Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

-> 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
265 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...