简体   繁体   English

包括.cpp而不是标题(.h)

[英]Include .cpp instead of header(.h)

There are some cases when we include .cpp file instead of standard header file (.h), for example: 在某些情况下,我们包含.cpp文件而不是标准头文件(.h),例如:

#include "example.cpp"

instead of 代替

#include "example.h"

It seems to work but is this safe or should I avoid it? 它似乎有用,但是这样安全还是我应该避免它?

What about the compilation time? 编译时间怎么样?

It's lazy coding. 这是懒惰的编码。 Use header files. 使用头文件。 Yes they can increase compile time but they mean that you can easily re-implement chunks of your code, or better yet, another developer could at anytime. 是的,它们可以增加编译时间,但它们意味着您可以轻松地重新实现代码块,或者更好的是,其他开发人员可以随时使用。 The header file serves as a template for what your C/C++ code is going to do. 头文件用作C/C++代码的模板。 It's a bad idea to discard or ignore it. 丢弃或忽略它是一个坏主意。

I agree with Kerrek SB. 我同意Kerrek SB的观点。

I did this once. 我这样做了一次。 I was building an excellent, widely used compression library that needed to be built separately for 8-bit images and for 12-bit images. 我正在构建一个优秀的,广泛使用的压缩库,需要为8位图像和12位图像单独构建。 The cleanest way I could come up with to fit this into the build system was (oversimplifying a bit) to have two master .cpp files, one that set #defines for an 8-bit build, the other for a 12-bit build. 我可以想出的最简单的方法是将它放到构建系统中(过度简化一点)有两个主.cpp文件,一个为8位构建设置#defines,另一个为12位构建。 The master .cpp files then #included the source files of the compression library. 然后,主.cpp文件#included压缩库的源文件。

It's okay to not follow a general rule if you understand the rule well enough to know the reasons for it and why it might not apply in your case. 如果您足够了解规则以了解其原因以及为什么它可能不适用于您的情况,那么不遵循一般规则是可以的。 (But those cases ought to be rare.) (但这些案件应该很少见。)

There are legitimate uses for #include "impl.cpp" : #include "impl.cpp"有合法用途:

  1. testing for access to static/etc variables 测试访问静态/ etc变量

  2. ad hoc templates like these if c++ template mechanism proves inadequate (rare) 如果c ++模板机制证明不足(罕见)这样的临时模板

    #define MACRO (...)

    #include "impl.cpp" // uses MACRO

Note that #include "impl.cpp" can be unsafe it same file is included in separate compilation units that are later linked together. 请注意, #include "impl.cpp"可能不安全,因为相同的文件包含在稍后链接在一起的单独编译单元中。

I have used it before and had no problem but I cannot ensure that this is safe. 我之前使用过它并没有问题,但我不能确保这是安全的。 Sometimes this was the only option for me so I used it, otherwise I will use the .h file. 有时这是我唯一的选择所以我用它,否则我会使用.h文件。

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

相关问题 如何在CMakeLists.txt中包含h / hpp头文件,而不是cpp或其他hpp文件 - How to include h/hpp header files in CMakeLists.txt instead of cpp or other hpp files 是否有充分的理由在 h 文件而不是 cpp 文件中使用包含保护? - Is there a good reason to use include guards in h files instead of cpp files? 包含 .cpp 和 #ifndef 而不是 .h 被认为是不好的做法? - include .cpp with #ifndef instead of .h considered bad practice? #include 在 .h 或 .c / .cpp 中? - #include in .h or .c / .cpp? #include .h或.cpp文件? - #include .h or .cpp file? 为什么我不应该包含 cpp 文件而是使用标题? - Why should I not include cpp files and instead use a header? #包括 <afxinet.h> 在头文件中会导致很多编译错误,但是,将其包含在cpp文件中是可以的 - #include <afxinet.h> in a header file causes a lot of compilation error, however, include it in a cpp file is ok .cpp和.h中包含标头的区别 - difference in including header in .cpp and .h 所有#include-ed的“ .h”头文件都必须与.cpp文件位于同一文件夹中吗? - Do all “.h” header files that are #include-ed have to be in the same folder as your .cpp file? 如果stdafx.h中已包含特定的标头-我是否需要(必须/应该)将其显式包含在.cpp文件中? - If a particular header already included in stdafx.h - do I need to (have to/should to) to explicitly include it in a .cpp file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM