Skip to main content

C++环境配置

核心组件

  • 安装MSYSY2 - 官网
  • 安装Clang
  • 安装LLVM

MSYSY2

1、下载msys2并配置

https://www.msys2.org 下载msys2,由于本人是win10 64位环境,所以选择msys2-x86_64-20201109.exe 。

msys2默认安装在C盘,在这里我自己不做修改,安装路径读者可自行决定

image-20230716115058140

成后安装打开开始菜单中的MSYS2,能成功进入以上界面即可

配置

打开C:\msys64\etc\pacman.d可以看到mirrorlist.mingw32mirrorlist.mingw64mirrorlist.msys三个文件,修改对应的下载源为国内的。

分别打开这第三个文件,分别加入这几行:

  • mirrorlist.mingw32
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686
  • mirrorlist.mingw64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64
  • mirrorlist.msys
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch

这三行是从清华镜像下载软件,速度会快很多。随后打开msys2,输入pacman -Sy同步软件包数据库。

随后打开msys2,输入pacman -Sy同步软件包数据库,然后再输入pacman -Su更新软件包,期间msys2提示你输入y之后会关闭,重新打开再输入一遍pacman -Su即可

在进行多次pacman -Su操作后,确认当前所有库版本为最新。

2、安装并配置Clang

打开msys2,输入:

# 安装gdb、make等
pacman -S mingw64/mingw-w64-x86_64-make mingw64/mingw-w64-x86_64-gdb mingw64/mingw-w64-x86_64-clang

按照提示,就可以完成Clang的安装了。

如果想安装Clangd,还得再执行

pacman -S mingw64/mingw-w64-x86_64-clang-tools-extra

添加环境变量

随后将C:\msys64\mingw64\bin添加到Path中,

检查安装是否成功:clang -vclangd

image-20230716121027716

使用clang -v不出错即可

image-20230716121131028

使用clangd 可以成功进入不报错即可。

# 使用
clang++ xxx.cpp

3、添加SublimeText的Build文件

  • windows
作者:南瓜瓜
链接:https://zhuanlan.zhihu.com/p/586687010
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

{
// Windows

// build only
"shell_cmd": "clang++ -std=c++11 -g -Wall ${file} -o ${file_path}/../bin/${file_base_name}",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only C and Cpp files can use this sublime-build file
"selector": "source.c, source.c++",
"variants": [
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "clang++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && start cmd /c \"\"${file_path}/../bin/${file_base_name}\" & pause \""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "clang++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
}
]
}

可以看到,我使用了两种运行方式——在sublime内部直接运行以及在 windows自带的cmd中运行,为什么要选用cmd呢?因为sublime有个缺点,就是它所有文本都是可以编辑的,也就是说我们没法像正常命令行一样输入xxxx,然后回车。(你们试试就知道我说的什么意思了)

注意:sublime-build文件中涉及很多方面的知识,而且极其脆弱,一改动就可能运行失败。

保存CppBuilder.sublime-build以后,通过Ctrl+Shift+B选择编译命令,选取Run In CMD:

4、使用Terminus代替cmd

Terminus是一个sublime插件,目的就是补全刚刚所说的缺点,实现sublime内置终端。

添加一个CppBuilder.sublime-buildbuild配置文件

{
// Windows
// build only
"shell_cmd": "clang++ -std=c++11 -g -Wall ${file} -o ${file_path}/../bin/${file_base_name}",
// check the file name using regex
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
// set the encoding to utf-8
"encoding": "utf-8",
// doesn't matter if I use full path
"working_dir": "${file_path}",
// only C and Cpp files can use this sublime-build file
"selector": "source.c, source.c++",
"variants": [
{
// build and run in Terminus (sublime plugin)
// [Recommanded]
"name": "Run In Terminus",
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"shell_cmd": "clang++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
},
{
// build and run in CMD
// you can type something in CMD then your progam can get its inputs
// [Recommanded]
"name": "Run in CMD",
"shell_cmd": "clang++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && start cmd /c \"\"${file_path}/../bin/${file_base_name}\" & pause \""
},
{
// build and run in sublime
// but you can't input anything to your progam
"name": "Run in Sublime",
"shell_cmd": "clang++ -std=c++11 -g -Wall \"${file}\" -o \"${file_path}/../bin/${file_base_name}\" && cmd /c \"${file_path}/../bin/${file_base_name}\""
}
]
}
  • OSX
{
// MacOS

"cmd": [
"bash",
"-c",
"clang++ '${file}' -std=c++11 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && ${file_path}/../bin/${file_base_name}"
],
"file_regex": "^(..{FNXX==XXFN}*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"encoding": "utf-8",
"selector": "source.c, source.c++",
"variants": [
{
"name": "Run In Terminus",
"target": "terminus_exec",
"cancel": "terminus_cancel_build",
"cmd": [
"bash",
"-c",
"clang++ '${file}' -std=c++11 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && ${file_path}/../bin/${file_base_name}"
]
},
{
"name": "Create Input File",
"cmd": [
"bash",
"-c",
"touch ${file_path}/../in_out/${file_base_name}.in && open -a Sublime\\ Text ${file_path}/../in_out/${file_base_name}.in"
]
},
{
"name": "Run In Terminal",
"cmd": [
"bash",
"-c",
"clang++ '${file}' -std=c++11 -stdlib=libc++ -o '${file_path}/../bin/${file_base_name}' && open -a Terminal.app '${file_path}/../bin/${file_base_name}'"
]
},
]
}

Create Input File是新建输入文件的意思,不需要的可以把它删掉。还有,提醒一下,sublime-build中最开始那行是"cmd","variants"里面也必须是“cmd”;最开始是"shell_cmd","variants"里面也必须是“shell_cmd”。

然后Ctrl+Shift+B选择如何编译,选择Run In Terminus

参阅文献