Have you encountered this error while building a C++ class in Microsoft Visual Studio? This is an issue that has been popping up in Microsoft Visual Studio since 2010, yet there are still many users to this day that encounter this error. This article will expand on the causes of “CL.exe exit code 2” and outline a few simple troubleshooting methods to resolve the issue.
To understand the error code “CL.exe exit code 2” in its entirety it is important to split the error into two distinct sections: “CL.exe” and “exit code 2”. Cl.exe is a tool that controls the Microsoft C++ and C++ compilers and linker.
It can only be run on operating systems that support Microsoft Visual Studio for Windows. Exit code 2 is an error that occurs when CL.exe is unable to process as it should. The next section will detail the causes for this failure in process.
How to Fix CL.exe Exited with Code 2
There is a troubleshooting method for each of the issues detailed in the preceding section. However, before you start with any of these troubleshooting methods, ensure that you have nailed down the cause of this error as going through each of these troubleshooting methods will be unnecessarily time-consuming for someone who simply deleted a header or source file on accident.
There is a way to check the cause of the error in Microsoft Visual Tools that will be outlined below in the first step. With that out of the way, here are a few simple troubleshooting methods to resolve “CL.exe exit code 2”.
1. Check the Cause of CL.exe Exited with Code 2
Before you start with any of the troubleshooting methods listed below, you must first assign the cause of your issue. You can check this in Microsoft Visual Studio by changing the build verbosity to detailed. To change the build verbosity to detailed, follow these three steps.
- Open ‘Tools’ and navigate to ‘Options’.
- Go to ‘Projects and Solutions’ and select ‘Build and Run’ in the corresponding menu.
- Select ‘Detailed’ in the drop-down menu corresponding to ‘MSBuild project build output verbosity’.
After completing these steps, simply run the build again and open the output tab. There you can find the error message in a more detailed form and address it accordingly.
2. Ensure the C++ Build Tools for Visual Studio are Installed
If you have assigned the cause of your issue to a failure to install the Microsoft C Compiler on your computer, and in your PATH, then you must install the C++ build tools for Microsoft Visual Studio and use a special command prompt to set your PATH automatically to locate CL.exe. To do this, follow these four steps.
- Step 1: Download Build Tools separately from the Visual Studio downloads page, then choose C++ build tools from the installer.
*If you already have Visual Studio, you can install Desktop development with C++ from
Visual Studio Installer which you should have in the Start Menu*
- Step 2: Open the special command prompt that corresponds to your version of Python. For 32-bit Python, this would be x86 Native Tools Command Prompt. For others it might be x86 x64 Cross Build Tools Command Prompt.
Step 3: Type in the command: ‘pip install package_name’.
Try compiling your code again to see if the error has been resolved.
2.Set All Directories in VC++ Directories as Empty
If you have assigned the cause of your issue to an issue with multiple paths within your PATH environmental variable, and you have already attempted the troubleshooting method listed above, then you might be able to resolve the issue by setting all directories in VC++ Directories to empty and selecting ‘Inherit from parent or project defaults’. To navigate to VC++ Directories, follow these three steps.
- Step 1: On the main menu, choose ‘View’ and then ‘Solution Explorer’.
- Step 2: Right-click on a project node (not the top-level solution) and choose ‘Properties’.
- Step 3: In the left-hand panel of the ‘Property Pages’ dialog box, select ‘Configuration Properties’ and then ‘VC++ Directories’.
From here, simply set all of the directories listed to empty and select ‘Inherit from parent or project defaults’.
3.Check Your Header and Source Files
If you have assigned the cause of your issue to an issue with your compiler locating the source file, then you have probably made a mistake in typing its name. Check your source file to ensure that you have typed it out correctly.
If you have accidentally deleted your header or source file, Microsoft Virtual Studio will not recognize the absence until you reload your project. Thus, it is worth reloading your project to ensure you are not missing a header or source file before moving on to other troubleshooting methods.
Causes of CL.exe Exited with Code 2
CL.exe is a Microsoft C Compiler that must be installed on your computer and in your PATH. PATH is an environment variable that tells Windows where to find executable files. Exit Code 2 occurs when CL.exe is not properly installed on your computer or when its PATH is disrupted in some way.
Therefore, the two most common causes for “CL.exe exit code 2” are CL.exe or its supporting tools not being installed properly on your computer or the PATH environment variable being disrupted.
These issues could occur due to a problem with the CL.exe source file, there being multiple paths within the PATH environment variable, an error in the code such as an incorrectly initialized for-loop, or even simply a failure to install the Microsoft C Compiler on your computer. Troubleshooting methods to all of these issues will be detailed in the next section.
In Conclusion
CL.exe exited with code 2 is a fairly common issue encountered in Microsoft Visual Studios. Thus, there are many listed solutions available for this problem on the internet. However, if you follow the three troubleshooting methods listed above, you should be able to resolve this issue in no time.