如何管理C头文件的依存关系? [英] How to manage C header file dependencies?

查看:191
本文介绍了如何管理C头文件的依存关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多C文件,有些有标头(.h),有些没有.

I've a lot of C files, some have a header (.h), some files not.

这是我的makefile文件:

Here's my makefile :

.SUFFIXES: 

SRC := $(wildard ./src/*.c)
OBJ := $(SRC:%.c=%.o)

all: $(OBJ)

%.o: %.c
    $(MyNotGCCCompiler) "@../$(*F).cmd"

它工作正常,除了如果我更改头文件时,由于不包含在依赖项中而不会重新编译目标.

It works fine except that if I change a header file, the target isn't recompiled because not included in the dependencies.

如何处理此案?

谢谢

推荐答案

标准方法是在编译时自动生成标头依赖项.

The standard approach is to generate header dependencies automatically while compiling.

对于第一次编译,不需要依赖,因为必须编译每个源文件.随后的重新编译加载由先前的编译生成的依赖项,以确定需要重新编译什么.

For the first compilation no dependencies are necessary since every source file must be compiled. Subsequent recompilations load dependencies generated by the previous compilation to determine what needs to be recompiled.

您的$(MyNotGCCCompiler)可能具有用于生成依赖项文件的命令行选项.

Your $(MyNotGCCCompiler) is likely to have a command line option to generate a dependencies file.

使用gcc时,其工作方式如下:

When using gcc it works like this:

.SUFFIXES: 

SRC := $(wildard ./src/*.c)
OBJ := $(SRC:%.c=%.o)
DEP := $(OBJ:%.o=%.d)

all: $(OBJ)

# when compiling produce a .d file as well 
%.o: %.c
    gcc -c -o $@ $(CPPFLAGS) $(CFLAGS) -MD -MP -MF ${@:.o=.d} $<

# don't fail on missing .d files
# there won't be any on the first run
-include $(DEP) 

这篇关于如何管理C头文件的依存关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆