打开.c文件时自动包含头文件

时间:2021-12-10 15:10:25

I want to know if this is possible...

我想知道这是否可能......

Whenever we want to compile a C program or C++ etc, we need some to include header files like #include in .c file....

每当我们想要编译C程序或C ++等时,我们需要一些在.c文件中包含#include等头文件....

Is there any way so that, any file which gets created by some name.c, will automatically include the header file without typing in the program... I'm not sure how this works...

是否有任何方式,任何由name.c创建的文件,将自动包含头文件而无需在程序中输入...我不确定这是如何工作的...

Or even something like this...i use vim, so if I save any file like a.c, (without putting #include in the program), the vim or gcc, whichever, considers this as .c file and adds the stdio.h file..I don't mean that it should add in the program, but while compiling it should take stdio.h file also...Is this possible...?

或者甚至是这样的......我使用vim,所以如果我保存任何文件,比如ac,(不将#include放在程序中),vim或gcc,无论哪个,都认为这是.c文件并添加了stdio.h file ..我不是说它应该添加到程序中,但是在编译它时也应该使用stdio.h文件......这可能......?

3 个解决方案

#1


4  

GCC has the -include option which allows you to do that. However, this feature is targeted at a different use case and IMHO what you are suggesting is not a good idea. Just write your #include directives in the source, you only need to do this once.

GCC有-include选项,允许您这样做。但是,此功能针对的是不同的用例和恕我直言,你所建议的并不是一个好主意。只需在源代码中编写#include指令,您只需要执行一次。

#2


1  

The reason you have to specify the #include in the implementation file is to tell the compiler which functionality you actually want. A function name is just a name, and though certain functions like fprintf() can be associated with a particular functionality, other stuff (how about generic names like remove(), create(), trigger()) certainly can't. It's your job to specify this, and in C, this is done via the combination of #include and include paths. This is common to most programming languages; for example, in Java you have import and the classpath.

您必须在实现文件中指定#include的原因是告诉编译器您实际需要哪些功能。函数名称只是一个名称,虽然某些函数(如fprintf())可以与特定功能相关联,但其他内容(如删除(),create(),trigger()等泛型名称当然不能。指定它是你的工作,在C中,这是通过#include和include路径的组合来完成的。这在大多数编程语言中都很常见;例如,在Java中,您有导入和类路径。

So, it's obviously a bad idea to leave the includes out. Now, if you're complaining about the effort to look this up and type it, try a snippet solution for Vim or use a full-fledged IDE, most of which offer handy shortcuts for this.

因此,将包含遗漏出去显然是一个坏主意。现在,如果您正在抱怨要查找并输入它,请尝试使用Vim的代码段解决方案或使用功能齐全的IDE,其中大部分都提供了方便的快捷方式。

#3


0  

using an sh script you may create a global variable containing the file name.

使用sh脚本,您可以创建包含文件名的全局变量。

you can after use gcc %tmp_file%.c -include %tmp_file%.h

你可以在使用gcc%tmp_file%.c -include%tmp_file%.h之后

#1


4  

GCC has the -include option which allows you to do that. However, this feature is targeted at a different use case and IMHO what you are suggesting is not a good idea. Just write your #include directives in the source, you only need to do this once.

GCC有-include选项,允许您这样做。但是,此功能针对的是不同的用例和恕我直言,你所建议的并不是一个好主意。只需在源代码中编写#include指令,您只需要执行一次。

#2


1  

The reason you have to specify the #include in the implementation file is to tell the compiler which functionality you actually want. A function name is just a name, and though certain functions like fprintf() can be associated with a particular functionality, other stuff (how about generic names like remove(), create(), trigger()) certainly can't. It's your job to specify this, and in C, this is done via the combination of #include and include paths. This is common to most programming languages; for example, in Java you have import and the classpath.

您必须在实现文件中指定#include的原因是告诉编译器您实际需要哪些功能。函数名称只是一个名称,虽然某些函数(如fprintf())可以与特定功能相关联,但其他内容(如删除(),create(),trigger()等泛型名称当然不能。指定它是你的工作,在C中,这是通过#include和include路径的组合来完成的。这在大多数编程语言中都很常见;例如,在Java中,您有导入和类路径。

So, it's obviously a bad idea to leave the includes out. Now, if you're complaining about the effort to look this up and type it, try a snippet solution for Vim or use a full-fledged IDE, most of which offer handy shortcuts for this.

因此,将包含遗漏出去显然是一个坏主意。现在,如果您正在抱怨要查找并输入它,请尝试使用Vim的代码段解决方案或使用功能齐全的IDE,其中大部分都提供了方便的快捷方式。

#3


0  

using an sh script you may create a global variable containing the file name.

使用sh脚本,您可以创建包含文件名的全局变量。

you can after use gcc %tmp_file%.c -include %tmp_file%.h

你可以在使用gcc%tmp_file%.c -include%tmp_file%.h之后