【发布时间】:2012-01-17 04:12:30
【问题描述】:
我尝试了以下示例程序来了解makefile在编译中的用法。
main.c
#include<stdio.h>
#include<functions.h>
int main()
{
print_hello();
printf("\nThe Factorial is : %d\n",factorial(5));
return 0;
}
你好.c
#include<stdio.h>
#include<functions.h>
void print_hello()
{
printf("\nHello world!\n");
}
factorial.c
#include<stdio.h>
#include<functions.h>
void factorial(int n)
{
if(n!=1){
return(n*factorial(n-1));
}
else
return 1;
}
函数.h
#include<stdio.h>
void print_hello();
int factorial(int n);
制作文件
exec : ./compile
compile : main.o hello.o factorial.o
gcc -o compile
main.o : main.c functions.h
gcc -c main.c
hello.o : hello.c functions.h
gcc -c hello.c
factorial.o : factorial.c functions.h
gcc -c factorial.c
错误:
cheetah@desktop:~/make_example$ 编译 gcc -c main.c main.c:2:22:错误:functions.h:没有这样的文件或目录 make: *** [main.o] 错误 1 猎豹@desktop:~/make_example$已编辑:
gcc -c main.c
gcc -c hello.c
gcc -c factorial.c
gcc -o compile
gcc: no input files
make: *** [compile] Error 1
请帮助我理解为什么它会抛出一个错误,因为我已将它包含在我的 makefile 中。
【问题讨论】:
-
注意
functions.h没有给出print_hello()的原型;它只是将函数声明为采用不确定(但固定)数量的参数。要成为 C 中的原型,您必须提供void print_hello(void);(尽管在 C++ 中,这一切都不同)。