Post

Mac에서 Clang++ 먹통

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Clang++ `<iostream>` Not Working on Mac

## Introduction

After updating macOS software to the latest version, C++ sometimes strangely stops working with `#include <iostream>`. More specifically, it seems like the system cannot find the `.dll` file where `<iostream>` is defined in the first place. Of course, if you can fix this manually, you can do that. But I did not want to.

I searched Stack Overflow and similar sites, but most of the answers said the same thing: `brew install gcc`. In other words, they were basically telling me to use GCC instead of Clang.

However, this can cause problems. Some packages explicitly set the compiler to `clang` and `clang++` inside their `Makefile`. Since failed package installations often remove previously installed packages, `clang++` needs to handle `#include <iostream>` properly.

## Solution 1

The simplest solution is to install Command Line Tools from the terminal.

```shell
xcode-select --install

This method works if Command Line Tools is not already installed. If it does not solve the issue, refer to Solution 2.

Solution 2

Solution 2 is for cases where Command Line Tools is already installed.

1
xcode-select: note: Command line tools are already installed. Use "Software Update" in System Settings or the softwareupdate command line interface to install updates

You will probably see a message like this, saying that it is already installed. The solution is very simple: delete it and reinstall it. Run the following commands in the terminal.

1
2
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

This is how I fixed the issue. I wasted two whole days on this one problem. Damn it.

Verification

Let’s verify it with C++.

1
2
3
4
5
#include <iostream>

int main(){
  return 0;
}
1
2
clang++ -o runfile main.cpp -lm
./runfile

If nothing is printed, it means the issue has been successfully resolved. Nice.

1
This post is licensed under CC BY 4.0 by the author.