MacOS配置C++开发环境

作为一名程序员,一定要学一下C语言和C++,因此本帖记录下在MacOS环境中进行C++学习的开发环境配置步骤,以及各种编译器的安装。如有错误,还望指正。

前置条件

  • 电脑:macOS BigSur Version 11.2.3(20D91)
  • 工具:homebrew
  • IDE:JetBrains Clion

安装编译器

Xcode Command Line Tools 安装

首先我们需要安装 command line tools,可以参考 How to Install Xcode Command Line Tools on a Mac 完成这一步之后,我们会拥有 gccg++这两款编译器(事实上安装的clang),即 Apple 官方默认的编译器,安装位置在 /usr/bin
到这里
验证是否安装完成:

MBP cpp_practice % gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

MBP cpp_practice % g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

brew 安装

通过 brew 安装就简单很多,当然国内有网速的问题,因此我们首先需要设置 homebrew 的源,可以参考 Homebrew / Linuxbrew 镜像使用帮助
直接输入 brew install gcc 即可安装成功,默认安装的位置在 /usr/local/bin
此时我们 PATH 中会有两个 gcc,可以通过设置 PATH 来设置默认的 gcc,比如想奖 brew安装的 gcc 放在前面,只需要在 PATH设置中将 /usr/local/bin 放在 /usr/bin 前面即可,关于 PATH 的设置,可以参考 Mac环境变量PATH的配置
验证是否安装完成:

MBP cpp_practice % gcc-11 --version 
gcc-11 (Homebrew GCC 11.2.0_1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

C++标准

C++ 发展至今出现过不同的标准,如果是一些使用新标准开发的代码就需要使用支持该标准的编译器进行编译,我们可以使用 __cplusplus 来查看当前编译器支持的标准,示例代码如下。

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    std::cout << __cplusplus << std::endl;
#if __cplusplus == 201402L
    std::cout << "C++14" << std::endl;
#elif __cplusplus == 201103L
    std::cout << "C++11" << std::endl;
#else
    std::cout << "c/c++" << std::endl;
#endif
    return 0;
}

我们键入以下命令来进行编译和执行上述代码。

注意如果出现Undefined symbols,又不属于自己代码里的结构,那么很大部分原因是库函数引用出错,编译项里加 -lstdc++

// default gcc 
MBP cpp_practice % gcc c++ver.cpp -o default-gcc-c++compiler -lstdc++
MBP cpp_practice % ./default-gcc-c++compiler 
Hello, World!
199711
c/c++

// default g++
MBP cpp_practice % g++ c++ver.cpp -o default-g++-c++compiler
MBP cpp_practice % ./default-g++-c++compiler 
Hello, World!
199711
c/c++

// brew gcc
MBP cpp_practice % gcc-11 c++ver.cpp -o gcc11-c++compiler -lstdc++
ld: warning: dylib (/usr/local/Cellar/gcc/11.2.0_1/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.5) than being linked (11.2)
MVP cpp_practice % ./gcc11-c++compiler 
Hello, World!
201703
c/c++

// brew g++
MBP cpp_practice % g++-11 c++ver.cpp -o g++-11-c++compiler
ld: warning: dylib (/usr/local/Cellar/gcc/11.2.0_1/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.5) than being linked (11.2)
MBP cpp_practice % ./g++-11-c++compiler 
Hello, World!
201703
c/c++

// default clang
MBP cpp_practice % clang c++ver.cpp -o clang-c++complier 
ld: warning: dylib (/usr/local/Cellar/gcc/11.2.0_1/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.5) than being linked (11.2)
MBP cpp_practice %  ./clang-c++complier 
Hello, World!
199711
c/c++

cppreference has information on the standard values of the __cplusplus macro in the section “Predefined macros.” Currently the standard values are:
199711L (C++98 or C++03)
201103L (C++11)
201402L (C++14)
201703L (C++17)
202002L (C++20)

上述代码用到了 __cplusplus, 该值体现了当前编译器使用的c++标准 可以看Apple编译器 clang 默认使用C++98或者C++03,所以当使用更高C++标准开发的代码时必须使用 -std 指定对应的标准,如何确定自己的编译器支持的标准以及默认标准,可以使用 man gcc 来进行查看

Reference

  1. C++ newbie tour: getting started with c++ on mac osx
  2. mac上使用g++编译出错“Undefined symbols for architecture x86_64:” 错误解决办法
  3. How to Install Xcode Command Line Tools on a Mac
  4. Is there a standard definition for __cplusplus in c++14?
  5. C++ preprocessor macro __cplusplus
  6. 如何确定gcc是否支持c11,c14,c17
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值