Search code examples
clangclang++

Is there any way to set clang 15 to compile c++17 by defaul?


Currently I compile my code using the following command. g++ helloWorld.cpp, however this defaults to I believe c++98(MacOs commandlinetools). I want it to compile in c++17/20. I know you can use the command g++ -std=c++17 helloWorld.cpp, which then forces it to compile in c++17. However is there any way to change this behavior so that it is default? When I run the command clang --version i get

clang version 16.0.6
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /opt/local/libexec/llvm-16/bin

clang 16 I know defaults to c++ 17, which will work for me, but when i run the command g++ --version i get the following. is there any way to change that?

Apple clang version 15.0.0 (clang-1500.3.9.4)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

this is the clang installed with apples commandlinetools, and the clang I have when I run clang --version, was installed using macports. Any help would be appreciated.

checking my clang version using the following command clang --version,


Solution

  • You have a few choices.

    1. avoid call g++ on mac and use explicit path,
    2. overwrite its definition by alias g++=/path/to/clang++16, or
    3. put shell script which wraps around g++

    In last choice, write following snippet:

    #!/bin/bash
    g++ -std=c++17 "$*"
    

    and place it under desired path (I name /path/to/wrap.bash for description purpose) which will be reachable from $PATH environment variable by following these steps:

    1. See how $PATH was set:
      echo "$PATH"
      
    2. Append /path/to to $PATH in your shell's setup script (~/.zshrc or ~/.bash_profile for locally; system-wide configuration is placed on different place):
      export PATH="$PATH:/path/to"