2013-02-01 81 views
0

我有一个自定义头文件example.h,它有几个函数的原型。有一个.C文件example.c,我实现了其中的“include”(#include“example.h”),并且具有example.h中原型函数的实现。现在,我有另一个函数test.c调用example.h中原型并在example.c中定义的函数。C makefile错误

我的化妆文件如下

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 

我获得以下消息在example.c

在文件中未定义第一次引用 符号

功能1 test.o中定义的函数

function2 test.o

功能3 test.o

function4 test.o

LD:致命的:符号引用错误。无输出写入测试

collect2:LD返回1退出状态

*错误代码1

化妆:致命错误:命令失败目标'测试”

任何帮助是最赞赏。

+0

为什么你不链接示例?你编译它... – thang

回答

2
%.o: %.c 
    gcc -c -g -o [email protected] $^ 

test: test.o example.o 
    gcc -o -g [email protected] $^ 

%.o: %.c这意味着任何*.o文件应该从它的equivalen从c文件被建造。 gcc -o test example.o test.o

例如test.o应从test.c被建造和example.o应该example.c

+0

OP似乎并没有用于makefile规则和模式,你应该多解释一下。为了澄清你的答案,你应该:1.定义什么是$(CC),$(CFLAGS)和$(LDFLAGS)(给出一个基本选项的例子),2.解释什么是@ @和$^ – Rerito

+0

我改变了他们用简单的命令'gcc' – MOHAMED

1

首先来建造,你必须生成可执行文件时包括example.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 

然后,考虑使用的变量来存储你的目标文件的名称,你想传递给连接器/编译器等标志...这将让您的生活更容易。

+0

对'test.o:test.c'和'example.o:example.c'使用相同的规则太多了。你可以在一条规则中减少这个数字。使用'%.o:%。c',而对于build命令,使用'gcc -c -Wall -g -o $ @ $ ^' – MOHAMED

+0

顺便说一下,OP在他的gcc命令中使用-g作为选项。你必须在你的gcc命令中加入它 – MOHAMED

+0

我知道,但是我想给OP一个基本的例子,而不使用特殊的makefile规则(比如'.c.o:')。为他澄清你的答案,我会upvote :) – Rerito

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文件的目标,我现在无法看到。