#include <string> file not found in iOS in C++ file - c++

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'

Related

Added C src files to my C++ project, but they cannot find "stdafx.h"

I have a simple VS C++ project that consists of a few headers and one source file that houses main(). I am using VS's default method of using precompiled headers with stdafx.h. So my one main.cpp has:
#include "stdafx.h"
at the top. For this configuration, everything compiles and runs fine.
However, I needed to add some C src and header files to my project that has data and function calls that I need to use. So I added these (3 .c files and 1 .h file) using the usual method: Add > Existing Item > choose file. But when I tried to compile I get
Error C101: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
So I went back to my new .c src files and added #include "stdafx.h" to the top, but VS is throwing
E1696 Error: cannot open source file "stdafx.h"
Does VS's precompiler work with C files in a C++ project or do I need to reconfigure it somehow?
EDIT: The reason I chose to go with the default method of using precompiled headers is because although my code isn't very large, I am using large header libraries like Boost and Eigen.

How to use google protobuf in a project with precompiled headers

I have a solution which contains several projects. My projects (but not all of them) use precompiled headers. I decided to use protobuf and I've met a problem. After generetaing *.pb.h from *.proto by protoc.exe I'm trying to include the header and get the error - precompiled header wasn't included into *.pb.h.
How I can solve this problem? I have an idea (but I don't like it at all) - after protoc generates *.pb.h I can run some script, which'll include my precompiled header into the *.pb.h. But I don't like it because some projects may not use PCH, and PCH file name can be different.
I understand that I can just remove PCH from my projects, but I don't like that idea too.
Dont add the generated myproto.pb.cc to your project. Instead, create a myproto.cpp with
#include "pch.h"
#include "myproto.pb.cc"
I resolved my problem by creating a static library called proto-objects (without PCH) and including all my *pb.h(cpp) files there. After it I link that library to every project where I need my protobuf objects. Profit!
You can disable the pre-compiled header option on a file-by-file basis.
Given that the pch option is intended to speed up compilation, you can turn it off for the whole project, and no further changes should be necessary.
The choice of name of the header file, and the pch file are also selectable per file in the project
Update
The idea behind Microsoft's Pre-compilation PCH system is to
Speed up compilation
Make it easy to use
The header file system in C/C++ is problematic, as it is really a textual replacement.
That means that
#include "localdefs.h"
#include <windows.h>
#include "project.h"
#include "support.h"
Is in no way similar to
#include <windows.h>
#include "project.h"
#include "support.h"
That is because localdefs.h can redefine the behavior of all of the other includes.
Further to this the costs of walking through the complexities of the windows.h header files, is time consuming.
The PCH system tries to solve this by the observation that most projects have a fixed set of include files which are included by most/all of the CPP files.
Defining this set in stdafx.h allows the textual result of that parsing to be pasted in the cpp file and save a lot of work.
If most of the includes in the project are different, then there is no need to use it.
So if you are including the same qt header files in lots of places - add them to a pre-compiled header file. The more of the common includes added to this file, the better the compile speed improvements will be.
Any non-standard cpp file can be excluded by being specifically disabled - examples are "generated files". Where the template generator does not understand the MSVC system.
If all the files are different, then only limited performance benefit will be gained - as each compile would probably also include a pch recompile.

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.

Clang cannot find include directory?

I just moved all my header files to be in the include directory of my project, rather than having both .cpp and .h in the src directory. I'm using YouCompleteMe in vim and now when I open vim, it tells me that my header files cannot be found on the side to alert me of compiler errors. Specifically, it gives me errors on the #include "my_header.h" of my_header.h file not found and errors on each method saying use of undeclared identifier. However, the code compiles just fine. And the auto-completion works fine. It seems that clang cannot find where my header files are located now since I moved them ../include. If I change my #include line to be #include "../include/my_header", all of the errors to away. But it seems better to only have #include "my_header.h" in all of my .cpp files or #include <my_header.h>.
Can anyone help me solve this issue?

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.