使用 .h 作为 c++ 的标头 文件错误?

发布于 2024-07-15 09:01:03 字数 283 浏览 4 评论 0原文

使用 .h 作为 C++ 文件的标头是否错误?

我到处都能看到这种情况,尤其是用“C 风格”编写的代码。 我注意到 Emacs 总是为 .h 标头选择 C ​​突出显示样式,但为 hpp 或 hh 选择 c++。

将标头标记为 .h 实际上是“错误的”,还是只是让我烦恼的事情?

编辑:

如果我有标记为“hpp &”的项目文件,这有一个很好的(ish)原因让我烦恼。 cpp' 我可以使用 'grep some *pp' 等,否则我必须输入 '.h cpp'

Is using .h as a header for a c++ file wrong?

I see it all over the place, especially with code written in the "C style".
I noticed that Emacs always selects C highlighting style for a .h header, but c++ for hpp or hh.

Is it actually "wrong" to label your headers .h or is it just something which annoys me?

EDIT:

There is a good (ish) reason why this annoys me, if I have project files labelled, 'hpp & cpp' I can get away with 'grep something *pp' etc. otherwise I have to type '.h cpp'

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

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

发布评论

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

评论(5

你怎么敢 2024-07-22 09:01:03

没有错。 这是 Microsoft Visual C++ 的默认设置。

只要遵循你喜欢的标准,并坚持下去即可。

nothing wrong with that. This is the default with Microsoft Visual C++.

Just follow the standard you like, and stick with it.

毁我热情 2024-07-22 09:01:03

将 C++ 头文件命名为 .h 并没有错误。 没有任何规则规定您的代码必须使用哪些扩展。 对于非标头,MSVC 使用 .cpp,在 Linux 上也使用 .cc。 没有一个全球标准,但 .h 的使用肯定非常广泛。

但我想说,将标头命名为 .hpp(我也见过 .hh 几次)比仅使用 .h 更加一致且信息丰富。

It's not wrong to call your C++ headers .h. There are no rules of what extensions your code must use. For your non-headers, MSVC uses .cpp and on Linux, .cc as well. There is no one global standard, and .h is definitely very widely used.

But I'd say calling your headers .hpp (I've seen .hh a few times as well) is a lot more consistent and informative than just using .h.

隐诗 2024-07-22 09:01:03

您可以自由使用 .h.H.hpp.hxx。 hh,或者你有什么。 就像您可以自由地将 .c 用于 C 源代码,以及 .C.cpp.cc,或 .cxx 对于 C++ 源代码。 扩展主要是一种约定。

You're free to use .h, .H, .hpp, .hxx, .hh, or whathaveyou. Just like you're free to use .c for C sources, and .C, .cpp, .cc, or .cxx for C++ sources. The extension is largely a convention.

自找没趣 2024-07-22 09:01:03

就像其他文件一样 - .c 代表 C,.cpp 代表 C++ - 使用 .h 代表 C,.hpp 代表 C++。 这将满足大多数编译器/编辑器的要求。 不这样做并没有什么错,但它可以用来提示您实际使用的是两种语言中的哪一种。

如果您想停止该行为而不重命名文件,您应该查看 Emacs 配置并尝试更改源代码检测。

It's like with the other files - .c for C, .cpp for C++ - use .h for C, .hpp for C++. This will satisfy most compilers/editors. It's not really wrong to not do so, but it can be used to give a hint which of the both languages you are actually using.

If you want to stop that behaviour without renaming the files, you should have a look at the Emacs config and try to change the source code detection.

秋千易 2024-07-22 09:01:03

由于 Emacs 没有简单的方法来确定 .h 文件是 C 还是 C++,因此为 .h 文件选择 C,为 .hpp 文件选择 C++ 并不荒谬。

请注意,所有 boost 头文件都以 .hpp 为后缀。

Since Emacs has no easy way of finding out whether a .h file is C or C++, it's not ridiculous to choose C for .h files and C++ for .hpp files.

Note that all boost header files are suffixed with .hpp.

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