Permanent alias on Windows

If you develop C++ programs on Windows, you might have felt the need to invoke the compiler from the command prompt itself. Its pretty easy to do so; just append the path of the compiler to the PATH environment variable.
However, if you decide to invoke it with some compiler/linker options, you’ll need to do some extra work.

Unlike Linux, it is a bit tricky to create permanent aliases on Windows as it requires editing the registry.

Before discussing on how to do it on Windows, let me first illustrate how it is done on Linux (Ubuntu specifically):
Suppose you want to set c+ as an alias to c++ -Wall -Wextra, simply create a file in your home folder named .bash_aliases and write the following in it:
alias c+ = 'c++ -Wall -Wextra'
Instead of writing the compiler options -Wall -Wextra directly, I prefer writing them in a separate file and then include them by specifying its path via the @file option of the GCC compiler, so that I don’t have to write them again in my IDE’s settings.

So, .bash_aliases now contains the following:
alias c+ = 'c++ @/path/to/cpp-options.txt'
where cpp-options.txt contains:
-Wall -Wextra

Now you can invoke the compiler by typing the following in a cmd shell:

c+ /path/to/source.cpp

In Windows, the closest thing to alias is doskey.

Just like in the Linux case, we have to somehow instruct cmd to launch the doskey command every time it starts.
To do this, we need to create 2 files:

  • cmd_aliases.txt, which will contain all the aliases:

    Note: The $* option is to allow the use of other command-line options along with c+ command. Without it, you won’t be able to specify the source-code and the output executable path when invoking the compiler with c+.

  • cmd_autorun.cmd, which loads all those aliases from the above file everytime cmd is invoked:

[I am assuming these 2 files are kept in your My Documents folder, although you can keep them in any other folder you like.]

Now we need to specify the location of this script in the registry so that it launches automatically along with cmd.
To do this, execute the following after pasting it to a text file and saving it with .reg extension:

For completeness, here are all the files which I use myself:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s