cotire icon indicating copy to clipboard operation
cotire copied to clipboard

Cotire does not generate _pch target for executable build from a single source file

Open leinkemmer opened this issue 7 years ago • 3 comments

I have a target

add_executable(example main.cpp)
cotire(example)

for which cotire does not generate the example_pch target.

$ make help | grep pch
$

As long as there are at least two source files everything works as expected (even if the main.cpp file is just repeated).

add_executable(example main.cpp main.cpp)
cotire(example)

$ make help | grep pch
... all_pch
... example_pch

I assume this is not the desired behavior?

A example of this (based on cotire-1.8.0) can be created easily:

$ git diff
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 92df233..f063c39 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,6 +1,6 @@
 # cotire example project
 
-add_executable(example main.cpp example.cpp log.cpp log.h example.h)
+add_executable(example main.cpp)
 
 # enable warnings
 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
diff --git a/src/main.cpp b/src/main.cpp
index 2ea1af6..dee8bec 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2,11 +2,6 @@
 
 #include <string>
 
-#include "example.h"
-#include "log.h"
-
 int main()
 {
-	std::string msg = example::get_message();
-	logging::info(msg);
 }

leinkemmer avatar Aug 29 '18 23:08 leinkemmer

cotire has a minimal number of target source files. The default is 2, but there is a cmake variable to change it. Have a look at the manual.

romanc avatar Sep 03 '18 15:09 romanc

Thank you, this is very helpful! Is there a particular reason why this is done or rather why using precompiled headers with a single cpp file is considered 'advanced usage'. I am just wondering if I am missing something here.

leinkemmer avatar Sep 04 '18 09:09 leinkemmer

While pre-compiled headers can also speedup the compilation of single cpp (for example, if the header files are large), the technique is mostly used to create a shared pre-compiled header for multiple translation units (ie multiple source files). In case of only one source file, you generate more overhead to create the pre-compiled header than you gain from using it. If you are interested, read up on pre-compiled headers and how cotire works.

romanc avatar Sep 05 '18 08:09 romanc