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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersc+ = c++ @/path/to/cpp-options.txt $*
Note: The$*
option is to allow the use of other command-line options along withc+
command. Without it, you won’t be able to specify the source-code and the output executable path when invoking the compiler withc+
.cmd_autorun.cmd
, which loads all those aliases from the above file everytimecmd
is invoked:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters@echo off cls doskey /macrofile=%USERPROFILE%\Documents\cmd_aliases.txt REM The `/macrofile` parameter of the `doskey` command is used to load the aliases from an external file.
[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:
Windows Registry Editor Version 5.00 | |
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor] "Autorun"="%USERPROFILE%/Documents/cmd_autorun.cmd" |
For completeness, here are all the files which I use myself:
-Wall -Wextra -Wfatal-errors -Wunreachable-code -Wfloat-equal -Winline -Wshadow -Wredundant-decls -Wcast-qual -Wcast-align -Wlogical-op -pedantic | |
-std=c++14 |
python2 = C:\Dev\Python27\python.exe $* | |
python3 = C:\Dev\Python34\python.exe $* | |
c+ = c++ @D:\anmol\coding\c++\cpp-options.txt $* |
@echo off | |
cls | |
doskey /macrofile=%USERPROFILE%\Documents\cmd_aliases.txt |
Windows Registry Editor Version 5.00 | |
[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor] "Autorun"="%USERPROFILE%/Documents/cmd_autorun.cmd" |