What is the meaning of a .cpp file containing only one #include?

I am starting some work using a third-party library, and when I created it in Visual Studio 2010, I noticed that I received this linker warning ( LNK4221 ) more than once . I looked at the sources used to create the object files that were linked, and found that the entire implementation for them is in the header files. Interestingly, I also noticed that the project included the corresponding .cpp files containing only #include for the header with the implementation.

I am curious - what is the meaning of this and why I want to use this technique? If the .cpp files do not add any value to the project, why don't I just delete them to get rid of the linker warnings?

I tried to find similar questions, but did not find anything interesting. If you know about them, please connect them.

+7
source share
2 answers

I use this to make sure that the header is in at least one file included in the first position. In doing so, I am sure that the header compiles on its own.

To link the linker so as not to generate a warning, you could use an external variable with a very large variable:

int variable_with_a_name_that_includes_the_file_name_somehow = 42; 
+1
source

Was there a single #include d stdafx.h file? I. In this case, you are dealing with precompiled headers . The usual setting is for one .cpp file with the option "generate precompiled headers", and the rest .cpp files in your project have "use pch".

+2
source

All Articles