Linux:通过make而不是#include在c中包含头文件

时间:2022-08-01 15:07:39

I have a macro which is used in afile.c and bfile.c (both in module A) This macro is in aheader.h

我有一个宏用在一个文件中。c和bfile。c(都在A模块)这个宏在前面。h

Both of these are in different modules/directories and aheader.h module is complied before module A is complied.

它们都位于不同的模块/目录和aheader中。h模块在A模块被编译之前被编译。

Now one way is to do #include "aheader.h" in each of the .c files. But instead of doing this, is there a way to make some addition in the Makefile (like adding it to the list of headers) for module A, so that aheader.h is picked for everywhere the macro is used?

现在一种方法是做#include“aheader”。h"在每个。c文件中。但是,与此相反,是否有一种方法可以在Makefile中为模块a添加一些内容(比如将其添加到header列表中),以便aheader。h是在使用宏的地方选择的?

1 个解决方案

#1


3  

#include "aheader.h" is the simple and correct thing to do. C has no feature to auto-include headers when a macro is used.

# include”无疑。h"是简单而正确的做法。当使用宏时,C没有自动包含标题的功能。

If you insist on doing it in the makefile, you can add -include aheader.h as a compilation flag. It will include it in all files.

如果您坚持在makefile中这样做,您可以添加-include aheader。h作为编译标志。它将包含在所有文件中。

It's possible to use the makefile to add this flag only when the macro is found in the C file, by using grep. But it's complicated makefile work, and I think you're better off without it.

只有在C文件中找到宏时,才可以使用makefile添加此标记,使用grep。但这是复杂的makefile工作,我认为你最好没有它。

#1


3  

#include "aheader.h" is the simple and correct thing to do. C has no feature to auto-include headers when a macro is used.

# include”无疑。h"是简单而正确的做法。当使用宏时,C没有自动包含标题的功能。

If you insist on doing it in the makefile, you can add -include aheader.h as a compilation flag. It will include it in all files.

如果您坚持在makefile中这样做,您可以添加-include aheader。h作为编译标志。它将包含在所有文件中。

It's possible to use the makefile to add this flag only when the macro is found in the C file, by using grep. But it's complicated makefile work, and I think you're better off without it.

只有在C文件中找到宏时,才可以使用makefile添加此标记,使用grep。但这是复杂的makefile工作,我认为你最好没有它。