在 Cygwin shell 中调用 cl.exe(MSVC 编译器)
我大量使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我通常通过添加
到 c:/cygwin/cygwin.bat 来解决这个问题。 请注意,VS80COMNTOOLS 变量非常有用,因为它为您提供了一种万无一失(hm)的定位 vsvars32.bat 的方法。
另一种方法是这样的,它允许您轻松地在不同的 Studio 版本之间切换:
您现在可以执行以下操作:
记下编译器版本。
I usually solve this by adding
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:
You can now do:
Note the compiler version.
我知道您的问题是将 vcvars32.bat 转换为 shell 脚本。
解决该问题的一种方法是基于这样的想法:当一个进程启动另一个进程时,环境变量会被继承。 所以你可以简单地在cmd下运行vcvars32,然后运行bash。 这在我的机器上运行良好:
或者,在 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:
Alternatively, run set before and after invoking vcvars32 under cmd, and then create a shell script to set the environment variables.
根据其他答案中的建议,我对使用和不使用 MSVC 变量的 bash“设置”结果进行了比较,并提出了以下脚本来为我安装的 Microsoft Visual C++ 2010 Express 重现它们。 我对其进行了一些重构,将所有依赖于安装的部分放在顶部,因此希望这对其他人有用。
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.
我实际上使用了 JesperE 答案 https://stackoverflow.com/a/374411/380247 (它允许您在 VS 之间切换)版本)但找到了一种不创建临时bat文件的方法。 所以结果就简单多了。
我建议将这些行添加到您的 .bash_functions 或类似的内容中,然后在那里导出这些函数。 这将允许您在 bash 脚本中使用函数。
那么你应该能够做到:
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.
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.
Then you should be able to do:
另一种选择是打开 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.
我已使用
visual_studio.env
文件转换了vsvars32.bat
文件。 当我需要使用命令行Visual Studio的环境时,我只需做一个这个文件的源。在
sh
shell 环境中,我无法注册 Windows 路径(\
和;
与sh
冲突),因此在使用 cygpath -au 或 cygpath -aup 命令翻译它们之前,我将它们写到我的 .env 文件中,然后使用 <代码>cygpath -aw 或
cygpath -aup 命令
。我的
visual_studio.env
文件如下所示:我希望这可以帮助您。
I have converted my
vsvars32.bat
file with myvisual_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 withsh
) sobefore I translate them with
cygpath -au
orcygpath -aup commands
then I write down them into my.env
file and translate them back withcygpath -aw
orcygpath -aup commands
.My
visual_studio.env
file look like this:I hope this can help you.
我发现使用
msbuild
是一个更好的策略,同时我还可以使用 cygwin 环境的g++
。 它是一种更加集成的方法,可以与您现有的 Visual Studio 开发和谐地融合在一起。 使用最底层的cl
,设置环境变量,就是自找麻烦,逆风而行。msbuild
可以更好地为我们设置东西。 另外,我可以逐步调试到具有完整的*.vcxproj
项目的 IDE。然后在你的~/.bashrc中,有这个函数:
开发体验的感觉看起来像这个。 至少,我能够以更好的集成方式使用
g++
编译器和Visual Studio 2017
编译器。 让msbuild
做它的事情。 不是cl
。I find it a better strategy to use
msbuild
and then at the same time, I can also useg++
of my cygwin environment. It is a more integrated approach and blends harmoniously with the your existingvisual studio
development. Using the lowest levelcl
and setting up environment variables is asking for trouble and going against the wind. Themsbuild
can better setup things for us. Also, I can step-debug into the IDE having a full-pledge*.vcxproj
project.Then in your ~/.bashrc, have this function:
The feel of the development experience looks like this. At least, i'm able to use the
g++
compiler and theVisual Studio 2017
compiler in a better integrated fashion. Letmsbuild
do its thing. Notcl
.对我来说,.bash_profile 中的以下内容有效:
"`cygpath -ua "$VS80COMNTOOLS/vsvars32.bat"`" > NUL
For me, the following in .bash_profile worked:
"`cygpath -ua "$VS80COMNTOOLS/vsvars32.bat"`" > NUL
我发现在 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 的折旧警告/错误,请进行以下更改:
将行:更改
为
并将:更改
为
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:
to
and change:
to
我无法向Diomidis的回复添加评论 :(,所以不得不发布答案。
我同意他的回答,但“打开命令提示符”,运行 Visual Studio/
vcvars32.bat
,然后运行 Bash 会很乏味。 如果您不想让环境变得混乱,最好的替代方法是修改Cygwin.bat
并将其修改为如下所示:将环境变量 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 modifyCygwin.bat
and modify it to look something like this: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!
我的 Igor 解决方案版本比较简短:
遗憾的是 vcvars64.bat 需要重复调用。
My version of Igor's solution, it's briefer:
it's a pity that vcvars64.bat needs to be called repetitively.
其他答案都不适合我,但这确实:
转储环境变量:
set > c:\temp\cl.env
打开 cygwin 命令提示符并创建
source
脚本:修改 cl.source 以将 TEMP 和 TMP 更改为 C:\Temp 例如
TEMP='C:\Temp'
现在,每当您需要 cl 环境时,请从 cygwin 提示符运行:
source cl.source
(可选)将 cl.source 放入您的 .bashrc 文件中,以便在您登录时自动运行
None of the other answers worked for me, but this did:
dump the env vars:
set > c:\temp\cl.env
Open a cygwin command prompt and create a
source
script:Modify cl.source to change TEMP and TMP to C:\Temp e.g.
TEMP='C:\Temp'
Now, whenever you need the cl environment, from your cygwin prompt run:
source cl.source
Optionally, source cl.source in your .bashrc file to run automatically when you log in