mac进行acm(C/C++)编程

在windows下咱们每每使用CB(CodeBlocksks)进行acm的编程,不用创建工程,能够直接编译并运行,而后弹出黑框框,sublime也是这样,只要MinGW搞好就行html

在ubuntu下我也经常使用CB,虽然有时候会出现小问题,可是仍是很是稳定的c++

在mac os下就比较头疼,CB是远古版本了,并且在OSX下可能闪退,可是咱们总不可能使用Xcode进行C/C++代码编写吧,比较麻烦git

前后配置了sublime和VSCode,最终仍是选择了VSCode,比较简单好用github

去微软官网下载以后拖到应用程序打开,而后下载C/C++的MS插件,而后下载codeRun(由于task的体验很差,不能编译并运行)编程

编译和运行其实就是一行命令行的事("g++ A.cpp -std=c++11 -o A && ./A),咱们配置codeRun到这样,而后加入一些人性化操做json

这个就是setting.json,能够点击查看->命令面板->找到设置ubuntu

 

    // 在终端中运行编译命令,不然咱们没法与程序经过标准输入交互
    "code-runner.runInTerminal": true,
    // 运行代码以前清除以前的输出
    "code-runner.clearPreviousOutput": true,
    // 开启这个后在运行编译命令以前会自动 cd 至文件所在目录
    "code-runner.fileDirectoryAsCwd": true,
    // 这里只保留了 C 和 C++ 的编译命令,有须要其余语言的请自行添加
    "code-runner.executorMap": {
        "c": "gcc $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt",
        "cpp": "g++ $fileName -std=c++11 -o $fileNameWithoutExt && ./$fileNameWithoutExt",
    },
    // 运行代码后切换焦点至终端,方便直接输入测试数据
    "code-runner.preserveFocus": false,
    // 在运行代码以前保存文件
    "code-runner.saveFileBeforeRun": true,

可是你会发现你可能没有一个正常的GCC编译器,若是有Xcode,那么将是clang,他不支持万能头文件,并且标准不太同样windows

这时候能够经过Homebrew安装GCCruby

若是没有安装Homebrew能够在终端执行这个指令,关于这个东西的介绍和使用能够到这里 https://brew.sh/index_zh-cn.htmlbash

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装gcc也很简单

brew install gcc

安装好了只要改一下~/bash_profile就行了,加入以下几行,由于我是安装的9,至于你是多少请随机应变

export PATH="/usr/local/Cellar/gcc/9.1.0/bin:$PATH"
alias gcc='gcc-9'
alias g++='g++-9'
alias c++='c++-9'

输入g++-9 -v显示这一坨就说明你好了,你在命令行编译cpp文件就可使用万能头文件了

sing built-in specs.
COLLECT_GCC=g++-9
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/9.1.0/libexec/gcc/x86_64-apple-darwin18/9.1.0/lto-wrapper
Target: x86_64-apple-darwin18
Configured with: ../configure --build=x86_64-apple-darwin18 --prefix=/usr/local/Cellar/gcc/9.1.0 --libdir=/usr/local/Cellar/gcc/9.1.0/lib/gcc/9 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 9.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
Thread model: posix
gcc version 9.1.0 (Homebrew GCC 9.1.0) 

而后VSC右键或者快捷键直接运行,就能够get到酸爽了

 

转载于:https://www.cnblogs.com/BobHuang/p/11113806.html