I have a question and I could not find a solution, so I ask here :)
I want to create an Foundation-based terminal application/script using Xcode-->Mac-->New-->Command Line Tool-->Foundation
This works and all, but then I want to ADD header.h files to my project.
One of this header.h files does the following:
#include <iostream>
This fails with the error: iostream file not found.
For a test I make a new c++ based terminla script and it does exactly the same:
#include <iostream>
But for some unknown reason it does not fail with an error.
Can anyone tell me, why the c++ script works to include and the objective c not?
objective c: .h file
c++: .cpp file
I renamed the .h to .cpp but then it does not find the NSString and such things.. any solution to use iostream and objective c?
I really need that, thanks
If you want to use both Objective-C and C++ (called Objective-C++) from within the same source module, then use the .mm file extension.
Related
I am have a third-party static library, which includes a header file written in C++. I have linked the library, but get compile errors, because the header file uses #include gives a file not found error. It is a library, so I don't think I should be editing that file at all, so is there so flag or property to change in the project settings to compile that header file?
The error is happening in: ViewRightWebiOS.h
The specific line the error is on the third line:
#include <string>
C++ headers can only be imported not a file compiled with C++.
The easy way to do this is to rename the file containing the #include to have an extension of .mm rather than the Objective C normal extension of .m Xcode will compile this file using the Objective-C++ dialect. In this mode C++ and Objective C constructs are understood in the same file. However you can use Objective C features on a C++ object and vice versa, interoperation is still using commonly understood C constructs
If you can, you should include the file in your implementation file (.m) file, but rename it to .mm. This way you'll actually be using objective-c++, but it should be ok.
i am trying to declare function returning string in header file because i will call this from objective-c. basically this would work, isn't it?
std::string myFunction();
but it throws error message says "Expected ';' after top level declarator", searched a lot, everyones suggest put #include in header files, i tried that as well however it is not working, this time it throws another error message "'string' file not found".
have another function returns double and have no problem with it.
double doSomething(double a);
-
#include <string>
does not work it is throwing error message saying "'string' file not found" . have tried to create new project just in case mine could be damaged but it is not working should i put something in search paths etc?
at last i made it.
The solution: have changed "Compile Source As" settings to Objective-C++ under Build Settings / Apple LLVM Compiler 4.2 and it worked like a charm.
At the very least, you have to include the C++ standard library header string, where class std::string is declared (in actual fact, it is a typedef to a class template, but that is another matter).
#include <string>
std::string myFunction();
You should also make sure to use include guards in your own headers.
You missed the header file:
#include <string>
You have to remember that ObjC files are built upon C not C++, if you want to use a C++ file in Objective-C you need to change the extension of the ObjC file from .m to .mm to make it an ObjC++ file, or else it will be like trying to include C++ headers in C files.
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.
I wrote a simple C++ program to parse an XML string, called sample.cpp. The program includes a header file, tinyxml.h. When I compiled the program on a unix machine I got the error:
tinyxml.h: No such file or directory
How can we add new header files to the standard library and make them compile? Can anyone please help to get it done? Thank you
You need to tell your compiler where to find the header file. This depends on the compiler, but is typically done by specifying -I<directory> on the command line.
If the header file is in the same directory as the cpp file, you need to include it in quotes, instead of angle brackets, ie.
#include "tinyxml.h"
Instead of
#include <tinyxml.h>
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.