javaLover javaLover -4 years ago 99
C++ Question

unit test - recompile only around "int main()" to reduce compilation time

A project in my Visual Studio solution can be divided into :-

Library.h, Library.cpp
User.h, User.cpp (#include Library.h)
Main.h, Main.cpp (#include User.h) has "int main()"


(The real project is far bigger than this.)

Whenever I edit any
Library.h
file and press
F5
, it will recompile the whole project. It makes sense.

Now I want to code/debug
Library
and test it alone, and I don't want to recompile everything because it costs more time.

Therefore :-


  • I created
    Main2.cpp
    that
    #include
    only
    Library.h
    , and have no code involving
    User
    at all.

  • I commented out the whole
    main.cpp
    .



Here is my main2.cpp :-

#include "Library.h"
int main(){ .... some code about library ....}


When I press F5, it compiled all
.cpp
and run fine. That still makes sense.

Problem



If I edited
Library.h
, and press F5 again, it will recompile :-


  • library.cpp

  • User.cpp
    <--- no!

  • main.cpp
    <--- no!

  • main2.cpp



Question



How to make Visual Studio recompile only necessary files to save compilation time?

(compile only
library.cpp
and
main2.cpp
in this case)

I guess the need to recompile a lot of files is that there may be some static function inside
User
that do something about
library
.

What if I know for sure that there are not?

Even if there is something, I want to (risk) ignore it.

Answer Source

Separate your solution into 3 projects:

  • Library
  • LibraryTests
  • Main

When you don't want to re-compile 'Main', just right-click it and unload the project.

You should probably look to break up Library.h into smaller pieces, so that the dependencies are more self-contained.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download