简体   繁体   English

使用.h 作为 c++ 文件的 header 是否错误?

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

Is using.h as a header for a c++ file wrong?使用.h 作为 c++ 文件的 header 是否错误?

I see it all over the place, especially with code written in the "C style".我到处都能看到它,尤其是用“C 风格”编写的代码。 I noticed that Emacs always selects C highlighting style for ah header, but c++ for hpp or hh.我注意到 Emacs 总是为 ah header 选择 C 突出显示样式,但对于 hBD9.039662

Is it actually "wrong" to label your headers.h or is it just something which annoys me?你的 headers.h 对 label 来说实际上是“错误的”还是只是让我烦恼的事情?

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

nothing wrong with that.没有错。 This is the default with Microsoft Visual C++.这是 Microsoft Visual C++ 的默认设置。

Just follow the standard you like, and stick with it.只要遵循你喜欢的标准,并坚持下去。

It's not wrong to call your C++ headers.h.调用你的 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.对于您的非标头,MSVC 使用 .cpp 和 Linux 和 .cc。 There is no one global standard, and.h is definitely very widely used.没有一个全球标准,而且.h 的使用肯定非常广泛。

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.但我会说调用你的 headers.hpp(我也见过几次.hh)比使用.h 更加一致和信息丰富。

You're free to use .h , .H , .hpp , .hxx , .hh , or whathaveyou.您可以自由使用.h.H.hpp.hxx.hh或其他。 Just like you're free to use .c for C sources, and .C , .cpp , .cc , or .cxx for C++ sources. 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.扩展主要是一种约定。

It's like with the other files -.c for C, .cpp for C++ - use.h for C, .hpp for C++. 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.如果您想在不重命名文件的情况下停止该行为,您应该查看 Emacs 配置并尝试更改源代码检测。

Since Emacs has no easy way of finding out whether ah file is C or C++, it's not ridiculous to choose C for.h files and C++ for.hpp files. Since Emacs has no easy way of finding out whether ah 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.注意所有boost header文件都以.hpp为后缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM