<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.7.3. Using Visual Studio's compiler (cl.exe)</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>

<BODY>
<h3>4.7.3. Using Visual Studio's compiler (cl.exe)</h3>
<p>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 <a href="4.2.html">[4.2]</a>.</p>
<p><b>1. Using a batch file (simple)</b></p>
<p>To use a simple batch file, create a file 'cl.cmd' with similar text: </p>

<blockquote>
<pre>
REM cl.cmd
REM Visual Studio 2017 Community Edition
@echo off
call &quot;%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat&quot; x86
cl.exe %*
</pre>
</blockquote>
<p>Now, use NppExec's script similar to</p>
<blockquote>
<pre>NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
cl.cmd &quot;$(FULL_CURRENT_PATH)&quot;
</pre>
</blockquote>
<p>to compile your source file(s). </p>
<p><b>2. Using a batch file (advanced)</b></p>
<p>To use an advanced batch file, create a file 'cl.cmd' with similar text: </p>

<blockquote>
<pre>
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 %*
</pre>
</blockquote>
<p>Now, use NppExec's script similar to</p>
<blockquote>
<pre>NPE_CONSOLE local -- x+ // enable the built-in error highlight filter locally
cl.cmd &quot;$(FULL_CURRENT_PATH)&quot;
</pre>
</blockquote>
<p>to compile your source file(s). </p>
<p>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.</p>
<p><b>3. Using a running (interactive) VS Developer Command Prompt</b></p>
<p>The following NppExec's script can be used to run the Developer Command Prompt for VS 2022 in NppExec's Console:</p>
<blockquote>
<pre>set local @exit_cmd = exit  // sends &quot;exit&quot; when NppExec's Console is closed
cmd /K &quot;C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat&quot; x86</pre>
</blockquote>
<p>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:</p>
<blockquote>
<pre>cd /D &quot;$(CURRENT_DIRECTORY)&quot; &amp;&amp; cl &quot;$(FILE_NAME)&quot;</pre>
</blockquote>
<p><b>4. Using ENV_SET</b></p>
<p>The initialization of environment variables for VC will look similar to the following:</p>
<blockquote>
<pre>// 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 &quot;local&quot;, 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&quot;$(CURRENT_DIRECTORY)\Resources.res&quot; &quot;$(CURRENT_DIRECTORY)\Resources.rc&quot;
cl.exe &quot;$(FULL_CURRENT_PATH)&quot;
</pre>
</blockquote>
<p>Don't forget that ENV_SET does not understand
such form of existing environment variables as &quot;%PATH%&quot; because NppExec does not accept such declaration. &quot;$(SYS.PATH)&quot; should be used instead of &quot;%PATH%&quot;.</p>
<p>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 <a href="4.2.html">[4.2]</a>. Such approach would allow you to use any of your tools without further specifying of full paths or additional environment variables.</p>
<p><b>5. Using ENV_SET, VS 10 + clang</b></p>
<p><em>[The text below was originally posted by Suman Kar in NppExec's forum.]</em></p>
<p>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 &quot;compile_or_run&quot; described in the documentation <a href="4.7.4.html">[4.7.4]</a>) worked for me:</p>
<blockquote>
<pre>// 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 &quot;local&quot;, 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
&quot;$(clangc)&quot; &quot;$(FULL_CURRENT_PATH)&quot; -o &quot;$(obj).exe&quot;
cmd /c &quot;$(obj).exe&quot;
</pre>
</blockquote>
<p>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.</p>
<P>&nbsp;</P>
</BODY>
</HTML>
