Compiling using clang’s C compiler from Notepad++/NppExec on Windows


Disclaimer: This post is about compiling programs written in the C language and not C++ (which is a different language) from within Notepad++ on Windows. The clang C++ compiler does not yet support Microsoft’s C++ ABI and hence cannot be run with Visual Studio. For more details see the Overview section of LLVM Getting Started with the LLVM system using Microsoft Visual Studio (yes, the devil is in the detail and the detail requires some digging!) I am currently using clang 3.0. Hopefully, things’ll improve in 3.1!

The first thing you need, of course, is Notepad++. And NppExec — the wonderful plugin to run just about anything from within Notepad++. The second thing is, of course, to read NppExec’s documentation. You’d find the mojo in section 4.7.3. There are a couple of things to iron out, but then, these aren’t for you if you ain’t got no time.
Once you have the compile_or_run script ready as follows:

// compile_or_run
NPP_SAVE
SET Compiler = C:\Program Files\Notepad++\plugins\NppExec\scripts\run@$(EXT_PART).txt
NPP_EXEC "$(Compiler)"

For the lazy, here’s a brief listing of the NppExec script that you need to invoke with your compile_or_run script (as described in section 4.7.1 of NppExec’s manual) to get clang to work with Visual Studio 2010:

// run@.cpp.txt

// setting NppExec's internal (user) variables
SET VCBASE=C:\Program Files\Microsoft Visual Studio 10.0
SET VCDIR = $(VCBASE)\VC
SET VSCOMMON = $(VCBASE)\Common7\IDE
SET MSSDK = C:\Program Files\Microsoft SDKs\Windows\v7.0A

// saving previous values of environment variables (just in case)
SET PATH_0 = $(SYS.PATH)
SET INCLUDE_0 = $(SYS.INCLUDE)
SET LIB_0 = $(SYS.LIB)

// setting NppExec's child process'es environment variables
ENV_SET PATH = $(VCDIR)\bin;$(MSSDK)\bin;$(VSCOMMON);$(SYS.PATH)
ENV_SET INCLUDE = $(MSSDK)\include;$(VCDIR)\include;$(SYS.INCLUDE)
ENV_SET LIB = $(MSSDK)\lib;$(VCDIR)\lib;$(SYS.LIB)

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
///! NOTE: change the following to the folder where your clang binary resides
SET clangc = D:\llvm_workspace\llvm\build\bin\Debug\clang.exe
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SET obj = $(CURRENT_DIRECTORY)\$(NAME_PART)

// run clang
"$(clangc)" "$(FULL_CURRENT_PATH)" -o "$(obj).exe"
cmd /c "$(obj).exe"
UNSET obj
UNSET clangc

// restoring previous values of environment variables
ENV_SET PATH = $(PATH_0)
ENV_SET INCLUDE = $(INCLUDE_0)
ENV_SET LIB = $(LIB_0)

UNSET VCBASE

Note: You will need to change the paths to the various folders of you are using say Visual Studio 2008 or have an installation at some other place than C:\Program Files\ as well as the path to where your clang binaries reside! I did an in-source build with Visual Studio 2010 and my clang binaries reside at D:\llvm_workspace\llvm\build\bin\Debug\

You need Visual Studio’s environment set up for you before you can do this. Otherwise, clang complains. Loudly. The long way is to wade through the different batch files such as vc32varsall.bat and whatnot from within your Visual Studio installation. Once you have the environment set up, the rest is easy, you simply define a couple of other commands to invoke your compiler and run the executable. The short way is to simply copy the code listing provided below.

If you haven’t busied yourself to see if the above works/wondering why the need for this inane post here’s the two essential tricks that I had to labour around:

  • The first is setting up the environment. NppExec has about three different sets of variables (and corresponding commands to set/reset these). The important part is to figure out where to use what. The VC environment requires changes to your system’s %PATH% enviroment variable. This can be done via the env_set command. And oh, before you forget, you will need to save the current value of the %PATH% variable before modifying it so you can restore it later.
  • The second is getting the output in the console provided by NppExec. Typically, if you followed the documentation alright, you’d be able to run your program. A window would come and go (for snippets — which is mostly what I write) and you’d be left staring blankly at the NppExec console without any hope of redemption. The trick is to run your executable as a standalone command via cmd.exe (with the /c switch so that it does not wait indefinitely for you to kill it). Once you’ve this setup, the console output is nicely output to your NppExec’s console window (which for me is at the bottom of Notepad++’s document area).
Advertisements

About this entry