C中的#import和#include有什么区别? [英] What is the difference between #import and #include in C?

查看:204
本文介绍了C中的#import和#include有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于预处理器指令的内容,并且我已经看到#import在C程序中被使用了几次。我不确定它们之间有什么区别,有些网站说#include只用于头文件,而#import在Java中使用得更多,在C中不推荐使用。

I've read up a bit on preprocessor directives and I've seen #import being used a few times in C programs. I'm not sure what the difference is between them, some sites have said that #include is only used for header files and #import is used more in Java and is deprecated in C.

如果是这种情况,为什么有些程序仍然使用#import,它与#include有何不同?另外,我在我的一些C程序中使用了#import,它看起来工作得很好,和#include做同样的事情。

If that's the case, why do some programs still use #import and how exactly is it different from #include? Also, I've used #import in a few of my C programs and it seems to work fine and do the same thing as #include.

推荐答案

这在Gnu CPP(C预处理器)手册中有详细解释,尽管clang(可能还有其他C编译器,但不是MSVC)的行为相同:

This is well-explained in the Gnu CPP (C preprocessor) manual, although the behaviour is the same in clang (and possibly other C compilers, but not MSVC):


  1. 问题。简介:您通常不希望在单个翻译单元中包含两次相同的标题,因为这可能导致重复声明,这是一个错误。但是,由于包含的文件本身可能要包含其他文件,因此很难避免。

  1. The problem. Summary: You don't usually want to include the same header twice into a single translation unit, because that can lead to duplicate declarations, which is an error. However, since included files may themselves want to include other files, it is hard to avoid.

一些非标准解决方案(包括 #import )。摘要:包含文件中的 #import 中的 #pragma一次包含文件都防止重复包含。但是 #pragma一次是一个更好的解决方案,因为包装者不需要知道是否可以接受重复包含。

Some non-standard solutions (including #import). Summary: #import in the including file and #pragma once in the included file both prevent duplicate inclusion. But #pragma once is a much better solution, because the includer shouldn't need to know whether duplicate inclusion is acceptable.

链接文档调用 #import 不推荐使用的扩展名,这是一种描述功能的奇怪方式这从来都不是任何标准C版本的一部分。但它并非毫无意义:许多预处理器实现都允许 #import (这是Objective-C的一个特性),因此它是一个常见的扩展。将其弃用是一种说法,即无论实施的广泛程度如何,扩展永远不会成为任何C标准的一部分。

The linked document calls #import a "deprecated extension", which is a slightly odd way of describing a feature which was never part of any standard C version. But it's not totally meaningless: many preprocessor implementations do allow #import (which is a feature of Objective-C), so it is a common extension. Calling it deprecated is a way of saying that the extension will never be part of any C standard, regardless of how widespread implementations are.

如果如果您想使用扩展程序,请使用 #pragma一次;也可能不会出现在未来的标准中,但是为给定的头文件更改它只需要在一个地方而不是在包含标题的每个文件中进行更改。 C ++甚至C可能在某种程度上开发某种模块功能,这将允许包含保护最终被替换。

If you want to use an extension, use #pragma once; that also might not be present in a future standard, but changing it for a given header file will only require a change in one place instead of in every file which includes the header. C++ and even C are likely at some point to develop some kind of module feature which will allow inclusion guards to finally be replaced.

这篇关于C中的#import和#include有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆