Reputation: 857
I just updated my MacBook Pro to macOS Catalina 10.15, and tried to compile and run a C++ command line program, but I had a problem which didn’t exist on previous versions;
This is simply the code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!\n";
return 0;
}
The code compiles and outputs the expected, but still the Xcode says:
fatal error: 'iostream' file not found
I tried changing the Build Settings/C++ Standard Library to libstdc++, but a warning says:
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead
And the same iostream error still exists.
Upvotes: 15
Views: 44947
Reputation: 41
I also experienced same after upgrading to Sequoia 15.3.1 of macOS and found clang++ stopped working. It started throwing error "fatal error: 'iostream' file not found". As as fix, I had to remove CommandLineTool:
sudo rm -rf /Library/Developer/CommandLineTools/
and reinstall xcode by:
xcode-select --install
This command threw an installation popup and took a few minutes to complete the installation. After successful installation, I checked CommondLineTool by:
ls -l /Library/Developer/CommandLineTools
, that showed:
drwxr-xr-x 5 root wheel 160 Nov 17 2023 Library
drwxr-xr-x 10 root wheel 320 Feb 24 00:33 SDKs
drwxr-xr-x 7 root wheel 224 Jan 15 2024 usr
It solved the defect and clang++ started working for me.
Upvotes: 1
Reputation: 381
If this is an option, note that CMake will handle this correctly. I got "could not find <iostream>
" with clang++ on the command line, but this simple project built fine:
cmake_minimum_required(VERSION 3.30)
project(cpptest)
add_executable(
cpptest
src/test.cpp
)
#include <iostream>
int main()
{
std::cout << "hello" << std::endl;
return 0;
}
Upvotes: 0
Reputation: 11
I tried a fresh Catalina install with Xcode. I copied and pasted your code into "test.cpp" and then ran:
clang++ test.cpp
in the same directory as the "test.cpp" file from Terminal. The result was an "a.out" file which when run:
./a.out
output the required "Hello, World!" result. Hopefully that is of some use (as a point of reference).
Upvotes: 0
Reputation: 2678
I'm compiling from the command line, and none of the answers listed here (or elsewhere) worked for me.
What does seem to work (so far) is to add the following to .profile
or whatever script your terminal uses to start up: (zsh, csh, bash, etc.)
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
export CPLUS_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include
You will probably have to change MacOSX10.15.sdk
whenever you upgrade your operating system.
C_INCLUDE_PATH
and CPLUS_INCLUDE_PATH
are options for the clang toolchain rather than MacOS environment, so hopefully this solution will work long-term, unlike xcode-select --install
(which won't fix the include directories on an upgrade) or ln -s ... /usr/include
(which is now forbidden by System Integrity Protection).
Upvotes: 4
Reputation: 31
I had the same problem and used the following youtube video to fix it. https://www.youtube.com/watch?v=hrPm7tWC-BI&feature=youtu.be
or you can follow this path. Make sure to include the quotation marks
Project - Build Settings - Search Paths - Headers Search Paths, and add the following path: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/"
Upvotes: 2
Reputation: 4038
libstdc++
is not OK for Xcode Build & Compile time,
libstdc++
is OK for iPhone Run Time
From answer recommended by @Alan Birtles
libstdc++
Support was removed from the iOS 12.0 Simulator runtime, but it remains in the iOS 12.0 (device) runtime for binary compatibility with shipping apps.
I encountered this when declaration in .hpp
file.
#include <iostream>
#include <string>
OK with
#ifdef __cplusplus
#include <iostream>
#include <string>
// usage code
#endif
Upvotes: 0