4.7.3. Using Visual Studio's compiler (cl.exe)

If you want to run 'cl.exe' from NppExec, it requires some additional environment. Such environment can be set using a batch file or using NppExec's command ENV_SET [4.2].

1. Using a batch file

To use a batch file, create a file 'cl.cmd' with similar text:

REM cl.cmd
REM Visual Studio 8.0 (2005) Express
@echo off
Set VCDIR=C:\Program Files\Microsoft Visual Studio 8\VC
Set VSCOMMON=C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
Set MSSDK=C:\Program Files\Microsoft Platform SDK for Windows Server 2003
Set PATH=%VCDIR%\bin;%MSSDK%\bin;%VSCOMMON%;%PATH%
Set INCLUDE=%MSSDK%\include;%VCDIR%\include;%INCLUDE%
Set LIB=%MSSDK%\lib;%VCDIR%\lib;%LIB%
cl.exe %*

Now, use something similar to

cl.cmd "$(FULL_CURRENT_PATH)"

to compile your source file(s).

As you can see, the VC's environment is handled inside the 'cl.cmd' in this case. I.e. it is handled by the system (well, by cmd.exe) and not by NppExec.

2. Using ENV_SET

The initialization of environment variables for VC will look similar to the following:

// setting NppExec's internal (user) variables
SET local VCDIR = C:\Program Files\Microsoft Visual Studio 8\VC
SET local VSCOMMON = C:\Program Files\Microsoft Visual Studio 8\Common7\IDE
SET local MSSDK = C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2

// saving previous values of environment variables (just in case)
SET local PATH_0 = $(SYS.PATH)
SET local INCLUDE_0 = $(SYS.INCLUDE)
SET local 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)

// executing the child process with specified environment variables 
rc /r /Fo"$(CURRENT_DIRECTORY)\Resources.res" "$(CURRENT_DIRECTORY)\Resources.rc"
cl.exe "$(FULL_CURRENT_PATH)"

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

Don't forget that ENV_SET does not understand such form of existing environment variables as "%PATH%" because NppExec does not accept such declaration. "$(SYS.PATH)" should be used instead of "%PATH%".

In general, it's not a good idea to set and then restore the environment every time it is needed by some tool. Instead, you can set all the required environment in NppExec's start-up script [4.2]. Such approach would allow you to use any of your tools without further specifying of full paths or additional environment variables.

3. Using ENV_SET, VS 10 + clang

[The text below was originally posted by Suman Kar in NppExec's forum.]

I have spent a good couple of hours getting this to work. Finally, I was able to coax NppExec, clang and Visual Studio to work together and let me compile my C snippets. The following script (along with "compile_or_run" described in the documentation [4.7.4]) worked for me:

// run@.cpp.txt

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

// saving previous values of environment variables (just in case)
SET local PATH_0 = $(SYS.PATH)
SET local INCLUDE_0 = $(SYS.INCLUDE)
SET local 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 local clangc = D:\llvm_workspace\llvm\build\bin\Debug\clang.exe
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

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

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

Things to note: I am using Visual Studio 2010 (installed in C:\Program Files\) on a 32-bit Windows 7 box with NppExec 0.4.2.1 and my clang 3.0 binaries reside at D:\llvm_workspace\llvm\build\bin\Debug\ (built with Visual Studio 2010). You will need to change the appropriate paths in the script of course depending on your installation.