Rust在windows上的配置的注意事项

作者 @飞洲人飞舟魂转载请注明出处.

Rust 在windows上的配置比linux上要复杂些,原因是因为windows可以使用两套ABI,其中一个是windows自己搞的MSVC工具链,另外一套则是GNU工具链.以前我一直对windows上选择和配置迷惑不解,正好今天研究了一下,现在记录一下.

安装时(详细讲解MSVC的配置)

要是执行官方提供的rustup.exe程序,应该会出现如下的提示.


Rust requires the Microsoft C++ build tools for Visual Studio 2013 or later,
but they don't seem to be installed.

The easiest way to acquire the build tools is by installing Microsoft Visual
C++ Build Tools 2019 which provides just the Visual C++ build tools:

https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019

Please ensure the Windows 10 SDK and the English language pack components are
included when installing the Visual C++ Build Tools.

Alternately, you can install Visual Studio 2019, Visual Studio 2017, Visual
Studio 2015, or Visual Studio 2013 and during install select the "C++ tools":

https://visualstudio.microsoft.com/downloads/

Install the C++ build tools before proceeding.

If you will be targeting the GNU ABI or otherwise know what you are doing then
it is fine to continue installation without the build tools, but otherwise,
install the C++ build tools before proceeding.

Continue? (Y/n)

这里就是要求你选择用微软的MSVC编译链,还是GNU编译链.
官方推荐使用微软的MSVC工具链,毕竟是在微软的操作系统上,不管怎样都更加兼容些.
要是执意要安装GNU编译链的话那就继续,如果你想要Rust官方推荐的配置那就请安装微软的C++ build tools然后再重新打开Rust安装程序.
这个C++ build tools如何安装呢?有两种方法,第一种最简单,那就是安装Visual studio 2013或者以上版本,软件安装完毕后C++ tools就已经安装在你的电脑上了;要是你嫌Visual stdio太过于臃肿,你也可以选择单独到这个页面安装这个软件包,如下:
在这里
下载这个
安装的时候一定注意这些都勾选上.
安装时

安装完毕后安装器应当有如下界面 (我还安装了vs 2017,你完全可以不用装):

装完了之后,你就可以再启动rustup.exe来安装Rust.

关于我踩过的一些坑

  • Rustup安装程序是需要在线安装的,由于国内网络原因,这个过程非常缓慢,我们需要设置镜像地址来加速,像下面这样设置就可以,具体方法可以百度搜索"Rustup 镜像"关键词.

  • 如果你安装GNU工具链的话,最好独立安装一个MingGW.虽然说Rust的安装程序已经内置了GNU的工具链,
    但是就我的经历而言体验并不好,我就在编译一个项目时出现了链接错误.另外你最好也别用TDM-GCC这个GNU工具链,因为这个工具链时GNU的一个分支,而它和Rust的兼容性不好,经常会莫名编译失败.

  • 如果你决定使用MSVC编译工具链,而且你同时又安装了GNU工具链,那么就把CC,CXX这两个环境变量在编译Rust前去掉,否则会出现链接错误.

posted @ 2021-02-22 15:25  飞洲人飞舟魂  阅读(924)  评论(0编辑  收藏  举报
.c_ad_block { display: none !important; }