Invoking cl.exe (MSVC compiler) in Cygwin shell

前端 未结 12 2215
执念已碎
执念已碎 2020-12-04 16:50

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. R

相关标签:
12条回答
  • 2020-12-04 17:14

    For me, the following in .bash_profile worked:

    "`cygpath -ua "$VS80COMNTOOLS/vsvars32.bat"`" > NUL

    0 讨论(0)
  • 2020-12-04 17:19

    I couldn't add comments to Diomidis's reply :(, so had to post an answer instead. I agree with his answer, but it would be tedious to do "open a command prompt", run Visual Studio/vcvars32.bat and then run Bash. If you don't bother about cluttering you environment, the best alternative is to modify Cygwin.bat and modify it to look something like this:

    @echo off
    D:
    chdir D:\cygwin\bin
    "%VS71COMNTOOLS%\vsvars32.bat" && bash --login -i
    

    Substitute the environment variable VS71COMNTOOLS with something appropriate on your machine.

    There is no need to open a command prompt and run Bash manually. I like this solution a lot :). I couldn't give credits to the real author of this hack, as I couldn't find a link back to his article, sorry about that!

    0 讨论(0)
  • 2020-12-04 17:20

    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.

    0 讨论(0)
  • 2020-12-04 17:21

    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"
    
    0 讨论(0)
  • 2020-12-04 17:21

    I have converted my vsvars32.bat file with my visual_studio.env file. When I need to use the command-line Visual Studio's environment, I just do a source of this file.

    In the sh shell environment, I cannot register the Windows path (\ and ; clash with sh) so before I translate them with cygpath -au or cygpath -aup commands then I write down them into my .env file and translate them back with cygpath -aw or cygpath -aup commands.

    My visual_studio.env file look like this:

    VS80COMNTOOLS=$(cygpath -aw '/cygdrive/c/Programmi/Microsoft Visual Studio 8/Common7/Tools/'); export VS80COMNTOOLS
    VSINSTALLDIR=$(cygpath -aw '/cygdrive/c/Programmi/Microsoft Visual Studio 8'); export VSINSTALLDIR
    VCINSTALLDIR=$(cygpath -aw '/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC'); export VCINSTALLDIR
    FrameworkDir=$(cygpath -aw '/cygdrive/c/WINDOWS/Microsoft.NET/Framework'); export FrameworkDir
    FrameworkVersion='v2.0.50727'; export FrameworkVersion
    FrameworkSDKDir=$(cygpath -aw '/cygdrive/c/Programmi/Microsoft Visual Studio 8/SDK/v2.0'); export FrameworkSDKDir
    
    echo Setting environment for using Microsoft Visual Studio 2005 x86 tools.
    
    DevEnvDir=$(cygpath -aw '/cygdrive/c/Programmi/Microsoft Visual Studio 8/Common7/IDE'); export DevEnvDir
    
    PATH='/cygdrive/c/Programmi/Microsoft Visual Studio 8/Common7/IDE:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/BIN:/cygdrive/c/Programmi/Microsoft Visual Studio 8/Common7/Tools:/cygdrive/c/Programmi/Microsoft Visual Studio 8/Common7/Tools/bin:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/PlatformSDK/bin:/cygdrive/c/Programmi/Microsoft Visual Studio 8/SDK/v2.0/bin:/cygdrive/c/WINDOWS/Microsoft.NET/Framework/v2.0.50727:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/VCPackages':$PATH
    INCLUDE=$(cygpath -awp '/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/ATLMFC/INCLUDE:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/INCLUDE:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/PlatformSDK/include:/cygdrive/c/Programmi/Microsoft Visual Studio 8/SDK/v2.0/include'); export INCLUDE
    LIB=$(cygpath -awp '/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/ATLMFC/LIB:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/LIB:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/PlatformSDK/lib:/cygdrive/c/Programmi/Microsoft Visual Studio 8/SDK/v2.0/lib'); export LIB
    LIBPATH=$(cygpath -awp '/cygdrive/c/WINDOWS/Microsoft.NET/Framework/v2.0.50727:/cygdrive/c/Programmi/Microsoft Visual Studio 8/VC/ATLMFC/LIB'); export LIBPATH
    

    I hope this can help you.

    0 讨论(0)
  • 2020-12-04 17:25

    Another alternative is to open up vcvars32.bat (or vsvars32.bat, which does the real work in recent version of Visual Studio), look at what it does, and replicate that in the appropriate shell script.

    It's not particularly complex - all it does is set a bunch of environment variables.

    0 讨论(0)
提交回复
热议问题