I have C++ file header (interface.hpp) in my Objective-C project.
When I use it I get a compiler error on the line: #include <map>
map file not found
and it's the same for #include <string>
string file not found
It seems to me that, despite the file extension hpp, Xcode is compiling it as C header, rather than C++ header.
Any suggestion?
The extension of the header is irrelevant. What is important is the extension of the file that includes that header. If you want to include a C++ header in Objective-C code, you need to change the extension of the Objective-C file to .mm. This indicates that the file is an Objective-C++ source file (a different language, in fact, so be careful that you know what you're doing). Headers that are included in a .mm file will be parsed as Objective-C++, and so then your C++ constructs should work.
You have to change the file extension to .mm in order to use it.
If changing the extension to .mm is not working try adding -l"c++" to Other Linker Flags in build settings.
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 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 have a objective c/c++ project under iOS, moving it from OS/X and I get a 'file not found' error on
#include <string>
It's a clean project, and I've just added the files from the old project. Are the STL includes set up in XCode? A find produces a number of possibilities e.g.
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/usr/include/c++/4.2.1/debug/
but adding this to the search path just threw up more errors. Any suggestions?
(apart from don't use string - it's in house code I'm porting)
xcode 4.2.1, ios5.0 running on OS/X 10.7.3 and it's in a .cpp file, the code works fine on OS/X
Are you really sure <string> is included only from a .cpp file?
I just tested on a fresh project, by adding a .cpp file and including <string>, and it works, just as expected (same Xcode version, same SDK version).
But if I include <string> in a .m file, then of course I got a «file not found» compiler error.
So double-check this, as the error may come from here.
Do you include <string> from a .cpp file only, or from a .h file, intended to be used by a .cpp implementation?
Then maybe it's also included by a .m file, hence the error.
Also check your precompiled headers, if any, to see if you include some C++ stuff there...
Also remember, in that later case, that you can rely on the __cplusplus macro, when needed.
If you include a header in an ObjC file and it includes <string> then you hit errors like this. For all .m files XCode uses a C compiler (clang or llvm-gcc). For all .mm files it will use (clang++ or llvm-g++).
I suggest going through and renaming all your .m files to .mm. Including main.m to main.mm.
For me the reason was
MyHeader.h (which includes #include ) target was public. Changed it to project and it compiled.
For cocoa pod:
s.public_header_files = 'MyProject/Classes/**/*.h'
s.project_header_files = 'MyProject/Classes/MyHeader.h'
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.
I have a C++ class I would like to use in an iPhone/iPad project.
I created this file in different ways (like with "New File" => C++) and the error is always the same.
When I compile the project without having any #import (of the .h C++ class), it's ok.
But as soon as I #import the header file in one of my header objective-c file, I get error like :
error: vector: No such file or directory
or
error: expected '=', ',', ';', 'asm' or 'attribute' before ':' token"
I tried setting different values to the file type (of C++ class) in File Info, renaming my objc class in .mm etc, but it doesn't seem to work.
So I must have missed something about importing the .h c++ class in the objc header file, but what :p ^^
SOLUTION thanks to Vlad
1°) To include the header c++ file :
#ifdef __cplusplus
#include "Triangulate.h"
#endif
2°) Renaming the objc file in .mm AND in his File Info (right clic) setting file type as sourcecode.cpp.objcpp
Thanks for helping !
Vincent
Note: Xcode requires that file names
have a “.mm” extension for the
Objective-C++ extensions to be enabled
by the compiler.
Trying to use C++ in Objective-C code residing in a file with .m extension is the most probable cause of the problem because compiler just does not recognize C++ constructs according to the error message. Renaming your .m file to .mm should help.
For more details, see Using C++ with Objective-C.
Assuming you want to use an Objective-C class in an Objective-C++ source file, there's no problem at all. The only restriction is that your .h file must be Objective-C clean. This means that you can't use any C++-isms in it, or that if you do you must wrap them all in #ifdef __cplusplus. The header will be compiled in ObjC mode when it's #included by a plain Objective-C file, so it has to eliminate any C++isms for that situation (1). So your Objective-C header file should include C++ header like this:
#ifdef __cplusplus
# include MyCPPHeader.h
#endif