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 (simple)

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

REM cl.cmd
REM Visual Studio 2017 Community Edition
@echo off
call "%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86
cl.exe %*

Now, use NppExec's script similar to

NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
cl.cmd "$(FULL_CURRENT_PATH)"

to compile your source file(s).

2. Using a batch file (advanced)

To use an advanced 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 NppExec's script similar to

NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
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.

3. Using a running (interactive) VS Developer Command Prompt

The following NppExec's script can be used to run the Developer Command Prompt for VS 2022 in NppExec's Console:

set local @exit_cmd = exit  // sends "exit" when NppExec's Console is closed
cmd /K "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" x86

Now, while the VS Developer Command Prompt is running in NppExec's Console, it is possible to e.g. compile the current file via the following command in NppExec's Console:

cd /D "$(CURRENT_DIRECTORY)" && cl "$(FILE_NAME)"

4. 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

// setting NppExec's child process'es environment variables
// (due to "local", the previous values of the environment
//  variables will be restored when this script ends)
ENV_SET local PATH = $(VCDIR)\bin;$(MSSDK)\bin;$(VSCOMMON);$(SYS.PATH)
ENV_SET local INCLUDE = $(MSSDK)\include;$(VCDIR)\include;$(SYS.INCLUDE)
ENV_SET local LIB = $(MSSDK)\lib;$(VCDIR)\lib;$(SYS.LIB)

// enabling the built-in error highlight filter locally
NPE_CONSOLE local -- x+

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

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.

5. 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

// setting NppExec's child process'es environment variables
// (due to "local", the previous values of the environment
//  variables will be restored when this script ends)
ENV_SET local PATH = $(VCDIR)\bin;$(MSSDK)\bin;$(VSCOMMON);$(SYS.PATH)
ENV_SET local INCLUDE = $(MSSDK)\include;$(VCDIR)\include;$(SYS.INCLUDE)
ENV_SET local 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)

// enabling the built-in error highlight filter locally
NPE_CONSOLE local -- x+

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

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.