Haj*_*khi 3 c makefile header-files
我有一个给定的makefile,由我们的教授编写。`
SHELL = /bin/bash
CC = gcc
CFLAGS = -Wall -W -std=c99 -pedantic
LIBS =
# Rajouter le nom des executables apres '=', separes par un espace.
# Si une ligne est pleine, rajouter '\' en fin de ligne et passer a la suivante.
# To compile without bor-util.c file
EXECS = main
# To compile with bor-util.c file
EXECSUTIL =
# To compile with bor-util.c & bor-timer.c files
EXECSTIMER =
.c.o :
$(CC) -c $(CFLAGS) $*.c
help ::
@echo "Options du make : help all clean distclean"
all :: $(EXECS) $(EXECSUTIL) $(EXECSTIMER)
$(EXECS) : %: %.o
$(CC) -o $@ $@.o $(LIBS)
$(EXECSUTIL) : %: %.o bor-util.o
$(CC) -o $@ $@.o bor-util.o $(LIBS)
$(EXECSTIMER) : %: %.o bor-util.o bor-timer.o
$(CC) -o $@ $@.o bor-util.o bor-timer.o $(LIBS)
clean ::
\rm -f *.o core
distclean :: clean
\rm -f *% $(EXECS) $(EXECSUTIL) $(EXECSTIMER)
`
Run Code Online (Sandbox Code Playgroud)
在这个项目中,我们要做的就是将我们的代码写在其他文件中,然后像往常一样使用这个 makefile 进行编译。我写了一个 helloWorld 函数来测试。我有 3 个文件 FUNCTIONS.C
#include <stdio.h>
#include "functions.h"
void printMsg(){
printf("Hello World !");
}
Run Code Online (Sandbox Code Playgroud)
函数.H
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void printMsg();
#endif /* FUNCTIONS_H */
Run Code Online (Sandbox Code Playgroud)
和一个 MAIN.C 文件来测试一切
#include "functions.h"
int main(){
printMsg();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经添加main到makefile中。但是我在编译时收到此错误消息
gcc -o main main.o
main.o: In function `main':
main.c:(.text+0xa): undefined reference to `printMsg'
collect2: error: ld returned 1 exit status
Makefile:32: recipe for target 'main' failed
make: *** [main] Error 1
Run Code Online (Sandbox Code Playgroud)
有谁知道解决方案是什么?谢谢
错误信息很明确:链接器没有找到该printMsg函数。这是完全正常的:执行的链接命令是:
gcc -o main main.o
Run Code Online (Sandbox Code Playgroud)
看?没有跟踪functions.o该printMsg功能在何处实现。要解决此问题,您必须使用以下命令链接:
gcc -o main main.o functions.o
Run Code Online (Sandbox Code Playgroud)
问题是你的 Makefile 没有提到functions.o作为先决条件,main也没有在配方中使用它。要么你没看懂教授的指令(他没有要求你加functions.c和functions.h),要么你忘记了他也说明了如何更新Makefile,或者他的Makefile与他自己的指令不兼容。在最后两种情况下,您可以通过更改以下规则来调整 Makefile $(EXECS):
$(EXECS) : %: %.o functions.o
$(CC) -o $@ $^ $(LIBS)
Run Code Online (Sandbox Code Playgroud)
$^扩展为所有先决条件的列表,即在您的情况下,main.o functions.o. 这条新规则将:
main如果main.o或functions.o更改,则重新构建。main.o 和 functions.o。警告:如果您有其他$(EXECS)不依赖于的可执行文件functions.o,或者依赖于其他目标文件,或者如果您有更多其他文件,例如functions.o,您将需要更复杂的东西。问一个新问题。
注意:由于 SO 是英语,所以最好翻译示例代码中的法语注释。我建议:
在 '=' 后添加可执行文件名称,以一个空格分隔。如果一行已满,请在末尾添加一个“\”并在下一行继续。
最后一点:字母大小写很重要。如果您的文件是functions.c不输入FUNCTIONS.C您的问题。与其他文件名相同。
| 归档时间: |
|
| 查看次数: |
12131 次 |
| 最近记录: |