Going The C++ Way With Microsoft C And C++ Compiler And Notepad++
Recently I decided that I need to work more with C++ than I do now (my primary focus being C#). I picked up a Visual C++ 2008 book (I wanted to start with Visual C++ since I already have some experience with .NET and I would really like to work with managed C++, as well as with unmanaged in one environment), started a couple of sample projects and got the basic ideas.
Now, sometimes I wanted to work on something real quick (like a small program or snippet), so loading Visual Studio for the sake of editing a single source file was a bit out of line (although performance is not the issue – it still loads really fast), so I decided to go another way – to set up a simple text editor (Notepad++ in my case) to be able to compile C++ programs by using the Microsoft C++ compiler. If you are interested in doing the same, then read the next part of this post. Please note that I am not going to do an in-depth description of all possible compiler parameters, but will much rather show you how to setup a ready-to-work compilation environment. Once you get this done, it will be fairly easy to customize the compilation options on your own.
First of all, you need to download and install Notepad++ (well, there are many other plain text editors out there that can be customized to do the same thing, but I am using Notepad++). It is free, by the way.
Obviously, you will need the C++ compiler from Microsoft, this being the most important part (you can compile C++ programs from command line, even without a text editor, but without a compiler you are pretty much stuck). For this article, I am going to use CL, the Microsoft C/C++ compiler, version 15.00.30729.01 for 80×86. If you have Visual Studio 2008 installed, then you already have it on your PC. If not, then you will have to download the .NET Framework SDK (e.g., version 2.0 available at Microsoft) or the Windows SDK (which I personally recommend if you going to work without Visual Studio).
First of all, you need to know the path to the compiler (cl.exe). In my case (Windows 7/Visual Studio 2008) it is C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin.
Now that you know the path to the compiler, you should also know that once you start working with the compiler directly from Notepad++, you need to set it up to run with a specific set of parameters, some of them including local (specific to Notepad++) environment variables. Here are those you should be aware of at the moment:
- FULL_CURRENT_PATH – The full path to the file you are currently working with.
- CURRENT_DIRECTORY – Represents the directory path, where the working file is stored.
- FILE_NAME – The file name (pretty self-explanatory).
- NAME_PART – Represents the file name without the extension.
- EXT_PART – The file extension.
Here is a sample C++ program you can try typing in (or just copy it directly into the editor) for testing purposes:
#include <iostream>
int main()
{
std::cout << "Sample!" << std::endl;
return 0;
}
I saved it as sample.cpp. You can use a different name, the main idea is to get the file saved, so that there would be a default path set.
Now let’s work with setting things up for the compiler. If you’ve ever ran the Visual Studio 2005/2008 Command Prompt, you probably know that you can just type cl for the C++ compiler and pass the needed file as the source. The executable will appear in the same folder with the source file, once compiled. However, if you try running cl.exe via a regular Command Prompt session, you will encounter several issues. First of all, you will see that there will be a problem referencing mspdb80.dll, then there will be a problem with directives. To avoid this, you need to edit the PATH environment variable for your system. Also, you need to create the INCLUDE, LIB and LIBPATH environment variables. To do this in Windows 7, press WinKey + PauseBreak (to open the System window) and select Advanced system settings on the left panel. In the Advanced tab, click on the Environment Variables… button. You need to edit the Path variable in the System variables box.
Image lost since old blog
In my case, for PATH I used the following:
c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN;c:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools; c:\Windows\Microsoft.NET\Framework\v3.5;c:\Windows\Microsoft.NET\Framework\v 2.0.50727; c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\VCPackages;C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;c:\Program Files (x86)\Microsoft SQL Server\90\Tools\binn\;C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE;C:\Program Files\Common Files\Microsoft Shared\Windows Live
For INCLUDE I used the following:
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\INCLUDE;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\INCLUDE;C:\Program Files\Microsoft SDKs\Windows\v6.0A\include;
For LIB I used the following:
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\LIB;C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib;
And for LIBPATH I used the following:
c:\Windows\Microsoft.NET\Framework\v3.5;c:\Windows\Microsoft.NET\Framework\v 2.0.50727;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\LIB;c:\Windows\Microsoft.NET\Framework\v3.5;c:\Windows\Microsoft.NET\ Framework\v2.0.50727;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\ATLMFC\LIB;c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\LIB;
Obviously, the paths may be different depending on the installation path and parameters.
To make this less confusing (when you need to know precisely what the contents of the above mentioned environment variables should be like), do the following. Open the Command Prompt (WinKey + R and then cmd). Now, open the Visual Studio installation folder and navigate to the VC folder (in my case it was C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC). Drag vcvarsall.bat from that folder in the console window and execute it. Once it sets the environment variables, type in this command:
set path > D:\Docs\path.txt
set include > D:\Docs\include.txt
set lib > D:\Docs\lib.txt
You can modify the file path to a more suitable one (in fact, you can use whatever path you like – these files are for reference purposes only). The newly created files will contain the contents of the PATH, INCLUDE, LIB and LIBPATH variables. Copy the contents from the files, replace the existing contents of the PATH variable and create the INCLUDE, LIB and LIBPATH variables (don’t forget to include the contents for those accordingly). Once you click OK, the command line environment will be ready to compile C++ applications with proper references.
No, go back to Notepad++. If the source file mentioned above is not open, then go ahead and open it. Now, click Run > Run (menu). Type in:
cl "$(FULL_CURRENT_PATH)" /Fo"$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
As you see, there is the /Fo parameter that sets where the compiled file will be placed. If you do not set that, the file will be compiled in the system folder by default. It is possible to add compilation parameters, but at this point I am sticking to this plain and simple write-and-compile method.
Once you click Run and you see that the file successfully compiled, you can save the command for later on and even assign a shortcut to it.
Note that this method is used to compile native C++ applications. To compile CLR (.NET) applications, you need to add the /clr parameter.