c - 使用 makefile 获取编译错误

标签 c unix

我尝试了以下示例程序来了解 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");
}

阶乘.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$ make compile
gcc -c main.c
main.c:2:22: error: functions.h: No such file or directory
make: *** [main.o] Error 1
cheetah@desktop:~/make_example$ 

EDITED:

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。

最佳答案

替换

#include<functions.h>

#include"functions.h"

详细解释在这里:http://www.geekinterview.com/question_details/3379

最后,目标文件在链接阶段丢失。

compile : main.o hello.o factorial.o
  gcc -o compile main.o hello.o factorial.o

关于c - 使用 makefile 获取编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8889548/

相关文章:

node.js - 在前台启动 redis 并使用一个命令并行但串行地启动 Node 服务器

c - 生成 C header 时隐藏 Rust 库的私有(private)字段

c++ - 是否可以在使用不同编译器编译的应用程序之间共享共享内存中的 C 结构?

c - 共享内存程序不同步 - 出现段错误

c - malloc 上的段错误

unix - ssh 进入远程系统时如何在 screen 上显示主机名?

linux - SFTP 停止打印消息 "Uploading <file> to/path/to/file"

linux - 查找特定文件夹,然后在其中的特定文件中搜索一个词

c - 使用 getch() 和 getchar() 清除输入缓冲区的区别?

linux - vim 函数 "update timestamp"事件 "BufWrite": to be improved