macos cmake can not find std header files

error: ‘iostream’ file not found [clang-diagnostic-error]

Solution

Add this to your CMakeLists.txt file.

if(APPLE)
    include_directories(
        SYSTEM
        /opt/homebrew/opt/llvm/include/c++/v1
        /Libaray/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
    )
endif()

Why it works

The snippet above adds clang header files and MacOS system header files to your CMakeLists.txt.

It didn’t work

If the snippet above didn’t work, make sure you have Apple’s dev tools installed.

xcode-select --install

If it still doesn’tt work…

If it still doesn’t work make sure you tried a clean build.

rm -rf build
cmake -B build

If it still doesn’t work then… >_<

Tags: