C++ in iOS Development issues - c++

So I have a .h file and when I include iostream xcode says that header file doesnt exist. But what is making me mad is that whenever I go though the new file process choosing c++ class the default .h file comes with one line of code, which includes iostream.h
so when I import that to my Objective-C code it fails to compile.

If you put #include <iostream> in a .h file, then you must be sure to only include that .h file in C++ files (.cpp or .cc) or Objective-C++ files (.mm). You're getting a compiler error because you're including your .h file in a C (.c) or Objective-C (.m) file.

Related

Xcode, Objective-C Project: not compiling C++ file

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.

Precompile header in CPP project and C generated from wsdl file

I'm creating simple web service client in CPP. I have generated source files from WSDL with help of svcutil ans wsutil utilites. At the end I got web service interface header and C file.
When I include C file into my console application that uses precompiled header I have message:
Error 3 error C1853: 'Debug\TestLib3.pch' precompiled header file is from a previous version of the compiler, or the precompiled header is C++ and you are using it from C (or vice versa)
How to solve this problem?
The C compiler cannot use a .pch file that was generated by the C++ compiler. Two basic options:
Rename the .c file to .cpp, likely to work fine on an auto-generated source file.
Right-click the .c file in the Solution Explorer window, Properties, C/C++, Precompiled headers, "Precompiled Header" option. Change it to "Not using". You may also need to modify C++ source files that #includes the .h file, it is liable to require extern "C" {} around the #include directive so the C++ compiler knows that the .h file contains C declarations. You'll know this is needed when you get linker errors that show the mangled name of the function.

How to import C++ header file, from static library, into Obj-C?

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.

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.

Problem when #import C++ Header File in iPhone/iPad Project

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