Hello, World on C/C++ on Windows

On to Windows. And we will continue using command line only, no Studio yet.

The are two options in my opinion: Cygwin which is Linux stimulations and Build Tools for Visual Studio 2019 (scroll down to Tools for Visual Studio).

Microsoft stuff is pretty heavy – minimum 6Gb and up to 10Gb if you want install everything (which includes two SDKs, MFC and .NET), but this would allow you create any type of Windows application. For a minimum select Desktop development with C++.

Let’s start with Microsoft compiler. First thing you should remember is that you cannot just open Command Prompt and use it. You’ll get the error cl.exe is not found. To compile something you will have to open special Command Prompt – go to Start Menu -> Visual Studio 2019 -> Developer Command Prompt for VS 2019. Now try cl.exe, this time you should be able to run compiler.

Now open your favorite text editor and type your program. On Windows I would recommend Notepad++:

#include <stdio.h>

int main()
{
    printf("Hello, World from C\n");
    return 0;
}

Now type
cl.exe hello-world-c.c
Microsoft compiler is verbose (compare to gcc), so you should see that your application compiled.
Run it:
hello-world-c.exe
You should be greeted with “Hello, World from C”!
Success!

Now let’s try Cygwin. Download and install Cygwin appropriate for your system. Please note that after you install it and later noticed that some packages are missing (like gdb is not installed be default) there are no command line tool to install it, but you will have to run installer again and search and select required packages (although it’s easy).
Launch Cygwin Terminal and type gcc, press Enter and verify that compiler if installed (you should see “no input file” error).

To write a program you could use Vi/Vim right there in the Cygwin Terminal, or use something else (like Notepad++).
Again save your work as hello-world-c.c, then switch back to Cygwin Terminal (if you were not using Vim), and type
gcc -g -Wall -std=c99 hello-world-c.c -o hello-world-c.exe

if everything went fine gcc won’t provide you with any feedback, but new file should appear: hello-world-c.exe
Execute it:
hello-world-c.exe
You should be greeted with “Hello, World from C”!
Success!

If you curious about all command line arguments please see my post about Linux.

part II

Leave a Reply