C++/常用编译器

< C++

GCC 编辑

gcc.exe是C语言编译器,但也能根据文件扩展名(.cc或.cpp)自动调用g++.exe。不过,它调用的链接器仍然使用C语言库,因此编译C++程序时链接会报错。

所以,编译C++程序,推荐:

g++ your.cpp

或者

gcc your.cpp -lstdc++

LLVM/CLANG 编辑

Windows平台安装使用CLANG 编辑

下载64位的 LLVM 压缩包,解压缩到硬盘。注意,clang官方是用MSVC编译生成的。

创建一个test.c源程序:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("Hello World!");    

    return 0;
}

编译时,如果用MSVC的资源,则

  • 第一步:创建一个批处理文件
call "%VS140COMNTOOLS%..\..\VC\vcvarsall.bat" amd64
set PATH=%~dp0bin;%PATH%
cmd /k
  • 第二步:在一个命令行窗口,运行上述批处理文件
  • 第三步:执行命令
clang++ your.cpp

编译时,如果用MinGW的资源,则

  1. 下载mingw64压缩包
  2. 将mingw64解压到LLVM环境中 (mingw64如果放到其他目录中,运行clang test.c出现no find header)
clang++ test.cpp -o hello.exe --verbose -target x86_64-pc-windows-gnu

或者

clang test.cpp -o -lstdc++ hello.exe --verbose -target x86_64-pc-windows-gnu

或者

clang -x c++ test.cpp -o -lstdc++ hello.exe --verbose -target x86_64-pc-windows-gnu

Visual C++ 编辑

Visual C++的Community版本对个人开发者、开源开发者免费试用。安装使用方法:[1]

  1. 从官网下载installer:https://aka.ms/vs/17/release/vs_community.exe
  2. 选定一个本地文件夹保存要下载的安装程序,例如 D:\vs2022
  3. 打开 命令行窗口,进入vs_community.exe所在的文件夹,执行: vs_community.exe --layout D:\vs2022 --lang en-US zh-CN (这是安装全部组建)或者 vs_community.exe --layout D:\vs2022 --lang en-US zh-CN --add Microsoft.VisualStudio.Workload.ManagedDesktop --add Microsoft.VisualStudio.Workload.NetWeb --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended 分别是安装.NET Web开发、.NET 桌面开发、C++ 桌面开发,安装相关工作负载的推荐组件
  4. 验证下载结果: vs_community.exe --layout D:\vs2022 --verify
  5. 断网离线安装: vs_community.exe --layout D:\vs2022 --noweb --lang en-US zh-CN --add Microsoft.VisualStudio.Workload.ManagedDesktop --add Microsoft.VisualStudio.Workload.NetWeb --add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended


Intel C++ 编辑

参考文献 编辑

  1. Create, maintain, and deploy a network installation of Visual Studio