Getting error on Xcode 11 when programming on C++

Please need the urgent support, i am student and programming C++ using xcode 11

getting following error


#include <iostream> . 'iostream' file not found





Posted on Oct 2, 2019 11:14 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 7, 2019 8:08 AM

Xcode is collosal overhead to just to compile a command-line C++ program. Once you have Xcode 11 installed, and have agreed to the extra download dialog, install the Xcode command-line tools:


xcode-select --install


Now, you will have access to clang++ on the command-line and by default, it knows to load the libC++ library if you are not doing any of the later standards (c++17).


To compile the following:

// memoized version of Fibonacci sequence
// Prior results are stored/searched to avoid expensive recalculations
// More: http://xlinux.nist.gov/dads/HTML/memoize.html
// see man clang
// clang++ -Wall -O2 -o fib_memo fib_memo.C -std=c++2a

#include <iostream>
#include <locale>
#include <map>

using namespace std;

// key/value pair associative array
std::map<u_long, u_long> alreadyknown;

u_long n=0, newvalue=0;

u_long fib(u_long n) {
	// true if we reach end of array without find success
	if (alreadyknown.find(n) == alreadyknown.end()) {
		newvalue = fib(n-1) + fib(n-2);
		alreadyknown[n] = newvalue;
	}
	return alreadyknown[n];
}

int main(int argc, char* argv[]) {

	// initialize memoization key/value pair
	alreadyknown[0] = 0;
	alreadyknown[1] = 1;

	cout<<"Enter the number of terms: ";
    cin>>n;
    std::locale mylocale("");
    std::cout.imbue(mylocale);	// use locale number formatting style
    cout<<fib(n) << endl;
    return 0;
}


Now, using the default C++ library and specifying the iso C++ 2017 spec with amendments and GNU extensions:

clang++ -Wall -O2 -o fib_memo fib_memo.C -std=c++2a


and:

fib_memo
Enter number of terms: 50
12,586,269,025
30 replies
Question marked as Top-ranking reply

Oct 7, 2019 8:08 AM in response to wjjo

Xcode is collosal overhead to just to compile a command-line C++ program. Once you have Xcode 11 installed, and have agreed to the extra download dialog, install the Xcode command-line tools:


xcode-select --install


Now, you will have access to clang++ on the command-line and by default, it knows to load the libC++ library if you are not doing any of the later standards (c++17).


To compile the following:

// memoized version of Fibonacci sequence
// Prior results are stored/searched to avoid expensive recalculations
// More: http://xlinux.nist.gov/dads/HTML/memoize.html
// see man clang
// clang++ -Wall -O2 -o fib_memo fib_memo.C -std=c++2a

#include <iostream>
#include <locale>
#include <map>

using namespace std;

// key/value pair associative array
std::map<u_long, u_long> alreadyknown;

u_long n=0, newvalue=0;

u_long fib(u_long n) {
	// true if we reach end of array without find success
	if (alreadyknown.find(n) == alreadyknown.end()) {
		newvalue = fib(n-1) + fib(n-2);
		alreadyknown[n] = newvalue;
	}
	return alreadyknown[n];
}

int main(int argc, char* argv[]) {

	// initialize memoization key/value pair
	alreadyknown[0] = 0;
	alreadyknown[1] = 1;

	cout<<"Enter the number of terms: ";
    cin>>n;
    std::locale mylocale("");
    std::cout.imbue(mylocale);	// use locale number formatting style
    cout<<fib(n) << endl;
    return 0;
}


Now, using the default C++ library and specifying the iso C++ 2017 spec with amendments and GNU extensions:

clang++ -Wall -O2 -o fib_memo fib_memo.C -std=c++2a


and:

fib_memo
Enter number of terms: 50
12,586,269,025

Oct 17, 2019 9:00 PM in response to razvan66

Finally... I too have fixed this issue. I’m sure it’s different for everyone but my issue was that I had duplicate header files installed on my system in two different locations.


I installed the command line tools with xcode select —install. and also had the package with the same header files that Xcode comes preinstalled with.


I confirmed this yes when I temporarily switched to vs code for my project. When I selected to go to the definition of the header file I was including vs code showed two of the same files in different locations.



Anyway...I deleted all the header files in library/developer/CommandLineTools/use/include/c++/v1


Xcode comes packaged with those exact files in /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1



Closed Xcode and rebooted and came back to to no error. Also rebooted several more times just in case the issue returned but it hasn’t.


hope this helps anyone still having trouble.



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Getting error on Xcode 11 when programming on C++

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.