Jw123
Jw123

Reputation: 57

C makefile errors

I have a custom header file example.h which has prototypes for a few functions. There is a .C file example.c that I implemented which "includes" (#include "example.h") and has the implementations of the functions that has prototype in example.h. Now, I have another function test.c that calls the functions that are prototyped in example.h and defined in example.c.

My make file is as follows

test: test.o
    gcc -o test -g test.o

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

clean:
    rm -f *.o test

I get following message for the functions that are defined in example.c

Undefined first referenced symbol in file

function1 test.o

function2 test.o

function3 test.o

function4 test.o

ld: fatal: Symbol referencing errors. No output written to test

collect2: ld returned 1 exit status

* Error code 1

make: Fatal error: Command failed for target `test'

Any help is most appreciated.

Upvotes: 0

Views: 2514

Answers (3)

MOHAMED
MOHAMED

Reputation: 43518

%.o: %.c
    gcc -c -g -o $@ $^

test: test.o example.o
    gcc -o -g $@ $^

%.o: %.c This means any *.o file should be builded from its equivalen from c files.

example test.o should be builded from test.c and example.o should be builded from example.c

Upvotes: 2

vjain419
vjain419

Reputation: 253

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

as per your code test.o target is calling test.c example.c example.h target which i am not able to see.

Upvotes: 0

Rerito
Rerito

Reputation: 6086

First of all, you must include the example.o file when generating the executable file : gcc -o test example.o test.o. Then, the dependencies you wrote for target test.o are incorrect. You should split it like this :

test: test.o example.o
    gcc -o test test.o example.o
test.o: test.c
    gcc -c -Wall test.c
example.o: example.c
    gcc -c -Wall example.c

Then, consider the use of variables to store the names of your object files, the flags you want to pass to the linker/compiler etc... This would make your life much easier.

Upvotes: 1

Related Questions