Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking cl.exe (MSVC compiler) in Cygwin shell

I'm heavily using Cygwin (with PuTTY shell). But, it's quite tricky to invoke cl.exe (that is, the Visual C++ compiler toolchain) in the Cygwin Bash shell. Running vcvars*.bat in the Bash shell doesn't work obviously. I tried to migrate VC++'s environment variables to Cygwin, but it's not that easy.

How do I run the VC++ compiler in Cygwin's Bash shell?

like image 483
user46185 Avatar asked Dec 14 '08 20:12

user46185


People also ask

What is CL in MSVC?

cl.exe is a tool that controls the Microsoft C++ (MSVC) C and C++ compilers and linker. cl.exe can be run only on operating systems that support Microsoft Visual Studio for Windows. You can start this tool only from a Visual Studio developer command prompt.

Where is MSVC compiler installed?

More precisely, the default path where you'll find the compiler is C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin .

How do I install Microsoft Visual C++ MSVC compiler toolset?

Install the Microsoft Visual C++ (MSVC) compiler toolset. If you have a recent version of Visual Studio, open the Visual Studio Installer from the Windows Start menu and verify that the C++ workload is checked. If it's not installed, then check the box and select the Modify button in the installer.

Where is Cl exe stored?

cl.exe is usually located at %VCINSTALLDIR%\bin\ . VCINSTALLDIR environment variable is not set by default, but it is being set when you open Visual Studio's Native Tools Command Prompt.


3 Answers

I usually solve this by adding

call "%VS80COMNTOOLS%vsvars32.bat" >NUL: 

to c:/cygwin/cygwin.bat. Note that the VS80COMNTOOLS variable is extremely useful, since it gives you a foolproof (hm) way of locating vsvars32.bat.

Another approach is this, which allows you to easily switch between different Studio versions:

function run_with_bat() {     batfile=$1; shift     tmpfile="$TMP/tmp$$.bat"     echo "@echo off" > $tmpfile     echo "call \"%$batfile%vsvars32.bat\" >NUL:" >> $tmpfile     echo "bash -c \"%*\"" >> $tmpfile     cmd /c `cygpath -m "$tmpfile"` "$@"     status=$?     rm -f $tmpfile     return $status }  function run_vs9() {     run_with_bat VS90COMNTOOLS "$@" }  function run_vs8() {     run_with_bat VS80COMNTOOLS "$@" } 

You can now do:

$ run_vs8 cl Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86 Copyright (C) Microsoft Corporation.  All rights reserved.   usage: cl [ option... ] filename... [ /link linkoption... ] $ run_vs9 cl Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation.  All rights reserved.  usage: cl [ option... ] filename... [ /link linkoption... ] 

Note the compiler version.

like image 181
JesperE Avatar answered Sep 28 '22 16:09

JesperE


I understand that your problem is converting vcvars32.bat into a shell script.

One way around the problem is based on the idea that environment variables are inherited when one process launches another. So you can simply run vcvars32 under cmd and then run bash. This works fine on my machine:

sh-3.2$ cl Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86 Copyright (C) Microsoft Corporation.  All rights reserved.  usage: cl [ option... ] filename... [ /link linkoption... ] 

Alternatively, run set before and after invoking vcvars32 under cmd, and then create a shell script to set the environment variables.

like image 39
Diomidis Spinellis Avatar answered Sep 28 '22 16:09

Diomidis Spinellis


Following suggestions in the other answers, I did a diff of my bash "set" results with and without the MSVC variables, and came up with the following script to reproduce them for my installation of Microsoft Visual C++ 2010 Express. I've refactored it a bit to put all the installation-dependent pieces at the top, so hopefully this can be of use to others.

# These lines will be installation-dependent.
export VSINSTALLDIR='C:\Program Files\Microsoft Visual Studio 10.0\'
export WindowsSdkDir='C:\Program Files\Microsoft SDKs\Windows\v7.0A\'
export FrameworkDir='C:\WINDOWS\Microsoft.NET\Framework\'
export FrameworkVersion=v4.0.30319
export Framework35Version=v3.5

# The following should be largely installation-independent.
export VCINSTALLDIR="$VSINSTALLDIR"'VC\'
export DevEnvDir="$VSINSTALLDIR"'Common7\IDE\'

export FrameworkDIR32="$FrameworkDir"
export FrameworkVersion32="$FrameworkVersion"

export INCLUDE="${VCINSTALLDIR}INCLUDE;${WindowsSdkDir}include;"
export LIB="${VCINSTALLDIR}LIB;${WindowsSdkDir}lib;"
export LIBPATH="${FrameworkDir}${FrameworkVersion};"
export LIBPATH="${LIBPATH}${FrameworkDir}${Framework35Version};"
export LIBPATH="${LIBPATH}${VCINSTALLDIR}LIB;"

c_VSINSTALLDIR=`cygpath -ua "$VSINSTALLDIR\\\\"`
c_WindowsSdkDir=`cygpath -ua "$WindowsSdkDir\\\\"`
c_FrameworkDir=`cygpath -ua "$FrameworkDir\\\\"`

export PATH="${c_WindowsSdkDir}bin:$PATH"
export PATH="${c_WindowsSdkDir}bin/NETFX 4.0 Tools:$PATH"
export PATH="${c_VSINSTALLDIR}VC/VCPackages:$PATH"
export PATH="${c_FrameworkDir}${Framework35Version}:$PATH"
export PATH="${c_FrameworkDir}${FrameworkVersion}:$PATH"
export PATH="${c_VSINSTALLDIR}Common7/Tools:$PATH"
export PATH="${c_VSINSTALLDIR}VC/BIN:$PATH"
export PATH="${c_VSINSTALLDIR}Common7/IDE/:$PATH"
like image 28
Brooks Moses Avatar answered Sep 28 '22 16:09

Brooks Moses