C++ 代码文件扩展名?.cc 与 .cpp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1545080/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:22:03  来源:igfitidea点击:

C++ code file extension? .cc vs .cpp

c++filenames

提问by Jessica

I have seen C++ code saved as both .ccand .cppfiles. Is there a difference between the two?

我已经看到 C++ 代码同时保存为.cc.cpp文件。两者有区别吗?

The Google style guideseems to suggest .cc, but provides no explanation.

谷歌的风格指南似乎在暗示.cc,但没有提供任何解释。

I am mainly concerned with programs on Linux systems.

我主要关注 Linux 系统上的程序。

回答by JaredPar

At the end of the day it doesn't matter because C++ compilers can deal with the files in either format. If it's a real issue within your team, flip a coin and move on to the actual work.

归根结底,这并不重要,因为 C++ 编译器可以处理任何一种格式的文件。如果这是您团队中的真正问题,请掷硬币并继续进行实际工作。

回答by Clifford

GNU GCC recognises all of the following as C++ files, and will use C++ compilation regardless of whether you invoke it through gcc or g++: .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx.

GNU GCC 将以下所有文件都识别为 C++ 文件,并且无论您是通过 gcc 还是 g++ 调用它,都将使用 C++ 编译:.C, .cc, .cpp, .CPP, .c++, .cp, 或.cxx

Note the .C- case matters in GCC, .cis a C file whereas .Cis a C++ file (if you let the compiler decide what it is compiling that is).

请注意,.C-case 在 GCC 中.c很重要,它是一个 C 文件,.C而是一个 C++ 文件(如果你让编译器决定它正在编译的是什么)。

GCC also supports other suffixes to indicate special handling, for example a .iifile will be compiled as C++, but not pre-processed (intended for separately pre-processed code). All the recognised suffixes are detailed at gcc.gnu.org

GCC 还支持其他后缀来表示特殊处理,例如一个.ii文件将被编译为 C++,但不进行预处理(用于单独的预处理代码)。所有公认的后缀都在gcc.gnu.org 上有详细说明

回答by John S.

Great advice on which to use for the makefile and other tools, considering non-compiler tools while deciding on which extension to use is a great approach to help find an answer that works for you.

关于将哪个用于 makefile 和其他工具的好建议,在决定使用哪个扩展时考虑非编译器工具是帮助找到适合您的答案的好方法。

I just wanted to add the following to help with some .ccvs .cppinfo that I found. The following are extensions broken down by different environments (from the "C++ Primer Plus" book):

我只是想添加以下内容来帮助我找到一些.ccvs.cpp信息。以下是按不同环境细分的扩展(来自“C++ Primer Plus”一书):

Unix uses: .C, .cc, .cxx, .c

Unix 使用:.C, .cc, .cxx,.c

GNU C++ uses: .C, .cc, .cxx, .cpp, .c++

GNU C++ 使用: .C, .cc, .cxx, .cpp,.c++

Digital Mars uses: .cpp, .cxx

数字火星使用: .cpp.cxx

Borland C++ uses: .cpp

Borland C++ 使用: .cpp

Watcom uses: .cpp

Watcom 用途: .cpp

Microsoft Visual C++ uses: .cpp, .cxx, .cc

Microsoft Visual C++ 使用: .cpp, .cxx,.cc

Metrowerks CodeWarrior uses: .cpp, .cp, .cc, .cxx, .c++

Metrowerks CodeWarrior 使用: .cpp, .cp, .cc, .cxx,.c++

The different environments support different extensions. I too was looking to answer this question and found this post. Based on this post I think I might go with .hppand .cppfor ease of cross-platform/cross-tool recognition.

不同的环境支持不同的扩展。我也想回答这个问题并找到了这篇文章。基于这篇文章,我想我可能会采用.hpp.cpp简化跨平台/跨工具识别。

回答by Ryu

.cppis the recommended extension for C++ as far as I know. Some people even recommend using .hppfor C++ headers, just to differentiate from C.

.cpp据我所知,是 C++ 的推荐扩展。有些人甚至推荐使用.hppC++ 头文件,只是为了与 C 区分开来。

Although the compiler doesn't care what you do, it's personal preference.

尽管编译器不在乎你做什么,但这是个人喜好。

回答by NewbiZ

I personally use .ccextension for implementation files, .hhfor headers, and .inlfor inline/templates.

我个人.cc对实现文件、.hh标题和.inl内联/模板使用扩展名。

As said before, it is mainly a matter of taste.

如前所述,这主要是品味问题。

From what I've seen, .ccseems to be more "open source projects oriented", as it is advised in some great open source software coding styles, whereas .cppseems to be more Windowish.

从我所见.cc似乎更“面向开源项目”,因为在一些优秀的开源软件编码风格中建议这样做,而 . cpp似乎更像Windowish。

--- EDIT

- - 编辑

As mentioned, this is "from what i've seen", it may be wrong. It's just that all Windows projects I've worked on used .cpp, and a lot of open source projects (which are mainly on unix-likes) use .cc.

如前所述,这是“据我所见”,这可能是错误的。只是我参与过的所有 Windows 项目都.cpp使用.cc.

Examples coding styles using .cc:

示例编码样式使用.cc

回答by NewbiZ

Other file extensions used include .cxxand .C(capital C). I believe Bjarne Stroustrup used .Coriginally. .cppis the name of the C preprocessor so it's unfortunate that it was used for C++ as well.

使用的其他文件扩展名包括.cxx.C(大写 C)。我相信 Bjarne Stroustrup.C最初使用过。.cpp是 C 预处理器的名称,因此不幸的是它也用于 C++。

回答by Roland Rabien

The other option is .cxxwhere the xis supposed to be a plus rotated 45°.

另一种选择是.cxx,其中x被认为是一个加旋转45°。

Windows, Mac and Linux all support .c++so we should just use that.

Windows、Mac 和 Linux 都支持,.c++所以我们应该使用它。

回答by Ben Voigt

Several people saying .ccdoesn't stand for anything? It might. C++ started life as "C with Classes".

几个人说.cc不代表什么?它可能。C++ 开始时是“C with Classes”。

True that .ccand .cppare also command names on most Unix systems (c compiler and c preprocessor respectively).

确实如此.cc.cpp并且也是大多数 Unix 系统上的命令名称(分别为 c 编译器和 c 预处理器)。

I use .cppexclusively, but I started on Windows. .ccis more a Unix convention, although I see it less and less even there. GNU make has rules for .cppso that's probably preferred, it will work by default on both Windows and everything else. On the other hand modern C++ uses no extension at all for headers, I really don't like that. All my projects use .hfor header files, and they support both C and C++ as much as possible via extern "C"and testing __cplusplus.

.cpp专门使用,但我是从 Windows 开始的。.cc更像是一种 Unix 约定,尽管我在那里看到它的次数越来越少。GNU make 有一些规则,.cpp所以这可能是首选,默认情况下它可以在 Windows 和其他任何东西上工作。另一方面,现代 C++ 根本不使用头文件扩展,我真的不喜欢那样。我所有的项目都.h用于头文件,它们通过extern "C"和 testing尽可能地支持 C 和 C++ __cplusplus

回答by Toji

I've personally never seen .ccin any project that I've worked on, but in all technicality the compiler won't care.

我个人从未.cc在我参与过的任何项目中看到过,但在所有技术方面,编译器都不会关心。

Who will care is the developers working on your source, so my rule of thumb is to go with what your team is comfortable with. If your "team" is the open source community, go with something very common, of which .cppseems to be the favourite.

谁会关心在你的源代码上工作的开发人员,所以我的经验法则是选择你的团队喜欢的东西。如果您的“团队”是开源社区,请选择一些非常常见的东西,其中.cpp似乎是最受欢迎的。

回答by Ben S

Just follow the convention being used for by project/team.

只需遵循项目/团队使用的约定即可。