我知道这已经得到了解答,但我仍然无法让它发挥作用。
first.c
#include <stdio.h>
#include "second.h"
int main(void){
printf("%d\n", addone(5));
return 0;
}
second.c
int addone(int a){
return ++a;
}
second.h
int addone(int a);
当我运行gcc -o executable first.c -Wall
时,它会显示undefined reference to addone
答案 0 :(得分:2)
#include <stdio.h>
#include "second.h"
也在second.c文件中包含那些头文件,然后编写Makefile并使用make编译它。
生成文件:
CFLAGS = -g -O -Wall
OBJ = first.o second.o
# An explicit rule is required to link.
# Compilation is handled automatically.
first: $(OBJ)
$(CC) $(CFLAGS) -o first $(OBJ)
# Declare that both object files depend on the header file.
first.o second.o: second.h
# Conventionally 'make clean' removes what 'make' creates.
# Not strictly required.
.PHONY: clean
clean:
-rm -f first $(OBJ)
您无法复制并粘贴上述内容;您还必须更改两个缩进行(在它们上面带有shell命令的行),以便缩进是一个单独的 TAB 字符。如果你不这样做,你会得到一个神秘的错误信息:
Makefile:5:***缺少分隔符。停止。
first.c:
#include <stdio.h>
#include "second.h"
int main(void){
printf("%d\n", addone(5));
return 0;
}
second.c
#include <stdio.h>
#include "second.h"
int addone(int a){
return ++a;
}
second.h
#include <stdio.h>
int addone(int a);