在 Cygwin shell 中调用 cl.exe(MSVC 编译器)

发布于 2024-07-11 11:21:30 字数 288 浏览 3 评论 0原文

我大量使用 Cygwin(使用 PuTTY shell)。 但是,在 Cygwin Bash shell 中调用 cl.exe(即 Visual C++ 编译器工具链)非常棘手。 在 Bash shell 中运行 vcvars*.bat 显然不起作用。 我尝试将VC++的环境变量迁移到Cygwin,但这并不是那么容易。

如何在 Cygwin 的 Bash shell 中运行 VC++ 编译器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

·深蓝 2024-07-18 11:21:30

我通常通过添加

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

到 c:/cygwin/cygwin.bat 来解决这个问题。 请注意,VS80COMNTOOLS 变量非常有用,因为它为您提供了一种万无一失(hm)的定位 vsvars32.bat 的方法。

另一种方法是这样的,它允许您轻松地在不同的 Studio 版本之间切换:

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 "$@"
}

您现在可以执行以下操作:

$ 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... ]

记下编译器版本。

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.

清音悠歌 2024-07-18 11:21:30

我知道您的问题是将 vcvars32.bat 转换为 shell 脚本。

解决该问题的一种方法是基于这样的想法:当一个进程启动另一个进程时,环境变量会被继承。 所以你可以简单地在cmd下运行vcvars32,然后运行bash。 这在我的机器上运行良好:

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

或者,在 cmd 下调用 vcvars32 之前和之后运行 set,然后创建一个 shell 脚本来设置环境变量。

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.

淡墨 2024-07-18 11:21:30

根据其他答案中的建议,我对使用和不使用 MSVC 变量的 bash“设置”结果进行了比较,并提出了以下脚本来为我安装的 Microsoft Visual C++ 2010 Express 重现它们。 我对其进行了一些重构,将所有依赖于安装的部分放在顶部,因此希望这对其他人有用。

# 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"

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"
执笏见 2024-07-18 11:21:30

我实际上使用了 JesperE 答案 https://stackoverflow.com/a/374411/380247 (它允许您在 VS 之间切换)版本)但找到了一种不创建临时bat文件的方法。 所以结果就简单多了。

function run_in_vs_env
{
    eval vssetup="\$1\\vsvars32.bat"
    cmd /Q /C call "$vssetup" "&&" "${@:2}"
}

function run_vs11
{
    run_in_vs_env VS110COMNTOOLS "$@"
}

function run_vs10
{
    run_in_vs_env VS100COMNTOOLS "$@"
}

我建议将这些行添加到您的 .bash_functions 或类似的内容中,然后在那里导出这些函数。 这将允许您在 bash 脚本中使用函数。

export -f run_in_vs_env
export -f run_vs11
export -f run_vs10

那么你应该能够做到:

run_vs11 cl

I actually used JesperE answer https://stackoverflow.com/a/374411/380247 (that allows you to switch between VS versions) but found a way not to create temporary bat file. So result is much simpler.

function run_in_vs_env
{
    eval vssetup="\$1\\vsvars32.bat"
    cmd /Q /C call "$vssetup" "&&" "${@:2}"
}

function run_vs11
{
    run_in_vs_env VS110COMNTOOLS "$@"
}

function run_vs10
{
    run_in_vs_env VS100COMNTOOLS "$@"
}

I suggest to add these lines to your .bash_functions or something similar and export these functions there. This will allow you to use functions in your bash scripts.

export -f run_in_vs_env
export -f run_vs11
export -f run_vs10

Then you should be able to do:

run_vs11 cl
青衫儰鉨ミ守葔 2024-07-18 11:21:30

另一种选择是打开 vcvars32.bat(或 vsvars32.bat,它在最新版本的 Visual Studio 中真正起作用),查看它的功能,然后在适当的 shell 脚本中复制它。

它并不是特别复杂——它所做的只是设置一堆环境变量。

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.

辞取 2024-07-18 11:21:30

我已使用 visual_studio.env 文件转换了 vsvars32.bat 文件。 当我需要使用命令行Visual Studio的环境时,我只需做一个这个文件的源。

sh shell 环境中,我无法注册 Windows 路径(\;sh 冲突),因此
在使用 cygpath -au 或 cygpath -aup 命令翻译它们之前,我将它们写到我的 .env 文件中,然后使用 <代码>cygpath -aw 或cygpath -aup 命令

我的 visual_studio.env 文件如下所示:

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

时光礼记 2024-07-18 11:21:30

我发现使用 msbuild 是一个更好的策略,同时我还可以使用 cygwin 环境的 g++。 它是一种更加集成的方法,可以与您现有的 Visual Studio 开发和谐地融合在一起。 使用最底层的cl,设置环境变量,就是自找麻烦,逆风而行。 msbuild 可以更好地为我们设置东西。 另外,我可以逐步调试到具有完整的 *.vcxproj 项目的 IDE。

ls -l ~/bin/msbuild
lrwxrwxrwx 1 johnny Domain Users 102 Sep  4 04:25 /home/johnny/bin/msbuild -> /cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin/MSBuild.exe

然后在你的~/.bashrc中,有这个函数:

builddebug (){
    [ $# -eq 0 ] && return 1;
    msbuild /m /p:Configuration=Debug /p:Platform=Win32 "$1"
}

开发体验的感觉看起来像这个。 至少,我能够以更好的集成方式使用 g++ 编译器和 Visual Studio 2017 编译器。 让 msbuild 做它的事情。 不是cl

I find it a better strategy to use msbuild and then at the same time, I can also use g++ of my cygwin environment. It is a more integrated approach and blends harmoniously with the your existing visual studio development. Using the lowest level cl and setting up environment variables is asking for trouble and going against the wind. The msbuild can better setup things for us. Also, I can step-debug into the IDE having a full-pledge *.vcxproj project.

ls -l ~/bin/msbuild
lrwxrwxrwx 1 johnny Domain Users 102 Sep  4 04:25 /home/johnny/bin/msbuild -> /cygdrive/c/Program Files (x86)/Microsoft Visual Studio/2017/Professional/MSBuild/15.0/Bin/MSBuild.exe

Then in your ~/.bashrc, have this function:

builddebug (){
    [ $# -eq 0 ] && return 1;
    msbuild /m /p:Configuration=Debug /p:Platform=Win32 "$1"
}

The feel of the development experience looks like this. At least, i'm able to use the g++ compiler and the Visual Studio 2017 compiler in a better integrated fashion. Let msbuild do its thing. Not cl.

如若梦似彩虹 2024-07-18 11:21:30

对我来说,.bash_profile 中的以下内容有效:

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

For me, the following in .bash_profile worked:

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

蓦然回首 2024-07-18 11:21:30

我发现在 cygwin 环境中使用 msvc 编译内容非常有用的一个实用程序是我在 Coin 3D 中找到的一个包装器 库的源存储库名为“wrapmsvc”,可以找到其二进制文件 在这里

该程序包装 cl.exe 并将指定的任何 GCC 参数转换为适当的 cl.exe 参数。 它还使用 cygwin API 将文件路径从 cygwin 形式 (/cygdrive/c/temp/test.c) 正确转换为实际文件路径 (C:\temp\test.c)。

上次我花了一些时间才找到源代码,但它的名称是“wrapmsvc.cpp”,因此如果您需要编译它,请查找该文件。 如果您碰巧编译它并且收到一些有关使用 cygwin_conv_to_posix_path 或 cygwin_conv_to_win32_path 的折旧警告/错误,请进行以下更改:

将行:更改

(void)cygwin_conv_to_posix_path(s.c_str(), buf);

(void)cygwin_conv_path(CCP_WIN_A_TO_POSIX, (const void *)s.c_str(), (void *)buf, (size_t)MAX_PATH);

并将:更改

(void)cygwin_conv_to_win32_path(s.c_str(), buf);

(void)cygwin_conv_path(CCP_POSIX_TO_WIN_A, (const void *)s.c_str(), (void *)buf, (size_t)MAX_PATH);

One utility I found pretty invaluable for compiling stuff with msvc from a cygwin environment is a wrapper I found in the Coin 3D library's source repository called "wrapmsvc", who's binary can be found here.

The program wraps cl.exe and converts any GCC args specified to the appropriate cl.exe arg. It also uses cygwin API to correctly translate the filepath from cygwin form (/cygdrive/c/temp/test.c) to it's actual file path (C:\temp\test.c).

The source took me a little while to find last time, but it's called "wrapmsvc.cpp", so if you need to compile it, look for that file. If you do happen to compile it and you get some depreciation warnings/errors about the use of cygwin_conv_to_posix_path or cygwin_conv_to_win32_path, make the following changes:

Change the line:

(void)cygwin_conv_to_posix_path(s.c_str(), buf);

to

(void)cygwin_conv_path(CCP_WIN_A_TO_POSIX, (const void *)s.c_str(), (void *)buf, (size_t)MAX_PATH);

and change:

(void)cygwin_conv_to_win32_path(s.c_str(), buf);

to

(void)cygwin_conv_path(CCP_POSIX_TO_WIN_A, (const void *)s.c_str(), (void *)buf, (size_t)MAX_PATH);
笔芯 2024-07-18 11:21:30

我无法向Diomidis的回复添加评论 :(,所以不得不发布答案。
我同意他的回答,但“打开命令提示符”,运行 Visual Studio/vcvars32.bat,然后运行 ​​Bash 会很乏味。 如果您不想让环境变得混乱,最好的替代方法是修改 Cygwin.bat 并将其修改为如下所示:

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

将环境变量 VS71COMNTOOLS 替换为适合您计算机的变量。

无需打开命令提示符并手动运行 Bash。 我非常喜欢这个解决方案:)。 我无法注明此黑客攻击的真正作者,因为我找不到返回他文章的链接,对此感到抱歉!

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!

美羊羊 2024-07-18 11:21:30

我的 Igor 解决方案版本比较简短:

cl() {
  tmpfile="/tmp/tmp$.bat"
  echo "@echo off" > $tmpfile
  echo "call \"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat\" >NUL:" >> $tmpfile
  echo "bash -c \"cl %*\"" >> $tmpfile
  cmd /c `cygpath -m "$tmpfile"` "$@"
  status=$?
  rm -f $tmpfile
  return $status
}

遗憾的是 vcvars64.bat 需要重复调​​用。

My version of Igor's solution, it's briefer:

cl() {
  tmpfile="/tmp/tmp$.bat"
  echo "@echo off" > $tmpfile
  echo "call \"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat\" >NUL:" >> $tmpfile
  echo "bash -c \"cl %*\"" >> $tmpfile
  cmd /c `cygpath -m "$tmpfile"` "$@"
  status=$?
  rm -f $tmpfile
  return $status
}

it's a pity that vcvars64.bat needs to be called repetitively.

烟花易冷人易散 2024-07-18 11:21:30

其他答案都不适合我,但这确实:

  1. 运行“MS Build Command Prompt”

在此处输入图像描述

  1. 转储环境变量:set > c:\temp\cl.env

  2. 打开 cygwin 命令提示符并创建 source 脚本:

awk < /cygdrive/c/temp/cl.env -F= '{ if($1 !~ ")") print "export " $1 "=\x27" $2 "\x27" }' > cl.source
  1. 修改 cl.source 以将 TEMP 和 TMP 更改为 C:\Temp 例如 TEMP='C:\Temp'

  2. 现在,每当您需要 cl 环境时,请从 cygwin 提示符运行:
    source cl.source

  3. (可选)将 cl.source 放入您的 .bashrc 文件中,以便在您登录时自动运行

None of the other answers worked for me, but this did:

  1. Run "MS Build Command Prompt"

enter image description here

  1. dump the env vars: set > c:\temp\cl.env

  2. Open a cygwin command prompt and create a source script:

awk < /cygdrive/c/temp/cl.env -F= '{ if($1 !~ ")") print "export " $1 "=\x27" $2 "\x27" }' > cl.source
  1. Modify cl.source to change TEMP and TMP to C:\Temp e.g. TEMP='C:\Temp'

  2. Now, whenever you need the cl environment, from your cygwin prompt run:
    source cl.source

  3. Optionally, source cl.source in your .bashrc file to run automatically when you log in

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文