Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable precompiled header for a single file

I am working on a project with many .cpp files on vs2013, and using a precompiled header for them. I am using CMake to build my project.

But I have one .c file (let's call it xyz.c) for which I want to disable precompiled header.

I tried several methods, but if I enable precompiled headers to all .cpp file, it automatically turns on for the .c file as well. This is what I tried:

set_source_files_properties (xyz.c
  PROPERTIES COMPILE_FLAGS /Y-xyz.c )

Assuming that /Yu is on for all files, I just try to turn off this option for xyz.c.

If any one knows any method, please let me know.

like image 621
hrshl90 Avatar asked Nov 30 '16 11:11

hrshl90


People also ask

How do I turn off precompiled headers?

To turn off precompiled headersSelect the Configuration properties > C/C++ > Precompiled Headers property page. In the property list, select the drop-down for the Precompiled Header property, and then choose Not Using Precompiled Headers. Choose OK to save your changes.

Why do I get precompiled headers?

Precompiled headers were invented for one purpose: to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time. Speed is important! The faster you compile, the faster you can complete the feedback loop to see if recent changes were successful.

Do I need precompiled headers?

Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units.

How does a precompiled header work?

The precompiled header is compiled only when it, or any files it includes, are modified. If you only make changes in your project source code, the build will skip compilation for the precompiled header. The compiler options for precompiled headers are /Y .


2 Answers

Starting from cmake 3.16 setting /Y- compiler option will not work. Correct way to disable precompiled headers for individual file is like this:

set_source_files_properties(non-pch.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)

Documentation: https://cmake.org/cmake/help/v3.16/prop_sf/SKIP_PRECOMPILE_HEADERS.html

(Not so useful anyway)

like image 192
TarmoPikaro Avatar answered Sep 30 '22 18:09

TarmoPikaro


/Y- doesn't take arguments. Try:

set_source_files_properties(xyz.c
  PROPERTIES COMPILE_FLAGS /Y-)

Alternatively, instead of using /Yu on all files and disable it only for your .c file, you can use the opposite approach and only use /Yu for the .cpp files. Given your .cpp files are listed in a variable SOURCES and my_pch.h is your precompiled header:

set_source_files_properties(${SOURCES}
  PROPERTIES COMPILE_FLAGS /Yumy_pch.h)
like image 23
rgmt Avatar answered Sep 30 '22 17:09

rgmt