views:

1044

answers:

2

Hi stackoverflowers

is there any way to build/run small C++ programs, in Visual Studio without creating projects, like in old days in IDEs like turbo c++ you could just compile and run without creating projects.

Thanks in advance!

+7  A: 

Within the actual IDE, I don't think it's possible to run a small program, you have to use the command line like Matt suggested.

The solution I have for this problem, is that I have one project on my computers called "sandbox", which just has a main.cpp, and it's where I can mess around.

It let's me try things out here and there, but I never have to keep starting a new project.

GMan
miss pressing ctrl+v , ctrl+F9 :)
The down-side of this is sometimes "throw away code" turns out to be helpful later, and if you do it this way you've really thrown it away.
Matthew Flaschen
Yea I was gonna warn about it. :/ But it's in the comments now.
GMan
My personal solution is to have a Tests folder with only test application projects that can be a little main function or more. That way I have each test named. Creating a simple console program in MSVC takes about 2 second ( + time to type the project name).
Klaim
+2  A: 

GMan's idea of having a 'sandbox' project is a good one, as it allows for easily trying out multi-file tests. I call mine "cppTest".

However, if you just want to be able to compile whatever C or C++ file you happen to have open, just create a simple "External Tool". Actually, it's not as simple as it probably should be.

First, create a batch file that will set up the compiler environment and run the compiler. Something like the following:

@rem - runcl.cmd
@rem     a batch file to drive simple VC9 compiles
#rem
@echo off

set LIBRARIES=kernel32.lib user32.lib advapi32.lib shlwapi.lib oleaut32.lib
set WIN32_WINNT=0x0500

call "%ProgramFiles%\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"

echo Visual C/C++ 2008 (VC 9.0) Compile...
set CC="%ProgramFiles%\Microsoft Visual Studio 9.0\VC\bin\cl.exe" /Zi /EHsc -D_WIN32_WINNT=%WIN32_WINNT% %1 /link /incremental:no %LIBRARIES%
echo %CC%
%CC%

In the "Tools/External Tools..." dialog, add a new item and fill in the following fields:

  • Title: &Compile File
  • Command: c:\path\to\runcl.cmd
  • Arguments: $(ItemPath)
  • Initial Directory: $(ItemDir)

  • check the "Use Output Window" box

Now you can use the "Compile File" item in the Tools menu to compile whatever is the currently open file. You can even double click on the errors in the output window to take you to the lines with errors.

There are some limitations with this some of which you can fix by fancying up the batch file or maybe with a Visual Studio macro (I'm not very familiar with VS macros).

  1. if you haven't saved the file, the compile will run against the most recent save. There's no option in the External Tools configuration to force a save.
  2. if you run the command and there's not a C or C++ file active, the batch file will fall over
  3. there are probably quite a few other areas where the batch file will get confused

The nice thing about a batch file like this is that you can also use it to help integrate the compiler into various editors that let you call external tools, like UltraEdit, Zeus Editor, EditPad Pro, PSPad, Programmer's Notepad, etc. etc.

If you like, I've posted a batch file that I use to integrate several compilers into editors like the above. This batch file handles several compilers including various MSVC compilers from version 6 through version 9, Digital Mars, MinGW and Comeau compilers (the compiler to use is selected by an additional parameter). The batch file is pretty convoluted (unfortunately, that's the nature of Windows batch files that have any kind of complexity). But I find it makes running these things from various editors pretty simple. I can quickly hit a few keys that I've assigned to the compilers to compile a single file against 5 different compilers so I can test for compatibility easily.

I make no promises about it other than I find it useful for my own purposes - if you do too, great. Otherwise, don't use it...

Michael Burr