Sublime Forum

How to setup Sublime text 4 with MSVC compiler?

#1

Here is my build system:-

{
    "target": "terminus_exec",
    "cancel": "terminus_cancel_build",
    
    "shell_cmd": "\"D:\\IDE\\Visual Studio\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat\" && cl \"${file}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c++",

    // "variants":
    // [
    //     {
    //         "name": "Run",
    //         "shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
    //     }
    // ]
}

And when I use this build system, it doesn’t make any executable or object file.

Btw, I downloaded the latest version of Microsft Visual Studio to get the latest MSVC compiler. And I commented out some text here because I get a build system for g++ from @OdatNurd and then I just changed shell_cmd value as you can see. So, can someone tell me what is wrong in my build system? :upside_down_face::upside_down_face::upside_down_face:

1 Like

#2

I don’t use windows compilers myself, but I suspect that the reason you don’t get an executable out of this might be related to not telling cl that it should, which probably makes it just compile the source file to an object file instead.

You might want to investigate this plugin (see this video if you don’t know how to set up a plugin), which sets up the ability to run the appropriate script once at Sublime startup so that your build doesn’t need to do it every time, since Windows is slow enough as it is spawning processes.

The plugin has a comment near the top with a link to a Stack Overflow question; my answer there (which is where the plugin comes from) has an example of using it as well. For your case it would be a simple matter of just changing the shell_cmd to something like what is specified there and everything else remains the same.

0 Likes

#3

How to install this plugin? Btw I don’t have git because I’m beginner.

0 Likes

#4

The video I linked to above shows you how to do it. You don’t need to use git, you can copy/paste it into the appropriate file.

0 Likes

#5

In my case the bat file is not the file that you said in that question in stackoverflow, but you said that I should locate the bat file of mine in this “vc_vars_cmd” settings. But what should I put in “vc_vars_arch” settings? My operating system is 32 bit and my processor is intel core i3-3220"

0 Likes

#6

I believe for a 32-bit Windows host OS and 32-bit Windows target architecture, “vc_vars_arch” should be set to “x86” assuming that is the same as the command-line parameter ultimately passed to “vcvarsall.bat”.

I have never actually attempted to use a Windows compiler from ST. I typically just use ST for editing tasks.

0 Likes

#7

On my system, the full path to “vcvarsall.bat” is as follows:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat

However, on a 32-bit Windows OS, the path would most likely be:

C:\Program Files\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat

0 Likes

#8

Perhaps a roundabout way of doing things but I start a shell with this shortcut

# The shortcut target
# This is a shortcut I leave on the desktop for easy access
%comspec% /k C:\<code path>\shell.bat

This is what it targets (note that your pathing will differ. I also subst my source dir so it’s always the same. Note that this is for a 64 bit target. If you have a different target select appropriate vcsvars script to run. This what Microsoft wants you to do in order to turn your shell into a dev shell.

# file: misc/shell.bat
@echo off

set VSDEVCMD="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"

if EXIST %VSDEVCMD% call %VSDEVCMD%

set path="<PATH TO MISC FOLDER>";C:\Program Files\Sublime Text;%path%

Now the trick is to start sublime from this shell so it can get all the env vars that vcvars sets up
so from the shell I have a subl script under that misc/ folder I added to path above

# File: misc/subl.bat
# another shortcut file to start sublime with my project preloaded
@echo off

call "C:\Program Files\Sublime Text 3\subl.exe" "--project <your project file>.sublime-project"

Now to get the project building it looks like this:

# file: misc/<your project file>.sublime-project
{
	"folders":
	[
		{
			"path": "..",
			"folder_exclude_patterns": ["build", ".vs"],
			"follow_symlinks": true
		}
	],
	"build_systems":
	[
		{
			"selector": "source.c",
			"name": "Project Build",
			"working_dir": "$folder/code",
			"osx":
			{
				"cmd": ["./build.sh"],
				"file_regex": "(^.*)[:](\\d+)[:](\\d+)[:].+$",
			},
			"windows":
			{
				"cmd": ["build.bat"],
				"file_regex": "^(.*)\\((\\d+),?(\\d+)?\\)\\s?:\\s([^\n]+)",
			}
		}
	]
}

# File: code/build.bat
# Here just do your build. I keep this file with the source code unde code/
cl <params>

Now I can build with cl from sublime just fine.


Of course you can also skip all the boilerplate stuff and just call vcvars64.bat in a shell of your choosing and open sublime from that shell using the build in shortcut subl if you have all path’s set up correctly from the start.

0 Likes

#10

Do I need to put the build.bat in misc folder?

0 Likes

#11

Apologies, I should have mentioned that the folder structure is of course up what you use in your project, I just happen to currently use this one:

code/build.bat
code/<all my code, i.e. cpp, h files etc>
misc/shell.bat
misc/subl.bat
misc/<project>.sublime-project

so build.bat as I use it is in the same location as the source code, but you can change it to be relative to whatever you wish, or even put your call to cl.exe there instead, by modifying the cmd argument under windows in sublime-project

0 Likes

#12

Can I put build.bat, subl.bat, shell.bat, all of my code and .sublime-project in same folder instead of two directories?

0 Likes

#13

Absolutely, you can put them anywhere you want. Just update the relative locations so they can find each other.

To sum up all you need to do is

  1. Open a dev-console where you have Microsofts C++ toolkit available (see
    https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line)
  2. Open sublime from this console (sublime comes with a shortcut to do so in the install location called subl.exe
  3. Make a build setting that calls cl.exe or any of the other tools (I did this above inside a .sublime-project file

The reason for this is two-fold: a) Microsoft didn’t put the c++ tools on the system path by default and b) microsoft decided that hiding the configuration to do so behind a semi-hidden script (vcvarsall.bat), that keeps moving around between visual studio installations, was a great idea :thinking:

0 Likes

#14

Is there anyway to learn commands that windows use for batch/.bat files?

0 Likes