Fastest way to write & compile a C/C++ program in Windows

55,063

Solution 1

I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question and executing it and seeing it's output.

For quick-and-dirty experimental coding, I really like codepad.org. Not having to create a file is especially nice as it saves me from coming up with a suitable name and disk location. Be aware that it uses g++ 4.1.2 behind the scenes so some of the latest C++11 features aren't supported.

Solution 2

Use TCC : Tiny C Compiler

  1. start a command prompt
  2. cd wherever
  3. notepad main.c
  4. write code in notepad. save
  5. back in the command prompt type tcc -run main.c
  6. notice errors, go back to 4

Note that with -run parameter you're invoking tcc like an interpreter

Solution 3

"really, really quick (and dirty, throw away?) programming "?

  • Compiler : VC++ command line - you already have it.
  • Editor: Notepad or somesuch
  • Compilation process: A .BAT file you write once and supply a parameter with the name of the single source file.
  • Location: Set up some desktop shortcuts to a known directory for your test code.

Solution 4

Which compiler you use doesn’t really matter. I prefer G++ but cl.exe (from Visual Studio) works equally well.

In order to use the compiler quickly from the command line, either

  1. include it into your PATH variable by setting it in the system settings, or
  2. create a simple .cmd script which launches a console with the right paths included.

Visual Studio incidentally comes bundled with such a .cmd script which is linked in the Start Menu entry of Visual Studio. Personally, though, I prefer adjusting the PATH variable.

Then you can simply invoke the compiler from any directory in the command line. If you are too lazy to write the whole command line, create a script to do it for you. Or use Cygwin and (C)Make.

Two additional remarks:

  1. Starting the project using the build configuration (Cntr+F5 (?)) leaves the console open after the program has run, without you having to include getch() calls or similar.

  2. I highly recommend you learn an editor such as Emacs or Vim, unless you plan never to use any other platform than Windows, and even then. These editors are just tremendously powerful, and in some ways light-years beyond what the Visual Studio code editor offers.

    But if you really don’t have the time, use a decent text editor such as Notepad++ instead.

Solution 5

On my machine, I have a "empty" project called "Test". When I want to test some random code on the internet, I simply put it into main.cpp in that project, and compile.

If you think MSVC takes too long to load, it should be possible to write a batch script that attempts to compile the project and puts the build log in a file. Then you can simply alter the existing main.cpp with notepad, double click the batch file, then pop open the build log or run the executable.

[Edit] I made a batch file to compile the entire solution. Turns out that requires loading visual studio. However, the batch file can compile/run a single cpp file easy enough.

Share:
55,063
Felix Dombek
Author by

Felix Dombek

Computational Linguistics B.Sc. from University of Potsdam, Germany. 7 years of experience developing Windows software for a backup/office software company (languages: classic and modern C++, VB6, Python, PHP) 4 years at TomTom, developing embedded routing software on Linux in modern C++.

Updated on May 07, 2020

Comments

  • Felix Dombek
    Felix Dombek about 4 years

    I'm usually using Visual Studio, but several things bother me when I just quickly want to test some code:

    • it has a rather long startup time
    • it always needs a project to execute/debug files
    • program output gets printed to the console, but the window simply closes when I don't insert a getchar() or a breakpoint in the program and thus I'm not seeing it.

    I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question, running it and seeing its output.

    I don't think that console programs or g++ under CygWin are a good solution, because there it takes ages to cd into the right dir to save the file, I'm not used to editors such as Vim, and typing in the compiler commandline myself has always annoyed me etc.

    So I guess what I'm looking for is a very lightweight free C/C++ IDE which is preconfigured to work with a free compiler (bonus points if it is even shipped with it.)

    What can you recommend which adresses at least two items from the list above?

    Is there maybe even a program which can execute/interpret C or C++ in an interactive commandline (like Python)?

  • Nerdtron
    Nerdtron over 12 years
    I see codeblocks hasn't had an update in almost 1.5 years -- is that even still being maintained? I tried looking around for some alternative windows IDEs awhile back but found the ones I saw weren't actively updated.
  • Felix Dombek
    Felix Dombek over 12 years
    Throw-away – yes. I'd still prefer a lightweight IDE over a separate editor and .BAT file. Thanks for your answer though.
  • TheBuzzSaw
    TheBuzzSaw over 12 years
    Actually, CB is updated constantly. Personally, I use the nightly builds. Yeah, the team is a bit slow at putting out "big official releases", but the IDE is heavily updated/maintained! Even so, the 10.05 release is plenty stable/adequate. Having a separately updated MinGW is more important than having a bleeding edge Code::Blocks.
  • Nerdtron
    Nerdtron over 12 years
    ok, I was just going off the main download page - last version there was 1.5 years old.
  • Felix Dombek
    Felix Dombek over 12 years
    Not the only useful answer, but I can only accept one, and codepad.org/ideone.com that Mooing Duck recommended struck me as most unexpected inventions.
  • Andrew Durward
    Andrew Durward over 12 years
    Credit (and apologies) to @Mooing Duck. I didn't even notice his comment when I posted this response. I'll have to give ideone a shot.
  • Mooing Duck
    Mooing Duck over 12 years
    It's also very common for answers to link to codepad/ideone to show proof of compilation.