How to fix the ‘Common Language Runtime was unable to set the breakpoint’ error in Visual Studio projects
When you have multiple projects which link together, it can be handy to have them open in Visual Studio to debug through or work on them. This is especially true if you’re using libraries which contain common code across projects. That is the purpose of a DLL after all (or static library), to provide common functionality to a number of projects. When debugging, it can be nice to have the DLL code project open in the same Visual Studio solution in order to set break points and debug through the DLL to ensure it is providing the correct outputs to the calling code.
While this is very easy to set up (simply add an existing project in Visual Studio and link to the .vsproj or .vcxproj or similar), you might come across the error where you cannot set break points on the lines you would like due to the Common Language Runtime failing (see image below for an example of this error).
How to fix
Fixing this error can be tedious, especially as the pop-up does not provide much information beyond the fact the Common Language Runtime (CLR) was unable to set the break point. You might even look among your DLL code and properties to try to find the error, but let me stop you there. This error is fixed not in the DLL properties, but in the properties of the original project you added the DLL to.
The error is caused by optimisation within the compilers and CLR debuggers which can cause strange concepts like break points being unable to be set. Fixing this is as simple as unchecking a check box in the project properties (see below).
Project properties
Go to the project’s properties of the calling code and navigate to the Build tab. In the second section, General, you’ll find a check box labelled ‘Optimize code’. Uncheck this check box and recompile your project. When you next try to debug, you should find that you can now set the break points in the places you would like within your DLL code.
If you know of a different solution, then feel free to comment it. If not, hopefully this will help you fix your breakpoint problems.