Creating Implicit Rules and Header Files

There are supposed to be implicit rules to make writing Make files easier, but if my understanding is correct, if my C files depend on any header files, I must write this rule explicitly. I'm right? This seems to significantly reduce the usefulness of implicit rules, since most C files depend on multiple header files, so I thought maybe something is missing for me.

+4
source share
3 answers

You do not need to write a rule, only dependencies. Example:

foo.o : foo.h bar.h 

The file foo.o will still be generated by an implicit rule, but it has additional dependencies foo.h and bar.h This dependency string can also be automatically generated by most compilers.

+5
source

You can auto-generate header dependencies using gcc using the following file fragment

 SOURCES := $(wildcard *.c) DEPS := $(SOURCES:%.c=%.d) CFLAGS += -MMD -include $(DEPS) 

Some adjustments may be required to work with your specific set of rules.

+7
source

make not a utility that goes and reads inside your C file and determines which header it includes. It works based on modified temporary files. Therefore, regardless of whether the target depends on the header or any other file, you need to explicitly specify make dependencies.

gcc can help you make your work easier by creating a dependency list for you

main.c

 #include<stdio.h> #include"my_header.h" int main () { return 0; } 

And then,

 gcc -M main.c 

Now, with the -M preprocessor flag, it will automatically generate a list of dependencies, for example

 main.o: main.c /usr/include/stdio.h /usr/include/features.h \ /usr/include/bits/predefs.h /usr/include/sys/cdefs.h \ /usr/include/bits/wordsize.h /usr/include/gnu/stubs.h \ /usr/include/gnu/stubs-64.h \ /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/include/stddef.h \ /usr/include/bits/types.h /usr/include/bits/typesizes.h \ /usr/include/libio.h /usr/include/_G_config.h /usr/include/wchar.h \ /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/include/stdarg.h \ /usr/include/bits/stdio_lim.h /usr/include/bits/sys_errlist.h \ my_header.h 

gcc found out that everything included inside stdio.h too!

+2
source

Source: https://habr.com/ru/post/1411592/


All Articles