C编辑中的自动定义......为什么?

时间:2021-04-29 15:04:35

When Eclipse creates a new file (.c or .h file) in a C project the editor always auto creates a #define at the top of the file like this: If the file is named 'myCFile.c' there will be a #define at the start of the file like this

当Eclipse在C项目中创建一个新文件(.c或.h文件)时,编辑器总是在文件顶部自动创建一个#define,如下所示:如果该文件名为“myCFile.c”,则会有一个#像这样定义文件的开头

#ifndef MYCFILE_C_
#define MYCFILE_C_

I have seen other editors do this as well (Codewright and SlikEdit I think). The #defines don't seem to do anything for the editor as I can just delete them without any problem, and I can't think of a reason why I would want to use them. Does anyone know why they are there?

我见过其他编辑也这样做(我认为Codewright和SlikEdit)。 #defines似乎没有为编辑器做任何事情,因为我可以毫无问题地删除它们,我想不出我想要使用它们的原因。有谁知道他们为什么在那里?

4 个解决方案

#1


4  

It's to guard against multiple definitions.

这是为了防止多种定义。

#2


2  

Sometimes people include a whole .c file in other .c files (or even .h files), so it has the exact same purpose of preventing an include file from getting included multiple times and the compiler spitting out multiple definition errors.

有时人们在其他.c文件(甚至.h文件)中包含一个完整的.c文件,因此它具有完全相同的目的,即防止包含文件被多次包含并且编译器吐出多个定义错误。

It is strange, though, that it would be the default behavior of an editor to put this in anything but a .h file. This would be a rarely needed feature.

但奇怪的是,编辑器的默认行为是将它放在除.h文件之外的任何内容中。这将是一个很少需要的功能。

#3


1  

A more modern version of this is to use:

更现代的版本是使用:

#pragma once

It is quite unusual to see this in a .c file, normally it is in the header files only.

在.c文件中看到这一点是很不寻常的,通常它只在头文件中。

#4


0  

I think it's a throwback of C include issues, where multiple copies of the source would get included - unless you are meticulous with include chains (One file includes n others). Checking if a symbol is defined and including only if the symbol is defined - was a way out of this.

我认为这是C包含问题的回归,其中包括源的多个副本 - 除非你对包含链一丝不苟(一个文件包含n个其他文件)。检查是否定义了符号,并且仅在符号被定义时包含 - 这是一种方法。

#1


4  

It's to guard against multiple definitions.

这是为了防止多种定义。

#2


2  

Sometimes people include a whole .c file in other .c files (or even .h files), so it has the exact same purpose of preventing an include file from getting included multiple times and the compiler spitting out multiple definition errors.

有时人们在其他.c文件(甚至.h文件)中包含一个完整的.c文件,因此它具有完全相同的目的,即防止包含文件被多次包含并且编译器吐出多个定义错误。

It is strange, though, that it would be the default behavior of an editor to put this in anything but a .h file. This would be a rarely needed feature.

但奇怪的是,编辑器的默认行为是将它放在除.h文件之外的任何内容中。这将是一个很少需要的功能。

#3


1  

A more modern version of this is to use:

更现代的版本是使用:

#pragma once

It is quite unusual to see this in a .c file, normally it is in the header files only.

在.c文件中看到这一点是很不寻常的,通常它只在头文件中。

#4


0  

I think it's a throwback of C include issues, where multiple copies of the source would get included - unless you are meticulous with include chains (One file includes n others). Checking if a symbol is defined and including only if the symbol is defined - was a way out of this.

我认为这是C包含问题的回归,其中包括源的多个副本 - 除非你对包含链一丝不苟(一个文件包含n个其他文件)。检查是否定义了符号,并且仅在符号被定义时包含 - 这是一种方法。