Is there a way to use pre-compiled headers in VC++ without requiring stdafx.h?

后端 未结 9 775
予麋鹿
予麋鹿 2020-12-05 16:20

I\'ve got a bunch of legacy code that I need to write unit tests for. It uses pre-compiled headers everywhere so almost all .cpp files have a dependecy on stdafx.h which is

9条回答
  •  甜味超标
    2020-12-05 16:39

    Precompiled headers can save a lot of time when rebuilding a project, but if a precompiled header changes, every source file depending on the header will be recompiled, whether the change affects it or not. Fortunately, precompiled headers are used to compile, not link; every source file doesn't have to use the same pre-compiled header.

    pch1.h:

    #include 
    #include ...
    


    pch1.cpp:

    #include "pch1.h"
    


    source1.cpp:

    #include "pch1.h"
    [code]
    


    pch2.h:

    #include 
    #include ...
    


    pch2.cpp:

    #include "pch2.h"
    


    source2.cpp

    #include "pch2.h"
    [code]
    

    Select pch1.cpp, right click, Properties, Configuration Properties, C/C++, Precompiled Headers.
    Precompiled Header : Create(/Yc)
    Precompiled Header File: pch1.h
    Precompiled Header Output File: $(intDir)pch1.pch

    Select source1.cpp
    Precompiled Header : Use(/Yu)
    Precompiled Header File: pch1.h
    Precompiled Header Output File: $(intDir)pch1.pch (I don't think this matters for /Yu)

    Do the same thing for pch2.cpp and source2.cpp, except set the Header File and Header Output File to pch2.h and pch2.pch. That works for me.

提交回复
热议问题