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

后端 未结 9 709
予麋鹿
予麋鹿 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 17:02

    Pre-compiled headers are predicated on the idea that everything will include the same set of stuff. If you want to make use of pre-compiled headers then you have to live with the dependencies that this implies. It comes down to a trade-off of the dependencies vs the build speed. If you can build in a reasonable time with the pre-compiled headers turned off then by all means do it.

    Another thing to consider is that you can have one pch per library. So you may be able to split up your code into smaller libraries and have each of them have a tighter set of dependencies.

    0 讨论(0)
  • 2020-12-05 17:04

    No, there is probably NOT a better way.

    However, for a given individual .cpp file, you might decide that you don't need the precompiled header. You could modify the settings for that one .cpp file and remove the stdafx.h line.

    (Actually, though, I don't how the pre-compiled header scheme is interferring with the writing of your unit tests).

    0 讨论(0)
  • 2020-12-05 17:04

    My advice is - don't remove precompiled headers unless you want to make your builds painfully slow. You basically have three options here:

    1. Get rid of precompiled headers (not recommended)
    2. Create a separate library for the legacy code; that way you can build it separately.
    3. Use multiple precompiled headers within a single project. You can select individual C++ files in your Solution Explorer and tell them which precomiled header to use. You would also need to setup your OtherStdAfx.h/cpp to generate a precompiled header.
    0 讨论(0)
提交回复
热议问题