Where

When

September 2017
M T W T F S S
« Jun    
 123
45678910
11121314151617
18192021222324
252627282930  

jb_logo_6_small

logo

Creating a Visual Studio aware console window

What good is findstr without a command prompt, and what good is a command prompt that is unaware of your development environment?

When you install Visual Studio 20xx, it adds a menu item for a “devenv” (my term, not Microsoft’s) command window. That’s great, but not terribly convenient. Terribly convenient but not particularly great is the “Open command window here” context menu item in the Windows Explorer. What we really need is a devenv window that we can launch from the Explorer context menu, like this:

screenshot

You’ll need two files; A registry patch, and a batch file to massage the command window. The download link below contains the files for Visual Studio versions 2008-2013.

  • Download and expand the archive.
  • Right click on the appropriate reg file and select “merge”.
  • Place the corresponding batch file somewhere in your default system path — windowssystem32 for example.
Now when you select “Open VS20xx env window here” from the Explorer context menu, the window that opens will contain the VS development environment. Good stuff.

File contents are listed below. The Visual Studio 2010 files are shown; all others are identical except for VS version number.

 vs2010.bat
@echo off
call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VCvcvarsall.bat" amd64
pushd %1
vs2010_shell.reg


Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOTDirectoryshellvs2010_shell] @="Open VS2010 env window here"
"NoWorkingDirectory"=""
[HKEY_CLASSES_ROOTDirectoryshellvs2010_shellcommand] @="cmd.exe /s /k vs2010.bat "%V""

Download the archive via the link below.

Continue Reading

Setting the Visual Studio Debugger path using CMake

Debugging our just-built executables was a real source of frustration early on in our Windows programming experience. Getting all the correct libraries into the path was tedious at best, and more often than not a nightmare of hellish proportion. To be certain, copying files around and hacking environment variables to this end is just asking for it. It’s not a matter of if a critical mistake will be made, it’s a matter of when; and that’s just the command line version of events. Needless to say, we weren’t happy.

When it came time to run the code in the Windows Debugger, things were almost just as bad. Every time we wanted to debug an executable, we had to make modifications to a generated Visual Studio project file. The procedure was time consuming, tedious, and error prone. We needed a solution that would ensure the correct behavior every time, and without any human intervention. The problem was that CMake did not, and to the best of my knowledge does not currently have, any mechanism for modifying the Visual Studio Debugger path in a generated project. Still, we needed to get the job done with CMake. Maybe it couldn’t set the debugger path, but it could write out a configured file. Enter the Visual Studio .user file.

The Solution

Before we begin, I am aware that Microsoft suggests avoiding the use of .user files. Their caution stems from source code control issues however, and the files we are about to generate are intentionally volatile. We aren’t going to commit anything in the build directory to our source code repository, though the input file that follows should be placed somewhere in your source tree and committed.

Step 1:
Create a CMake input file called “project.vcxproj.user.in” with the following content:

<?xml version="1.0" encoding="utf-8"?>	 	
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">	 	
 <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">	 	
 <LocalDebuggerEnvironment>PATH=@CMAKE_BINARY_DIR@\bin\Debug;%PATH%</LocalDebuggerEnvironment>	 	
 <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
 </PropertyGroup>	 	
 </Project>

The value for PATH in the LocalDebuggerEnvironment tag is of course up to you; the above is merely a simple example using “Debug” as the Configuration Type.

Step 2:
Configure project.vcxproj.user.in for each executable in your solution/project; something like this:

foreach(src ${EXEC_SRCS}) 
	add_executable(${src} ${src}.cc) 
	configure_file(project.vcxproj.user.in ${CMAKE_BINARY_DIR}/${src}.vcxproj.user @ONLY) 
endforeach()

Now when you start a debugging session, the appropriate paths will be set and things should work as expected. There are likely other and perhaps better ways to accomplish this goal, with the ideal solution being to set the debugger path with some CMake property. In the meantime, we make do with the tools avalable to us.

Continue Reading

Overriding the default CMake build types

Let’s imagine that our C++ source code utilizes Design By Contract methodology. The default build types provided by CMake are too general to work for us, and we want to be able to toggle use of contracts during the compile. We need to create new build types that suit our needs, and we need them to work on Linux and Windows. And so we’d do something like this in CMakeLists.txt:

# Set the Configuration types ...
if(WIN32)
    set(CMAKE_CONFIGURATION_TYPES Debug_contracts Debug_no_contracts Release_contracts Release_no_contracts 
         RelWithDebInfo_contracts RelWithDebInfo_no_contracts CACHE STRING "Supported configuration types"
        FORCE)
else()
    set(CMAKE_CONFIGURATION_TYPES Debug_contracts Debug_no_contracts Release_contracts Release_no_contracts 
         CACHE STRING "Supported configuration types"
        FORCE)
endif()
# And then make the Project function call.
project(MyProject)

And then set values for your newly defined build types:

if(WIN64MSVC OR WIN64INTEL)
# Just an example for Debug_contracts; repeat with needed flags for other types.
# Contracts on is the default state. We'd add /DNDEBUG in a "no_contracts" config. 
   set(CMAKE_CXX_FLAGS_DEBUG_CONTRACTS
        "${MY_CXX_FLAGS} /Zi /D"_ITERATOR_DEBUG_LEVEL=2" /MDd /LDd /Od " 
        CACHE STRING "Compiler flags for Debug_contracts builds" )       
   set(CMAKE_SHARED_LINKER_FLAGS_DEBUG_CONTRACTS "${MY_SHARED_LINKER_FLAGS} /DEBUG /NODEFAULTLIB:MSVCRT" 
        CACHE STRING "Linker flags for shared libraries; Debug_contracts" )
   set(CMAKE_EXE_LINKER_FLAGS_DEBUG_CONTRACTS "${MY_EXE_LINKER_FLAGS} /DEBUG" 
        CACHE STRING "Linker flags for executables; Debug_contracts")            
else() # Linux
   set(CMAKE_CXX_FLAGS_DEBUG_CONTRACTS "${MY_CXX_FLAGS} -g " 
        CACHE STRING "Compiler flags for Debug_contracts builds") 
   set(CMAKE_EXE_LINKER_FLAGS_DEBUG_CONTRACTS ${CMAKE_EXE_LINKER_FLAGS} 
       CACHE STRING "Linker flags for executables; Debug_contracts") 
endif()

Note that setting CMAKE_CONFIGURATION_TYPES prior to the project function call will not eliminate the default configuration types (Debug, Release, MinSizeRel, RelWithDebInfo, et al.), from the cache, but will prevent them from appearing in the VS configuration list.

Continue Reading