make: *** 没有规则可以生成“main.o”所需的目标“main.c”。停止
函数.h
#include<stdio.h>
void print_hello(void);
int factorial(int n);
main.c
#include<stdio.h>
#include<functions.h>
int main()
{
print_hello();
printf("\nThe factorial is: %d \n",factorial(5));
return 0;
}
hello.c
#include<stdio.h>
#include<functions.h>
void print_hello()
{
printf("\nHello World!\n");
}
阶乘.c
#include<stdio.h>
#include<functions.h>
int factorial(int n)
{
if(n!=1)
{
return(n*factorial(n-1));
}
else
return 1;
}
makefile
exec : \
compile
echo "Executing the object file"
./compile
compile : main.o hello.o factorial.o
echo "Compiling"
gcc -o compile $^
main.o : main.c functions.h
gcc -c main.c -I./INCLUDE -I./SRC
hello.o : hello.c functions.h
gcc -c hello.c -I./INCLUDE -I./SRC
factorial.o : factorial.c functions.h
gcc -c factorial.c -I./INCLUDE -I./SRC
文件夹:make_example\INCLUDE\functions.h 文件夹: make_example\SRC\main.c 文件夹: make_example\SRC\hello.c 文件夹: make_example\SRC\factorial.c 文件夹:make_example\makefile
时出现错误
将 make 文件编译为“desktop:~/make_example$ make make: * 没有规则来生成目标 main.c',由
main.o' 需要。”
请帮助我理解为什么会出现这个错误
functions.h
#include<stdio.h>
void print_hello(void);
int factorial(int n);
main.c
#include<stdio.h>
#include<functions.h>
int main()
{
print_hello();
printf("\nThe factorial is: %d \n",factorial(5));
return 0;
}
hello.c
#include<stdio.h>
#include<functions.h>
void print_hello()
{
printf("\nHello World!\n");
}
factorial.c
#include<stdio.h>
#include<functions.h>
int factorial(int n)
{
if(n!=1)
{
return(n*factorial(n-1));
}
else
return 1;
}
makefile
exec : \
compile
echo "Executing the object file"
./compile
compile : main.o hello.o factorial.o
echo "Compiling"
gcc -o compile $^
main.o : main.c functions.h
gcc -c main.c -I./INCLUDE -I./SRC
hello.o : hello.c functions.h
gcc -c hello.c -I./INCLUDE -I./SRC
factorial.o : factorial.c functions.h
gcc -c factorial.c -I./INCLUDE -I./SRC
Folder: make_example\INCLUDE\functions.h Folder:
make_example\SRC\main.c Folder: make_example\SRC\hello.c Folder:
make_example\SRC\factorial.c Folder: make_example\makefile
I'm getting an error when compiled the make file as "desktop:~/make_example$ make
make: * No rule to make target main.c', needed by
main.o'. Stop."
Please help me understand why this error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您运行
make
时,它会尝试创建文件中的第一个目标 (exec
),这取决于compile
,而compile
又取决于main.o
,它依赖于main.c
。没有文件main.c
,只有SRC/main.c
。没有规则创建一个名为main.c
的文件,并且没有预先存在的main.c
,因此make
出现错误并退出。您可以使用
VPATH
或vpath 修复此问题
:
When you run
make
it tries to make the first target in the file (exec
), which depends oncompile
, which depends onmain.o
, which depends onmain.c
. There is no filemain.c
, onlySRC/main.c
. There is no rule to make a file calledmain.c
, and there is no pre-existingmain.c
, somake
has an error and exits.You can fix this with
VPATH
orvpath
:你的 main.c 位于子目录 src 中,并且 make 不知道去那里查找。有很多选项,最简单的就是在 makefile 中编写“src/main.c”而不是“main.c”。您还可以将 main.c 向上移动一个级别,或者将 makefile 移动到 src/ 并在那里构建,或者将 makefile 放在顶层,将 cd 放入 src 并调用 make (这是递归 make,许多人认为这是“有害的”,但在许多(大多数)情况下都很好。)您还可以使用 VPATH 指令,但如果您选择这样做,请注意这不适用于所有版本的 make。
Your main.c is in the subdirectory src, and make does not know to look there. There are many options, the easiest of which is to write "src/main.c" instead of "main.c" in the makefile. You could also move main.c up a level, or move the makefile to src/ and build there, or put a makefile in the toplevel that cds into src and calls that make (this is recursive make, which many consider "harmful", but is perfectly fine in many(most) cases.) You can also use a VPATH directive, but if you choose to do so, be aware that this will not work with all versions of make.