You're trying to use precompiled headers to "speed up compilation". Xcode has a file called YourProject_Prefix.pch. You can include any number of header files in there that you like.
My question is, how do you select what header files should make it into your PCH? Should you just throw all your header files into there, or will that actually not be optimal?
Just import your header files there like the ones below.
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "YourHeaderFile.h"
#endif
Pre-compiled headers, especially during building your app, can be very useful. The headers in the .pch file are only compiled the first time and then only if the headers change in the future. If your app imports many frameworks/headers that do not change; this could accelerate building (compiling) since the compiler will use the .pch versus compiling every imported framework/class every time you compile.
the pch file will be included in all your source files by default.
that means you should really only put header files in there that are more or less global or never change. I believe putting all your headers in there would slow down compilation because every time you changed one it would cause every other file in your project to have to recompile. (I did not test or research this)
here is a sample from one of my projects:
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#import "Errors.h"
#import "Localization.h"
#import "Logging.h"
#endif
Additionally, take the linked comments about C++ with a grain of salt. C++ uses templates and such that go in header files and make compilation take much longer than you are going to see in objective-c. in objective-c you are only likely to have types and interfaces, not implementation in a header.
Related
I've added something in -prefix.pch file, but XCode gives me error when I use it, saying
Use of undefined identifier ...
However if I compile it (Command+B), it compiles, so I thought it was caused by derived data, I then tried to remove derived data (thus to force it to be re-generated again) but in Organizer there was NO derived data for the project, I also tried Clean but does not work.
Can anyone help?
edit: the macro I defined is not important for the question, but here is the macro:
#define IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
edit 2: I added Kiwi as unit test tool to the project, probably something in project settings are changed which caused the problem.
as bbum says
Ewww… don't put macros in a .pch file! A .pch file is, by definition, a project specific precompiled header. It really shouldn't be used beyond the context of the project and it really shouldn't contain anything but #includes and #imports1
may be this helps
You will need to include inly framework related headers and also adding header in pch file means you need not import that header in any of the file in your project.
#import <Availability.h>
#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
I'm using the most excellent AFNetworking library in a project that I'm currently upgrading to iOS 6. I'm in the middle of the upgrade, whittling down the bunch of warnings that I get when compiling against the iOS 6 SDK.
AFNetworking gives me two warnings in all targets:
SystemConfiguration framework not found in project, or not included in
precompiled header. Network reachability functionality will not be available.
and
MobileCoreServices framework not found in project, or not included in
precompiled header. Automatic MIME type detection when uploading files
in multipart requests will not be available.
Here's the thing, though: those two libraries are added in all my targets. I'd like to get rid of those warnings the proper way; I won't modify the AFNetworking files. I suspect it's Xcode being silly. It's admittedly a small thing, but leaving warnings around is bad practice.
How can I remove those warnings?
I've tried restarting Xcode and cleaning. Both don't work.
I'm not sure if you're using CocoaPods or not but this is a known issue being tracked on the AFNetworking Github page.
I was able to fix this by adding the correct import statements directly to my `PROJECTNAME-Prefix.pch there I changed it to this.
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#endif
If you have something else in there don't delete it. Just add the imports for SystemConfiguration and MobileCoreServices.
For OS X:
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <CoreServices/CoreServices.h>
#endif
If you're using swift: Xcode compiles Swift code before the Prefix.pch file is compiled, so you'll get these warnings even if the correct imports are in your .pch file. The best solution I've found is to add them to the project's Bridging-Header.h file before importing AFNetworking:
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "AFNetworking.h"
This has already been answered, but still, if you're developing a command line tool potentially to be compiled for both OS X and iOS (not App Store for sure), you can add this:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#if TARGET_OS_IPHONE
#import <MobileCoreServices/MobileCoreServices.h>
#elif TARGET_OS_MAC
#import <CoreServices/CoreServices.h>
#endif
#endif
By evaluating the target you're compiling to, it will include the proper files.
I have been building a project with cocos2d 2.0 and box2d, and after cleaning it, it completely breaks.
The compiler stops after 30+ errors: "Too many errors committed, stopping now."
errors like:
precompile prefix.pch: unknown typename 'NSUInteger'
any ideas?
what other information would help? (rookie here)
I solved it. The problem came from a class named "Block". I renamed the class and it solved the problem.
My guess is you added one or more Box2D header files to the prefix.pch without enclosing it in #ifdef __cplusplus therefore the compiler will try to compile them as regular C (Objective-C) files.
Here's a sample prefix.pch with Box2D headers included correctly:
#ifdef __OBJC__
// objective-c headers go here, for example:
#import "cocos2d.h"
#endif // __OBJC__
#ifdef __cplusplus
// C++ header files go here, for example:
#import "Box2D.h"
#endif // __cplusplus
When trying to make a Cocoa framework I ran into the old problem of the headers not copying, which I fixed by changing the visibility(?) of the headers to public. now I Would Like to organise the framework more logically. when the headers are copied there are are placed all in the same directory with no subfolders. I would like it to organise the framework by placing the headers in sub folders so that I can #import them like:
#import <Myframework/Math/MathFunction.h> // import some math related functions
however I notice that the system frameworks contain subframeworks. If I must I am willing to do that and also is there a fast way to convert all the
#import "someFile.h"
to
#import <Myframework/somefile.h>
in the release so the in other project I can #import them without confufing the compiler?
when I create an xCode project with the 'Command Line Tool' c++ stdc++ template, i am able to include and compile opencv headers and run some code.
But i want to use OpenCV in a 'Cocoa Application' context. When created with that template, i got compile errors when I include the OpenCV headers in main.mm. (I already changed main.m to main.mm, the '//NSApplicationMain(argc, (const char **) argv);' is commented out)
One of those errors is: "Statement-expressions are allowed only inside functions"
I suppose its some kind of compiler version error, but when i compare the project build settings i cant find differences.
Do you have any ideas/expertise?
I ran into the same problem, I spent 2 days doing serious system tracing and as expected, the solution was so simple that an 8-year-old could have found it more quickly than I did.
Ready for this?
In the .mm file where you want to use OpenCV, you need to do your #include "opencv2/opencv.hpp" BEFORE any other includes. That's it! Just move it up a line and watch your problem magically disappear faster than that rug that really tied the room together.
This is for OpenCV 2.2 by the way, hence the new include file. Also if your XCode project uses a prefix header file (look in "Other Sources" for a "YourProjectName_Prefix.pch" file), then you'll need to put your #include "opencv2/opencv.hpp" there instead of in any other file.
Ian Charnas's Answer is correct, but there is one modification I would make.
This article has a more specific solution, and an explanation of why you need to do this.
http://aptogo.co.uk/2011/09/opencv-framework-for-ios/
// Add this new section BEFORE the #import statements for UIKit and Foundation
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
Even if you rename your "main.m" to "main.mm" and moving the "#include opencv2/opencv.hpp" to the top (in the main file), the preprocessor will insert cocoa.h first because of the precompiled header files nemed something like "_Prefix.pch". To avoid such problems
- delete the #import statement or
- insert an #import statement above the cocoa.h import statement
Try adding: -lstdc++ to the "Other linker flags" in the build settings for your Cocoa app.
A cocoa application made by the Xcode templates won't link include the c++ library in it's settings by default.
add this to your .pch file
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
dont forget to convert all of your .m files into .mm files
You'll probably find it easier to use OpenCV's C API rather than the C++ API. You can call C APIs directly from Objective C of course, so this makes life a lot easier. You just lose some of C++'s syntactic sugar, like default parameter values.