precompile header inclusion in Visual studio | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

precompile header inclusion in Visual studio

Hi As we know , we generally use stdafx.h as pre compiled header in visual studio for fast compilation. For my visual studio project, C/C++ ---> Precompiled Header property is set as Use (/Yu) with stdafx.h... My project is working fine as I have included stdafx.h in my all cpp files.. Problem is when I try to include two files one .h and another .cpp into my project... These two files let's assume a.h and a.cpp are from third party and should not be changed except to use it in solution... After including these two files , I am getting unexpected error of inclusion of stdafx.h into a.cpp...... How to overcome this error ? I don't want to change precompile header option from property page and also don't want to include stdafx.h into my a.cpp Am I asking for something weird ? If so, I am also confused... The third party who has provided those a.cpp and a.h have provided me sample project for reference... They neither have changed precompile option nor have included stdafx.h into a.cpp... Plz help

15th Aug 2020, 1:50 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Answers
+ 1
Cpp file not need to be included! You need add both files to project. Then you have 2 variants(1 add include stdafx.h to cpp file or secons one select h and cpp file via mouse Rbutton click, open properties and specify do not use precompiled header) Usually 3 part files used without precompile headers. Then include only h file in your own files. Cpp not need to include so according to your variant. 1. add files to solution(project) 2. select added cpp file via mouse r click 3. open properties 4. set not use precompile header(only this file will have such setting) 5. use including only h file in your project solution
16th Aug 2020, 6:53 PM
george
george - avatar
+ 1
Variant 2 is very very very very bad solution!!! Since all files will be included in stdafx, any changes even in one .h file will rebuild full solution. In case when u write comercial code with 2,3 thousand .h files increase time of compiling, linking. So h file consist declaration, cpp file includes implementation OF declaration so h file included in cpp not vice versa. In c++ language for using some code,classes and functions you do not need its implementation just need declaration. So in all files h files cpp files you use only including of H files. It is rule. According to cpp files you need to make using of precompile header for quick compilation for all cpp files. By default all generated cpp files with vs add include stdafx.h at the file begining. If you haven't this entry in cpp error occurs. But using 3 party library it may happen that library constructed without using precompile header, so you need to switch off using precompile header for this library. For example i use such method for dll
18th Aug 2020, 7:05 PM
george
george - avatar
0
Sounds good.... Any specific reason for the specific .h not include for precompile header and include .cpp with same .h ? Isn't it good to get all header into precompiled one or not ?
17th Aug 2020, 8:56 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Precompiled header used only for cpp files. If your cpp file has #include <stdafx.h>, this file can use precompiled header. But if not then without changing settings for this file or modifying the file itself, you will get error. So the best practice is change settings of your solution/project for this specific file(described in previous comment). According to h file. I wrote that you operate only with h file not with cpp. You do not need include cpp file, only h file in your code!
18th Aug 2020, 3:05 AM
george
george - avatar
0
Yeah I got that point... Let me reiterate my understanding... 1. We use generally respective .h into .cpp file... no need to use pre compiled option. 2. Other option is to include all .h into stdafx.h so compiler can fastly generate or reuse same code rather than building again and again.now each cpp doesnot need to include respective .h and all can include stdafx.h file... Use option of pre compiled header as solution level property Correct me if my above understanding is not correct... 3. This is what I feel tricky and don't know why to use this option. This is same as point 2 above but only change is for specific .h and .cpp... Use .h into .cpp instead of going with pre compiled header for this specific file... Why to ommit this file into pre compiled stdafx.h and not following what all we are doing for other files..
18th Aug 2020, 6:53 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Thanks a lot for your effort to make me understand... Appreciate your time... Many thanks... Now it is clear to me
18th Aug 2020, 10:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar