What is the point of a .cpp file containing only a single #include?

I am starting some work using a third party library and when building it in Visual Studio 2010, I noticed I was receiving this linker warning many times (LNK4221). I looked at the sources used in creating the object files that were being linked and found that all of the implementation for these is located in the header files. Interestingly, I also noticed the project included corresponding .cpp files containing only a #include for the header with the implementation.

I am curious - what is the point of this and why would I want to use this technique? If the .cpp files aren't adding any value to the project, why shouldn't I just remove them to get rid of the linker warnings?

I tried searching for similar questions, but didn't find anything of interest. If you know of any, please link them.


I'm using this to make sure, that the header is at least in one file included at the first position. By doing so, I make sure that the header is compilable on it's own.

To convence the linker to not issue a warning, one could use an external variable with a very large variable:

int variable_with_a_name_that_includes_the_file_name_somehow = 42;

Was the single #include d file stdafx.h? I. That case, you're dealing with precompiled headers. The normal setup is for one .cpp file having "generate precompiled headers" compiler option, and the rest of the .cpp files in your project having "use pch".

链接地址: http://www.djcxy.com/p/11018.html

上一篇: Apache2上的FallbackResource(2.2.22(Ubuntu))

下一篇: 只包含一个#include的.cpp文件的要点是什么?