最近在看makefile规则,简单写了一个sample code 验证了一把
首先是代码目录架构,有A /B /C 三个文件夹:
A文件夹中的代码如下:
Ahello.c
#include <stdio.h>
void ahello(const char *name)
{
printf("----AAAA---I am in Hello %s!\n", name);
}Ahello.h
void ahello(const char *name);
C文件夹中的代码如下:
Chello.c
#include <stdio.h>
void chello(const char *name)
{
printf("---CCCC---I am in Hello %s!\n", name);
}Chello.h
void chello(const char *name);
main.c:
#include "../A/Ahello.h"
#include "../C/Chello.h"
int main()
{
ahello("everyone");
chello("everyone");
return 0;
}Makefile;
valuable:=main.o ../A/Ahello.o ../C/Chello.o
hello:$(valuable)
gcc -o hello $(valuable)
main.o:main.c
gcc -c main.c
Ahello.o:../A/Ahello.c
gcc -c ../A/Ahello.c
Chello.o:../C/Chello.c
gcc -c ../C/Chello.c
clean:
rm hello $(valuable)
在B中执行make 命令生成如下的目录结构:
执行./hello 输出:
----AAAA---I am in Hello everyone!
---CCCC---I am in Hello everyone!
参考博客:http://blog.youkuaiyun.com/ruglcc/article/details/7814546/#t30

本文详细介绍了如何利用Makefile管理C++项目中的文件依赖,并通过跨文件函数调用来构建简单的应用实例。展示了如何通过Makefile自动化编译过程,确保不同文件间的正确引用和编译顺序,最终生成可执行文件。
799

被折叠的 条评论
为什么被折叠?



