4

I'm trying to compile StegHide from source and I got an error message, saying that iostream can't be found.

I wrote the following program and compiled it with -std set to all standards that my system has:

#include <iostream> int main() {return 0;} 

This gives this error message:

test.cpp:2:10: fatal error: 'iostream' file not found 1 | #include <iostream> 

The /Library/Developer/CommandLineTools/usr/include/c++/v1 dir only contains the __cxx_version file. Is there anything I need to (re)install?

Edit: iostream was in /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/, but how can I make g++/clang search this path by default?

2 Answers 2

14

tl;dr: Rename or if you're confident enough, delete /Library/Developer/CommandLineTools/usr/include/c++, then clang++ will automatically search headers under /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 and find your <iostream> header. That directory is very likely an artifact of OS upgrade and by deleting it clang++ will realize that it should search in the header paths of new SDKs. The same fix should apply to almost any "standard library header not found" problem as long as the header clang++ couldn't find resides under /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 but clang++ only searches /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1.

Note: Another symptom of this problem is that clangd also can't find any standard library header. I encountered this problem when I was using the VS Code Clangd extension and clangd simply refused to recognize any stdlib header without setting up a compile_commands.json or some magical incantations of --query-driver, both being solutions widely suggested on the web. I didn't give it much thought back then because I believed it was only clangd, not the compilers clang and clang++, that couldn't find the headers. Maybe clangd was just more simplistic in its header search logic. Now after renaming or deleting the directory, clangd should be able to discover stdlib headers out of the box without any configuration.

The rest of this answer shows how I arrived at this solution.


I'm on Sequoia 15.4.1 and I encountered almost the same problem as OP. First clang++ couldn't find <cstdint>. Then like OP I constructed a C++ snippet that included only <iostream> and clang++ couldn't find <iostream> either. I used -v to check the header search path:

> clang++ src/playground.cpp -v Apple clang version 17.0.0 (clang-1700.0.13.3) Target: arm64-apple-darwin24.4.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple arm64-apple-macosx15.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name playground.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fobjc-msgsend-selector-stubs -target-sdk-version=15.4 -fvisibility-inlines-hidden-static-local-var -fdefine-target-os-macros -fno-assume-unique-vtables -fno-modulemap-allow-subdirectory-search -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.5a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -debugger-tuning=lldb -fdebug-compilation-dir=/Users/xushangning/Downloads/llama2.c -target-linker-version 1167.4.1 -v -fcoverage-compilation-dir=/Users/xushangning/Downloads/llama2.c -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/17 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/usr/lib/clang/17/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Library/Developer/CommandLineTools/usr/include -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -clang-vendor-feature=+disableAtImportPrivateFrameworkInImplementationError -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/c9/hs7776l50t32_6dz7csp4h780000gn/T/playground-8243a8.o -x c++ src/playground.cpp clang -cc1 version 17.0.0 (clang-1700.0.13.3) default target arm64-apple-darwin24.4.0 ignoring nonexistent directory "/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/SubFrameworks" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks" #include "..." search starts here: #include <...> search starts here: /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1 /Library/Developer/CommandLineTools/usr/lib/clang/17/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include /Library/Developer/CommandLineTools/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory) End of search list. src/playground.cpp:1:10: fatal error: 'iostream' file not found 1 | #include <iostream> | ^~~~~~~~~~ 1 error generated. 

and searched for the <iostream> header under the SDK root:

> find /Library/Developer/CommandLineTools -name iostream /Library/Developer/CommandLineTools/SDKs/MacOSX15.4.sdk/usr/include/c++/v1/iostream /Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/usr/include/c++/v1/iostream /Library/Developer/CommandLineTools/SDKs/MacOSX14.5.sdk/usr/include/c++/v1/iostream 

Just like OP, <iostream> resides under /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/iostream (MacOSX.sdk is symbolic linked to MacOSX15.4.sdk) but clang++ only bothered to search /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1. The latter directory looked really barren to me with few headers left:

> ls -l /Library/Developer/CommandLineTools/usr/include/c++/v1 total 56 drwxr-xr-x 3 root wheel 96 Oct 19 2022 __bit/ drwxr-xr-x 5 root wheel 160 Oct 19 2022 __charconv/ drwxr-xr-x 9 root wheel 288 Oct 19 2022 __compare/ drwxr-xr-x 24 root wheel 768 Oct 19 2022 __concepts/ -rw-r--r-- 1 root wheel 14688 Dec 22 2020 __cxx_version -rw-r--r-- 1 root wheel 3142 Oct 19 2022 __cxxabi_config.h drwxr-xr-x 14 root wheel 448 Sep 25 2023 __format/ drwxr-xr-x 4 root wheel 128 Sep 25 2023 __functional/ drwxr-xr-x 3 root wheel 96 Sep 25 2023 __iterator/ drwxr-xr-x 9 root wheel 288 Sep 25 2023 __ranges/ drwxr-xr-x 3 root wheel 96 Sep 25 2023 __utility/ -rw-r--r-- 1 root wheel 7215 Oct 19 2022 cxxabi.h 

Then the last modified date of these files stood out to me. They looked really old! The same for the parent directory "c++":

> ls -l /Library/Developer/CommandLineTools/usr/include/ total 24 drwxr-xr-x 3 root wheel 96 Jul 7 2023 c++/ -rw-r--r-- 1 root wheel 6911 Mar 13 11:12 FlexLexer.h -rw-r--r-- 1 root wheel 575 Mar 11 10:22 module.modulemap drwxr-xr-x 4 root wheel 128 Apr 4 13:17 swift/ 

Then it occurred to me that could the "c++" directory be an artifact of OS upgrade and was no longer needed because in newer versions of SDK those headers were moved to another directory. I tried renaming the "c++" directory, recompiled my program and voila, it worked!

> sudo mv /Library/Developer/CommandLineTools/usr/include/c++{,-backup} Password: > clang++ src/playground.cpp -v Apple clang version 17.0.0 (clang-1700.0.13.3) Target: arm64-apple-darwin24.4.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin ignoring nonexistent directory "/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1" "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple arm64-apple-macosx15.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -dumpdir a- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name playground.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-strict-return -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fobjc-msgsend-selector-stubs -target-sdk-version=15.4 -fvisibility-inlines-hidden-static-local-var -fdefine-target-os-macros -fno-assume-unique-vtables -fno-modulemap-allow-subdirectory-search -target-cpu apple-m1 -target-feature +zcm -target-feature +zcz -target-feature +v8.5a -target-feature +aes -target-feature +altnzcv -target-feature +ccdp -target-feature +complxnum -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +fptoint -target-feature +fullfp16 -target-feature +jsconv -target-feature +lse -target-feature +neon -target-feature +pauth -target-feature +perfmon -target-feature +predres -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sb -target-feature +sha2 -target-feature +sha3 -target-feature +specrestrict -target-feature +ssbs -target-abi darwinpcs -debugger-tuning=lldb -fdebug-compilation-dir=/Users/xushangning/Downloads/llama2.c -target-linker-version 1167.4.1 -v -fcoverage-compilation-dir=/Users/xushangning/Downloads/llama2.c -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/17 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/usr/lib/clang/17/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Library/Developer/CommandLineTools/usr/include -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -fdeprecated-macro -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -clang-vendor-feature=+disableAtImportPrivateFrameworkInImplementationError -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/c9/hs7776l50t32_6dz7csp4h780000gn/T/playground-b5d115.o -x c++ src/playground.cpp clang -cc1 version 17.0.0 (clang-1700.0.13.3) default target arm64-apple-darwin24.4.0 ignoring nonexistent directory "/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/SubFrameworks" ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks" #include "..." search starts here: #include <...> search starts here: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 /Library/Developer/CommandLineTools/usr/lib/clang/17/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include /Library/Developer/CommandLineTools/usr/include /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory) End of search list. "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch arm64 -platform_version macos 15.0.0 15.4 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -mllvm -enable-linkonceodr-outlining -o a.out -L/usr/local/lib /var/folders/c9/hs7776l50t32_6dz7csp4h780000gn/T/playground-b5d115.o -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/17/lib/darwin/libclang_rt.osx.a 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use xcode-select to activate one of the toolchains. If the libraries are not installed then you need to run xcode-select --install command.

More details in Xcode stops working after set "xcode-select -switch"

2 Comments

I've reinstalled using xcode-select, but the cpp libraries aren't anywhere in the active developer dir, I looked through every subdir too. Do you know where the libraries could be installed?
You are probably missing the cli-tools. That might be due to recent changes in xcode release. See forums.developer.apple.com/forums/thread/732670

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.