.c 与 .cc 与 .cpp 与 .hpp 与 .h 与 .cxx
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5171502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.c vs .cc vs. .cpp vs .hpp vs .h vs .cxx
提问by user541686
Possible Duplicates:
*.h or *.hpp for your class definitions
Correct C++ code file extension? .cc vs .cpp
I used to think that it used to be that:
我曾经认为它曾经是这样的:
.h
files are header files for C and C++, and usually only contain declarations..c
files are C source code..cpp
files are C++ source code (which can also be C source code).
.h
文件是 C 和 C++ 的头文件,通常只包含声明。.c
文件是 C 源代码。.cpp
文件是 C++ 源代码(也可以是 C 源代码)。
then files like .hpp
, .cc
, and .cxx
came along, and I got totally confused... what's the difference(s) between those? When do you use the "new" ones?
然后像.hpp
,.cc
和这样的文件.cxx
出现了,我完全糊涂了……它们之间有什么区别?你什么时候使用“新”的?
回答by AProgrammer
Historically, the first extensions used for C++ were .c
and .h
, exactly like for C. This caused practical problems, especially the .c
which didn't allow build systems to easily differentiate C++ and C files.
从历史上看,C++ 使用的第一个扩展是.c
and .h
,与 C 完全一样。这引起了实际问题,尤其是.c
它不允许构建系统轻松区分 C++ 和 C 文件。
Unix, on which C++ has been developed, has case sensitive file systems. So some used .C
for C++ files. Other used .c++
, .cc
and .cxx
. .C
and .c++
have the problem that they aren't available on other file systems and their use quickly dropped. DOS and Windows C++ compilers tended to use .cpp
, and some of them make the choice difficult, if not impossible, to configure. Portability consideration made that choice the most common, even outside MS-Windows.
开发了 C++ 的 Unix 具有区分大小写的文件系统。所以有些用于.C
C++文件。其他用过.c++
,.cc
和.cxx
。 .C
并且.c++
存在它们在其他文件系统上不可用并且它们的使用迅速下降的问题。DOS 和 Windows C++ 编译器倾向于使用.cpp
,并且其中一些使选择变得困难,如果不是不可能的话,配置。可移植性考虑使该选择成为最常见的选择,即使在 MS-Windows 之外也是如此。
Headers have used the corresponding .H
, .h++
, .hh
, .hxx
and .hpp
. But unlike the main files, .h
remains to this day a popular choice for C++ even with the disadvantage that it doesn't allow to know if the header can be included in C context or not. Standard headers now have no extension at all.
头已经使用相应的.H
,.h++
,.hh
,.hxx
和.hpp
。但与主文件不同,.h
时至今日它仍然是 C++ 的流行选择,即使它的缺点是它不允许知道头文件是否可以包含在 C 上下文中。标准标题现在根本没有扩展名。
Additionally, some are using .ii
, .ixx
, .ipp
, .inl
for headers providing inline definitions and .txx
, .tpp
and .tpl
for template definitions. Those are either included in the headers providing the definition, or manually in the contexts where they are needed.
此外,有些使用.ii
, .ixx
, .ipp
,.inl
用于提供内联定义的标头和.txx
,.tpp
以及.tpl
模板定义。这些要么包含在提供定义的标题中,要么包含在需要它们的上下文中。
Compilers and tools usually don't care about what extensions are used, but using an extension that they associate with C++ prevents the need to track out how to configure them so they correctly recognize the language used.
编译器和工具通常不关心使用了哪些扩展,但是使用与 C++ 关联的扩展可以防止需要跟踪如何配置它们以便正确识别所使用的语言。
2017 edit: the experimental module support of Visual Studio recognize .ixx
as a default extension for module interfaces, clang++ is recognizing .c++m
, .cppm
and .cxxm
for the same purpose.
2017 编辑:Visual Studio 的实验性模块支持识别.ixx
为模块接口的默认扩展,clang++ 正在识别.c++m
,.cppm
并且.cxxm
出于同样的目的。
回答by Bo Persson
Those extensions aren't really new, they are old. :-)
这些扩展并不是真正的新,它们是旧的。:-)
When C++ was new, some people wanted to have a .c++ extension for the source files, but that didn't work on most file systems. So they tried something close to that, like .cxx, or .cpp instead.
当 C++ 刚出现时,有些人希望源文件有一个 .c++ 扩展名,但这在大多数文件系统上不起作用。所以他们尝试了类似的东西,比如 .cxx 或 .cpp。
Others thought about the language name, and "incrementing" .c to get .cc or even .C in some cases. Didn't catch on that much.
其他人考虑了语言名称,并在某些情况下“递增” .c 以获得 .cc 甚至 .C 。没有抓住那么多。
Some believed that if the source is .cpp, the headers ought to be .hpp to match. Moderately successful.
有些人认为,如果源是 .cpp,则标头应该是 .hpp 才能匹配。中等成功。
回答by Martin Beckett
It really doesn't matter.
If you feed .c to a c++ compiler it will compile as cpp, .cc/.cxx is just an alternative to .cpp used by some compilers.
真的没关系。
如果将 .c 提供给 c++ 编译器,它将编译为 cpp,.cc/.cxx 只是某些编译器使用的 .cpp 的替代。
.hpp is an attempt to distinguish header files where there are significant c and c++ differences. A common usage is for the .hpp to have the necessary cpp wrappers or namespace and then include the .h in order to expose a c library to both c and c++.
.hpp 试图区分存在显着 c 和 c++ 差异的头文件。一个常见的用法是让 .hpp 拥有必要的 cpp 包装器或命名空间,然后包含 .h 以便向 c 和 c++ 公开 ac 库。
回答by crodriguezo
I use ".hpp" for C++ headers and ".h" for C language headers. The ".hpp" reminds me that the file contains statements for the C++ language which are not valid for the C language, such as "class" declarations.
我对 C++ 头文件使用“.hpp”,对 C 语言头文件使用“.h”。“.hpp”提醒我该文件包含对 C 语言无效的 C++ 语言语句,例如“类”声明。
回答by Parsa Jamshidi
Talking about .hpp extension, I find it useful when people are supposed to know that this header file contains C++an not C, like using namespaces or template etc, by the moment they see the files, so they won't try to feed it to a Ccompiler! And I also like to name header files which contain not only declarations but implementations as well, as .hpp files. like header files including template classes. Although that's just my opinion and of course it's not supposed to be right! :)
谈到 .hpp 扩展名,当人们应该知道这个头文件包含C++而不是C时,我发现它很有用,比如使用命名空间或模板等,当他们看到文件时,所以他们不会尝试提供它到C编译器!我还喜欢将不仅包含声明而且包含实现的头文件命名为 .hpp 文件。像包括模板类的头文件。虽然这只是我的意见,当然它不应该是正确的!:)
回答by Boojum
Generally, .c and .h files are for C or C-compatible code, everything else is C++.
通常,.c 和 .h 文件用于 C 或 C 兼容代码,其他一切都是 C++。
Many folks prefer to use a consistent pairing for C++ files: .cpp with .hpp, .cxx with .hxx, .cc with .hh, etc. My personal preference is for .cpp and .hpp.
许多人更喜欢对 C++ 文件使用一致的配对:.cpp 与 .hpp、.cxx 与 .hxx、.cc 与 .hh 等。我个人更喜欢 .cpp 和 .hpp。