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

发布于 2024-07-04 19:03:24 字数 405 浏览 5 评论 0原文

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

#ifndef MYCFILE_C_
#define MYCFILE_C_

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

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

#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的黑色迷你裙 2024-07-11 19:03:24

我认为这是 C 包含问题的倒退,其中源代码的多个副本将被包含 - 除非您对包含链非常细致(一个文件包含 n 个其他文件)。
检查符号是否已定义并仅包含该符号是否已定义 - 是解决此问题的一种方法。

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.

淡笑忘祈一世凡恋 2024-07-11 19:03:24

更现代的版本是使用:

#pragma once

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

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.

尤怨 2024-07-11 19:03:24

这是为了防止多重定义

It's to guard against multiple definitions.

昔梦 2024-07-11 19:03:24

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文