Switch command line tools on macOS

Asked 2 years ago, Updated 2 years ago, 44 views

How do I switch command line tools on macOS?

I have two command line tools installed in my environment.

I would like to use it while switching between 1 and 2 below.
If you look at the Command line tools in the XCode Preferences locations, you can only see what came with the XCode and you cannot switch.

What came with Xcode
(located in /Applications/Xcode.app/Contents/Developer)

Download and install only Command line tools from Apple Developer site (Command line tools beta 1 for XCode 10)
(installed in /Library/Developer)

xcode macos

2022-09-30 17:58

1 Answers

In general, it would be better to use virtual machines or switch active tools in partitions, but if you only use command line tools from Terminal, this may be the way to do it.

First, the Xcode command line tools running from Terminal are not running under /Applications/Xcode.app/Contents/Developer.
You can check this by trying whichclang or which swiftc from the Terminal, and you can see both of the following commands:

Below Developer is the usr folder.There is also bin underneath, where executables and libraries are located.

Therefore, in $HOME, create a file called .xcode10beta, and change the reference order of PATH.
Specifically

export PATH=/Library/Developer/usr/bin: $PATH
export CC=/Library/Developer/usr/bin/clang
export CXX=/Library/Developer/usr/bin/clang++
export CFLAGS="-I/Library/Developer/usr/local/include-std=gnu99-arch x86_64-Os"
export CXXFLAGS="-I/Library/Developer/usr/local/include-std=gnu++11-arch x86_64-Os"
exportLDFLAGS="-L/Library/Developer/usr/local/lib-Os-arch x86_64"

Create a file similar to the one shown in .
This file is for bash/zsh, so if you use csh, please rewrite it accordingly.

If you want to switch the execution environment, you can type source~/.xcode10beta from the command line to switch the command line tool.

The important thing is to write /Library/Developer/usr/bin in the search path of the previous executable file, and to prioritize the executable in /Library/Developer/usr/bin if any.
The second line and beyond are options for the clang C compiler, so when you use swiftc, check how to configure the environment when compiling the swift file with swiftc--help and rewrite it accordingly.


2022-09-30 17:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.