0

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.

4

3 回答 3

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

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

%.o: %.c这意味着任何*.o文件都应该从其等效的c文件中构建。

示例test.o应该建立自test.c并且example.o应该建立自example.c

于 2013-02-01T08:35:24.340 回答
1

首先,生成可执行文件时必须包含example.o文件:gcc -o test example.o test.o。然后,您为目标 test.o 编写的依赖项不正确。你应该像这样拆分它:

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

然后,考虑使用变量来存储目标文件的名称、要传递给链接器/编译器的标志等……这将使您的生活更轻松。

于 2013-02-01T08:48:48.310 回答
0

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

根据您的代码 test.o 目标正在调用我看不到的 test.c example.c example.h 目标。

于 2013-02-01T08:50:34.267 回答