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

时间:2020-12-21 15:08:44

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?

编译时间怎么样?

4 个解决方案

#1


20  

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. It's a bad idea to discard or ignore it.

这是懒惰的编码。使用头文件。是的,它们可以增加编译时间,但它们意味着您可以轻松地重新实现代码块,或者更好的是,另一个开发人员可以随时使用。头文件用作C / C ++代码的模板。抛弃或忽略它是一个坏主意。

#2


7  

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. 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. The master .cpp files then #included the source files of the compression library.

我这样做了一次。我正在构建一个优秀的,广泛使用的压缩库,需要为8位图像和12位图像单独构建。我可以想出的最简单的方法是将它放到构建系统中(过分简化)有两个主.cpp文件,一个为8位构建设置#defines,另一个为12位构建。然后,主.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.)

如果您足够了解规则以了解其原因以及为什么它可能不适用于您的情况,那么不遵循一般规则是可以的。 (但这些案件应该很少见。)

#3


3  

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 (...)

    #define MACRO(...)

    #include "impl.cpp" // uses MACRO

    #include“impl.cpp”//使用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”可能不安全,因为同一文件包含在稍后链接在一起的单独编译单元中。

#4


1  

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文件。

#1


20  

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. It's a bad idea to discard or ignore it.

这是懒惰的编码。使用头文件。是的,它们可以增加编译时间,但它们意味着您可以轻松地重新实现代码块,或者更好的是,另一个开发人员可以随时使用。头文件用作C / C ++代码的模板。抛弃或忽略它是一个坏主意。

#2


7  

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. 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. The master .cpp files then #included the source files of the compression library.

我这样做了一次。我正在构建一个优秀的,广泛使用的压缩库,需要为8位图像和12位图像单独构建。我可以想出的最简单的方法是将它放到构建系统中(过分简化)有两个主.cpp文件,一个为8位构建设置#defines,另一个为12位构建。然后,主.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.)

如果您足够了解规则以了解其原因以及为什么它可能不适用于您的情况,那么不遵循一般规则是可以的。 (但这些案件应该很少见。)

#3


3  

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 (...)

    #define MACRO(...)

    #include "impl.cpp" // uses MACRO

    #include“impl.cpp”//使用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”可能不安全,因为同一文件包含在稍后链接在一起的单独编译单元中。

#4


1  

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文件。