I am trying to figure out why XCode cannot resolve a C++ file I imported.
I have changed the file extension to .mm
I have added the .h file contents to the top of my file
I have command line tools installed
The code complete detects the algorithm library:
But when I try compile I get this:
Ok I found the issue.
As it turns out you need to not only use the .mm extension to invoke the objective-c++ compiler, but also anything that REFERENCES the .mm too. So I had a view controller that referenced the c++ header.
Related
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.
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.
I want to integrate Box2d in my OS X application, so I've used the following Podfile to grab it:
pod 'box2d'
And the version of box2d is 2.3.0. In the xcworkspace I got from pod install, I've created a bridge header to interoperate with the C++ APIs (according to doc, developer cannot import C++ projects directly from Swift lang, you should create a ObjC bridge).
And when I hit build button, I got a compiler error:
<unknown>:0: error: /path/to/project/Pods/Headers/Box2D/Common/b2Settings.h:22: 'cassert' file not found
So I wonder how can I fix this?
Finally I figured it out by myself.
I've already created the bridging file as mentioned in apple's doc, but the content I was putting there was:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <Box2d/Box2d.h>
This is the root for the compiler error. So, I have to create a new Objective-C class, rename the .m file to .mm, and put the line of import in the .mm file. NOTE: putting the import line in .h file does not solve the compiler error.
And that's it, now it compiles happily.
I've been using a C++ library without problems on projects built with Xcode 3, but I'm now getting build problems on projects built with Xcode 4.
Drop the library into the Xcode 4 project and it builds fine, but as soon as I #include it, I get a "Lexical or Preprocessor Issue" error, more specifically " 'string' file not found, on line 4 of its main header file.
On closer inspection, the error specifies that 'string' file not found in ~/my project's directory/include/mainheader.h
I've tried the solutions listed here, but none worked.
So it thinks that header file is in my project directory, but it's obviously a C/C++ header… How can I tell Xcode to look for these C/C++ headers?
The problematic #include was at the top of my ViewController.mm, which I had already turned into Objective-C++ by giving it .mm as its extension. But ViewController.mm gets eventually imported by AppDelegate.m, which is Objective-C by default – and I had forgotten to make it Objective-C++, hence the problem.
So renaming AppDelegate.m to AppDelegate.mm solved the problem.
I think #include is a c++ class template..so you need to used using namespace std in your header file and also rename your source file .m format to .mm format.
it works for me :) try this...
I'm trying to copy over some example code into my own project. The example project is iPhoneExtAudioFileConvertTest from the sdk. The example project contains a file called ExtAudioFileConvert.cpp. This file contains what looks like Objective-C code:
assert([NSThread isMainThread]);
The example project runs fine, but the compiler complains about the code above when I build my own project: error: expected primary-expression before '[' token
What's going on here? Obviously there's some way to use objective c bracket syntax in a .cpp file. What do I need to do to make it work?
Change the file extension to .mm for Objective-C++ instead of just .cpp for C++.
The default build settings of iPhoneExtAudioFileConvertTest is Objective-C++.
If you change the settings to According to File Type, you will get the same error message.
So, change the file extension to .mm or change the build settings of your project.