xcode 4.6 iostream file not found error? - c++

I am facing a problem regarding iostream file not found in header file.I just added a c++ file in my project a header file also included by default with some macro definition and including iostream file as
#ifndef __ObjectiveCPlus__File__
#define __ObjectiveCPlus__File__
#include <iostream>
#endif
but at this line I am getting error at include line as
I google it a lot and found various types of answer regarding this.But no one is able to correct my errors.
Please help
Thanks!

You don't need <iostream> in your header file, put it in your .cpp file. You're not referring to anything in the iostream library in your header file, using this library is more of an implementation detail.
Why?
I believe UIAppDelegate imports UIViewController.h, that includes MathUtils.h. Because UIAppDelegate's implementation is in a .m file, it's being compiled for Objective-C, and this chain of includes (which is all based on the header files) is including something that is C++. As such, the Objective-C portion is unable to find <iostream>, as that library does not exist in pure Obj-C.
Putting it in your .cpp file limits it to one compilation unit, the MathUtils unit. Having it in your header file includes it in all compilation units that have a dependancy on whatever is using it, which may not be Objective C++.
Alternative Solution
You could have your whole project as Objective C++ (in this case, by changing UIAppDelegate.m to UIAppDelegate.mm), which means C++ can be used throughout. I'm not a fan of this method, and it could mask bad coding practices.

I got the solution from another post:
Renaming your implementation file with .mm extension instead of .m will solve the issue.

Related

Removing .h extension in user defined c++ header file

Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
after following up the comments and answers:
using codeblocks ide
i have created a "add" of type File and tried it including in my source file and it worked. attaching the snapshot below.
the aim of my question is to ask if userdefined header files can also omit .h extensions and how?
i am really trying to explore this fact and don't have a good understanding of how compilers stores standard header files.
A easy to understood conclusion is really appreciated
Thankyou.
Can we remove .h extensions while we define our own header file in c++?
Sure, as long as that matches the filename of the file. As far as the language is concerned, the name of the file is largely irrelevant.
However, .h or similar such as .hpp is conventional, and helps the reader of the source to understand what the file is used for. This is an important consideration.
Another consideration is that some tools use the filename as a heuristic to determine the purpose of the file. For example, your IDE might not assume that the file contains C++ code, and thus not enable C++ features such as source analysis unless you follow a common naming convention.
I have created a header file and named it add.h and tried it including in source file using #include "add" but it didn't work.i know i am missing some important concepts here.
What you're missing is that the include directive must match the name of the file. If you include "add", then you must name the file add, not add.h. If you name a file add.h, then you must include "add.h", not "add".
Can we remove .h extensions while we define our own header file in c++? like in case of standard header files in c++.
You've misunderstood how the files in the stardard library are named. The header file iostream is actually named iostream and not iostream.hpp or iostream.h (unless you use a very old compiler).
I have created a header file and named it add.h and tried including it using #include "add" but it didn't work.
The reason that doesn't work is because the pre-compiler tries to read the file add and you've named the file add.h.

Xcode and C++ Headers

I have a question about using C++ header files in Objective-C++ modules in Xcode. Specifically, why can I #include them in source files but not header files?
Here is a specific example.
I'm using Xcode 7.2.1 and have two projects. The first is a C++ framework I package into "myFramework.framework". It exposes "myFramework.h", which in turn pulls in "myLib.h". At the top of "myLib.h" is an "#include <string>".
The second project is an Objective-C iOS app which consumes the above framework. In this project, "myViewController.mm" (Objective-C++ source) has "#import "myFramework/myFramework.h" at the top and makes reference to things defined in that header file.
At this point all is well and good. It builds and runs with no issues.
When I move the "#import myFramework/myFramework.h" line to "myViewController.h", the compile fails because it cannot locate the "" header dependency.
It doesn't matter if I change the file type for "myViewController.h" to Objective-C++ header from plain old "C Header". Either way, Xcode's header search paths don't look for standard C++ headers.
So my main question is why does it behave this way? Why is a #include/#import treated differently just because it's in a header file?
My second question is if there's some way to make Xcode treat the #include/#import the same when it's in the header file instead of the source file?
Thanks much!
Are you sure that you get the error while compiling the myViewController.mm file?
Check if myViewController.h is imported into some other, non ObjC++ file (and that that one is the file that fails to compile).
I suspect the issue with including C++ headers inside other headers is that an Objective-C source file gets to see the C++ header file, which upsets it.
If you have mixed C++/Objective-C++/Objective-C then you are probably better off only exposing a pure Objective-C interface to other modules in the project and include any C++ header files in the Objective-C++ source files only.
Alternatively make everything Objective-C++ and then you don't need to worry about it at all.
Hopefully this answers your second question as well.

How to handle non-ARC code in header file?

My project structure
MainProject (ARC)
SubProject(Non-ARC)
Boost library(i.e. Popular C++ library here is a link http://www.boost.org/)
Problem :
One of header file(intrusive_ptr.h) of Boost library has a inline function with "retain" statement(That file is edited by someone and it is working fine in "SubProject(Non-ARC)"). That header file is public, many files of subporjects are imported in "MainProject" and those file has a reference of this header file. So, indirectly that file comes in MainProject which is an ARC based. That's why compiler refuse to compile.
What I know or tried:
I know how to set the non ARC flag but that we can set only for .m file(compilable file only) not on .h file. if somebody could help me or suggest me any out of box solution.
For the persons who are interested in seeing the "intrusive_ptr.h" can find here intrusive_ptr.h. This file is a part of boost library so, suggestion should consider this library as well.
Any help or directions will be appreciated.
You can use the preprocessor to alter your header file using the technique from this answer
#if __has_feature(objc_arc)
//ARC-specific things
#else
//Non-ARC specific things
#endif
If you need Boost facility in some classes you can hide that using Objective-C++. Helper links:
http://philjordan.eu/article/strategies-for-using-c++-in-objective-c-projects
http://support.apple.com/kb/TA45902?viewlocale=en_US

LLVM refuses to compile C++ source, weird errors

I've made a struct which does cached file manipulation for my application. I built and tested to in a separate project before putting it into my current one.
Ever since I've moved it over, Xcode refuses to build it. Except when I don't include the file from any Objective-C based header file.
I get one error when I try to include iostream:
And more when I comment it out:
Its file extension is .mm, however I have tried it with .cpp and .hpp, but all of them refuse to build unless I don't #include it from the Objective-C header file.
I've also tried #import from iostream and the file itself in the Objective-C header file.
Any clues as to why this is happening?
As a matter of principle, you cannot include a C++ header file from an objective-C source file.
After all, #including (or #importing) a file only means that the preprocessor replaces the #include directive by the contents of the #included file, before passing the result on to the "actual" compiler. The file extension of the header file is a matter of convention, only, it has no actual meaning.
The error messages your are seeing are clearly the result of the file being compiled as [Objective-]C rather than [Objective-]C++.
Solution: All the source files that include your C++ header file have to be either C++ (.cpp or .cc or a few other extensions) or Objective-C++ (.mm). All source files that include a header file that includes your C++ header file, also have to be C++ or Objective-C++.
EDIT: I just saw that you are defining non-inline, non-template functions in your C++ file that you want to include. This is an unrelated problem, but it will lead to "multiple definition" errors sooner or later. Those function definitions belong in a .cpp, which shouldn't get #included anywhere, only the struct/class definition belongs in a header.
Take a look here and here. You need to tell the compiler to include libstdc++. When mixing Objective-C and C++ all you're files need to have the ".mm" extension, as stated in the second link.
I suspect the error is occurring when you compile a .m or .c file that includes the same header.

Header imports and includes failing in XCode, cmath, objc-class.h, etc

I keep having issues with including basic headers such as cmath. It is most prevalent when using example projects.
Example:
#include <cmath>
for instance gets a file not found, even though I can verify that the SDK I'm using has it:
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/usr/include/c++/4.2.1/tr1/cmath
I can sometimes work around the issue by importing directly to the file, but this doesn't always work.
#include </usr/include/c++/4.2.1/cmath>
What is the extension of your sourcecode file? .m or .mm? If it's .m, the compiler will assume you have a regular objective-C file, whereas .mm would imply an objective-C++ file. If its not a .mm file, the compiler may not be looking for C++ includes.