43

I just want to know if we can compile a single file/class in visual studio.I often change just a single file but end up compiling the entire project.THis might be a trivial case but will be very helpful:) I am using visual studio 2005 working on C# project in .net 2.0

Ravisha
  • 3,261
  • 9
  • 39
  • 66
  • Wow - the things you forget about when using ReSharper! That does has a lot of syntax checking (which is essential what Ctrl+F7 does) on the fly – MPritchard Feb 25 '10 at 07:28
  • 1
    Note: in Visual Studio 2012 (and possibly above) the buid only compiles modified files and files affected (e.g. after editing a header file) – jave.web Feb 18 '15 at 17:04

4 Answers4

53

Ctrl+F7 will compile only the active source file.

Look for the Compile item at the bottom of the Build menu.

Of course, you'll still have to do a build before you can test, but if you just want a quick sanity check after modifying a source file, this can be handy.

Scott Smith
  • 3,900
  • 2
  • 31
  • 63
  • Thats what i wanted.Thanks scott. – Ravisha Feb 25 '10 at 07:16
  • 3
    Scott, you made me quite curious. How do you get a 'Compile' item at the bottom of the Build menu in VS 2005 in a C# project ? I cannot see it on my copy of VS. – Timores Feb 25 '10 at 15:52
  • 3
    @Timores - Whoops, I was thinking this was a C++ question; looks like in a C# solution, the smallest thing you can build is a single project, using *Build Selection* (**SHIFT+F6**). – Scott Smith Mar 01 '10 at 00:02
  • 1
    how do you now debug a single file? – MySchizoBuddy Dec 26 '13 at 13:14
  • 1
    To then debug with this single file compiled, you must link the project: Build > Project Only > Link Only (assuming a C++ project for a .dll or .exe) – JDiMatteo Apr 19 '16 at 22:38
4

The granularity of compilation is the DLL, so there is no way to do what you are asking.

Or do you mean that you compile the whole solution for a singe change (or at least VS checks if all projects require building) ? There is an option under 'project and solution' / 'Build and run', 'only build startup projects and dependencies on Run' that helps.

Edited: Ctrl-f7 for 'build file' is for C++ projects.

Timores
  • 14,439
  • 3
  • 46
  • 46
4

Okay, I think I know what you are looking for. I think there's a better way to word this, but what you need to do, is to open the Visual Studio Command Prompt Found in the Start Menu. Within it you can use the "cl" command to compile C/C++. Usage: cl [ option... ] filename... [ /link linkoption... ]

As in:

cl /O2 test.c

Santeri
  • 49
  • 1
0

Compile a project — ignoring all irrelevant files. (See also https://stackoverflow.com/a/18940150/2422360 for an answer I wrote that has a fuller look at this.)

Community
  • 1
  • 1
sbhal
  • 145
  • 10