locked
Why no basic "compile" option? RRS feed

  • Question

  • This has bugged me for years, why can't I simply get the VS 2008 IDE to compile a specific src file?

    I work on very large projects and sometimes I am tweaking a file and jsut want to make sure it compiles cleanly.

    Hoeever, the only option I have is to build or rebuild the project and this takes a while.

    How about adding this (perhaps VS 2010 has this???)

    Thx

    Cap'n

    Monday, November 16, 2009 2:16 PM

Answers

  • The smallest unit of compiled stuff in .NET is an assembly so it can't really compile a single file without building the rest of the assembly. You can break your project up into a lot of assemblies and hook them all together, although that would be some extra work.

    But if you just want an option to check whether a file can compile, that should be possible. It still might not compile depending on things it uses that are in other files (for example, it might use an invalid property from a class defined in another file) but you should be able to check basic syntax.

    Rod
    Monday, November 16, 2009 2:30 PM
  • Compilation in C# is not like C++.  C++ compilations involve several phases starting with the preproc and ending with the linker.  C# does not have these phases.  You compile the source file directly to an assembly.  There is no intermediary file (at least that gets persisted).  Hence compilation requires that all information be available at the time of compilation rather than waiting until the linker to fix up imports.

    Unlike C++ there are more settings that are needed than just compilation flags.  You would need all the assembly information as well anything the assembly might rely on.  A simple compilation would just fail if you reference any other assemblies (which you always will) w/o project-level settings.

    Having said all that the C# language service does support single file compilation.  It does it in the background after you change files.  So any compilation errors should appear in the error list while you are editing your code.  It doesn't necessary detect all errors but it'll get most of them.

    C# compilations don't take that long in my experience.  I have projects with several hundred files in them and compilation is in the seconds.  There really isn't a need, IMO, for single-file compilation.  But that doesn't mean you can't create it.  You can quite easily create a macro or VS addin to expose the functionality you want.  The problem is going to be that you'll need to get the project-level information needed for compilation including the paths to the referenced assemblies, entry point information, resource definitions and any types that are being referenced.  Exactly how the C# background compiler does it is beyond me but I suspect it only does a syntactical scan of the code with basic semantic checks.

    As an aside VS2010 does not have this feature either.  If it is really important to you then you should submit a request to the Connect site to see if they'll add it in a future release.

    Michael Taylor - 11/16/09
    http://p3net.mvps.org
    Monday, November 16, 2009 2:41 PM
    Moderator

All replies

  • The smallest unit of compiled stuff in .NET is an assembly so it can't really compile a single file without building the rest of the assembly. You can break your project up into a lot of assemblies and hook them all together, although that would be some extra work.

    But if you just want an option to check whether a file can compile, that should be possible. It still might not compile depending on things it uses that are in other files (for example, it might use an invalid property from a class defined in another file) but you should be able to check basic syntax.

    Rod
    Monday, November 16, 2009 2:30 PM
  • Compilation in C# is not like C++.  C++ compilations involve several phases starting with the preproc and ending with the linker.  C# does not have these phases.  You compile the source file directly to an assembly.  There is no intermediary file (at least that gets persisted).  Hence compilation requires that all information be available at the time of compilation rather than waiting until the linker to fix up imports.

    Unlike C++ there are more settings that are needed than just compilation flags.  You would need all the assembly information as well anything the assembly might rely on.  A simple compilation would just fail if you reference any other assemblies (which you always will) w/o project-level settings.

    Having said all that the C# language service does support single file compilation.  It does it in the background after you change files.  So any compilation errors should appear in the error list while you are editing your code.  It doesn't necessary detect all errors but it'll get most of them.

    C# compilations don't take that long in my experience.  I have projects with several hundred files in them and compilation is in the seconds.  There really isn't a need, IMO, for single-file compilation.  But that doesn't mean you can't create it.  You can quite easily create a macro or VS addin to expose the functionality you want.  The problem is going to be that you'll need to get the project-level information needed for compilation including the paths to the referenced assemblies, entry point information, resource definitions and any types that are being referenced.  Exactly how the C# background compiler does it is beyond me but I suspect it only does a syntactical scan of the code with basic semantic checks.

    As an aside VS2010 does not have this feature either.  If it is really important to you then you should submit a request to the Connect site to see if they'll add it in a future release.

    Michael Taylor - 11/16/09
    http://p3net.mvps.org
    Monday, November 16, 2009 2:41 PM
    Moderator