Microsoft 开发人员网络 > 论坛主页 > Visual C++ General > Avoiding recompilation of C++ modules used in multiple projects
提出问题提出问题
 

已答复Avoiding recompilation of C++ modules used in multiple projects

  • 2009年11月4日 13:40gg3 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I have an unmanaged C++ DLL. In my solution is a VC project for the DLL, and a separate project, Win32 Console App, that contains my unit tests.

    My test project compiles several .cpp files from the DLL project, so that the symbols defined therein can resolve at runtime.

    When I build my solution, for every .cpp file that is used in my test project, I have to wait for that file to be compiled once for the DLL project, and then AGAIN for my test project.  How can I avoid this double compilation?

    I tried to make both projects share the same folder for Output and Intermediate locations, hoping that the test project would find the .obj files there and use them, but it didn't work.

    Any other ideas on how I can avoid double-compiling every one of my modules-under-test?

答案

  • 2009年11月23日 20:39gg3 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Finally found a satisfactory solution to this problem.

    What I did is change the test project's linker input additional dependencies to include the obj files for the already-compiled modules.

    For example, if my solution has two projects, DLLProject and TestProject, and TestProject calls into symbols defined in DLLProject\Module.cpp, then the TestProject linker input additional dependencies needs to include "Module" (the .obj suffix is implicit), and the linker additional library directories needs to include something like "..\DLLProject\Debug", or whatever folder your obj's are written to.

    I also used this same technique to link with a .res file (compiled form of .rc).
    • 已标记为答案gg3 2009年11月23日 20:39
    •  

全部回复

  • 2009年11月4日 15:00«_Superman_»MVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    You could try and make pre-compiled headers out of the .cpp files that you need to test.
    The unit tests could then be made to use those pre-compiled headers.

    You can change the settings by right clicking on the .cpp file in solutions explorer and selecting properties.
    In the Configuration Properties -> C/C++ -> Precompiled Headers change the Create/Use Precompiled Header option.


    «_Superman_»
    Microsoft MVP (Visual C++)
  • 2009年11月11日 16:48gg3 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I'd rather not make all my .cpp files precompiled headers, because this project is in active development.

    If I understand precompiled headers right, they're not appropriate for code that changes often.
  • 2009年11月12日 4:39Brian MuthMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    The problem I see is that the test code is using the same cpp files. It's better to consider building a DLL out of the common cpp files and then use test code that calls the DLL.
  • 2009年11月16日 12:45gg3 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    You mean a DLL separate from my primary project DLL?  (The code that I'm testing is already being built into a DLL, a VSPackage, actually.)  Wouldn't I then run into the same re-compilation issues when building this DLL?

    Further, my test program can't just call into the primary project DLL because many of the functions I'm testing aren't exported in the DLL.
  • 2009年11月23日 20:39gg3 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Finally found a satisfactory solution to this problem.

    What I did is change the test project's linker input additional dependencies to include the obj files for the already-compiled modules.

    For example, if my solution has two projects, DLLProject and TestProject, and TestProject calls into symbols defined in DLLProject\Module.cpp, then the TestProject linker input additional dependencies needs to include "Module" (the .obj suffix is implicit), and the linker additional library directories needs to include something like "..\DLLProject\Debug", or whatever folder your obj's are written to.

    I also used this same technique to link with a .res file (compiled form of .rc).
    • 已标记为答案gg3 2009年11月23日 20:39
    •  
  • 2009年11月23日 20:57Brian MuthMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    That's very clever!