I have some C code in an iOS project that I would like to optimize using GCD. Currently I can only get my code to compile if change my C file to an Objective-C file and import the Foundation framework. What do I have to include in my C file to get access to GCD?
I've tried:
#include <dispatch/dispatch.h>
but that doesn't seem to work it always complains about code blocks having the ^ character
You'll need to tell the compiler to enable Blocks with the -fblocks flag. You'll also need to use a compiler that understands blocks (Clang, for one).
You might need to;
#include <Block.h>
But it's not something I've done myself so could be wrong here.
Related
I am trying to add FBConnect to my application which includes the SBJson framework. However, when I try to compile the project I get these two errors:
Duplicate interface definition for class 'SBJsonWriter'
Duplicate interface definition for class 'SBJsonParser'
What can I do to fix this error? Thanks for any help.
Delete
#import FacebookSDK/FacebookSDK.h
In your project
I start using FacebookSDK, but then I was disappointed with it's current state and tried to use the old "FBConnect", that's how I have got the error
There are two possibilities:
you have two interfaces with the same name. Use Xcode's find in project menu option to find instances of SBJsonWriter. Then rename one of the interfaces
somehow you have managed to import the .h file twice. Check to make sure you always use #import and not #include.
A bit more info on #import/#include:
include blindly includes the file at the location of the #include statement. This means that if you #include a file twice in your .m you will get two copies of the file. Almost all traditional C #include files have something like the following bracketing all the content:
// some_file.h
#if !defined SOME_FILE_H
#define SOME_FILE_H
// entire content of #include file
#endif
he above is sometimes referrwed to as an include guard macro.
In Objective-C, if you #import a file, a check is performed by the compiler to make sure it has not already been imported. Consequently the guards are usually omitted. So if you #include a file that was supposed to be #imported, neither check will be done and you will sometimes get duplicate definitions.
Sorry in advance if this has been answered, I've searched repeatedly here and the in the apple docs, but haven't found out what is causing this problem. I have a iOS app that is driven by a 'layout' file that contains references to the content. I wanted to create a command line tool to optimize the content and modify the layout file if need. For example, by tiling an image and replacing it in the layout by the tiles.
I thought to create my first ever OSX tool and used the newest Xcode to create a CoreFoundation project, which gives me a main.c like this:
#include <CoreFoundation/CoreFoundation.h>
int main (int argc, const char * argv[])
{
CFShow(CFSTR("Hello, World!\n"));
return 0;
}
which builds fine. I thought to use some of the NS* classes to start working with the command line args, but as soon as I included the Foundation framework and added this line:
#include <Foundation/Foundation.h>
I started to get tons of build errors. I think I'm missing something basic, here, but I can't see it! Did I choose the wrong template? Is there a better one? Or, what's the problem with using Foundation here?
You need to set the file language to Objective C for it to compile. Either that or rename main.c in main.m
I have many constants in my application used by many classes of my project. These constants have to be set at compilation time (they are not modified later).
For now, I use #define statements at the top of each classe that requires the constant. The problem is that I have to repeat these statement in each classe which requires the constant.
I plan to define all these constants in my main.m or in another .h imported by main.m but I think it is not a good idea.
-> Is there a XCODE / IOS mechanic or file made for that purpose ?
-> If not, is it a good idea to define the constants in my main. ?
Thanks for you help
kheraud
You can write all constants in any .h file , then you can import that file in your projectname_Prefix.pch file .
then you don't need to import file in any other source file . its directly get imported .
you can save them in your *_Prefix.pch then they will apply for all classes without importing another class.
Generally the best way to handle shared constants is to declare them extern in one or more dedicated .h files, and then define them in corresponding implementation files. That way you'll be guaranteed to only have one copy of each constant in your binary, unlike with a #define.
You can provide target-wide compiler defines in Xcode by adding them to the Preprocessor Macros build setting. For example, this might let you create a free Lite version of your application by creating a target for it within your project, then adding a LITE define in the Preprocessor Macros.
See this question for more on this.
This is probably a totally noob question but I have missing links in my mind when thinking about linking libraries in iOS. I usually just add a new library that's been cross compiled and set the build and linker paths without really know what I'm doing. I'm hoping someone can help me fill in some gaps.
Let's take the OpenCV library for instance. I have this totally working btw because of a really well written tutorial( http://niw.at/articles/2009/03/14/using-opencv-on-iphone/en ), but I'm just wanting to know what is exactly going on.
What I'm thinking is happening is that when I build OpenCV for iOS is that your creating object code that gets placed in the .a files. This object code is just the implementation files( .m ) compiled. One reason you would want to do this is to make it hard to see the source code and so that you don't have to compile that source code every time.
The .h files won't be put in the library ( .a ). You include the .h in your source files and these header files communicate with the object code library ( .a ) in some way.
You also have to include the header files for your library in the Build Path and the Library itself in the Linker Path.
So, is the way I view linking libraries correct? If , not can someone correct me on this ?
Basically, you are correct.
Compiling the source code of a library produces one object file for each of the source files (in more than one, if compiled multiply times against different architectures). Then all the object files are archived (or packaged) into one .a file (or .lib on Windows). The code is not yet linked at this stage.
The .h files provide an interface for the functionality exposed by the library. They contain constants, function prototypes, possibly global declarations (e.g. extern int bad_global;), etc. -- basically, everything that is required to compile the code which is using the library.
.h files do not 'communicate' with object code in any way. They simply provide clues for the compiler. Consider this header file:
// library.h
extern int bad_global;
int public_func(int, const void*);
By including this file in your own code, you're simply telling the compiler to copy and paste these declarations into your source file. You could have written declarations for OpenCV library and not use the headers provided with it. In other words, you're asking the compiler to not issue errors about undefined symbols, saying "I have those symbols elsewhere, ok? Here are their declarations, now leave me alone!".
The header files need to be included in the search path in order for compiler to find them. You could simply include them via the full path, e.g. #include "path/to/file.h", or supply an -I option for your compiler, telling him where to look for additional headers, and use #include <file.h> instead.
When your code is compiled, the declarations in header files serve as an indication that symbols your code is using are defined somewhere. Note the difference between the words declaration and definition. Header files contain only declarations most of the time.
Now, when your code is compiled, it must be linked in order to produce the final executable. This is where the actual object code stored in the library comes into play. The linker will look at each symbol, function call, etc. in your object code and then try to find the corresponding definition for each such symbol. If it doesn't find one in the object code of your program, it will look the standard library and any other library you've provided it with.
Thus, it is important to understand that compilation and linkage are two separate stages. You could write any function prototypes at all and use them in your code, it will compile cleanly. However, when it comes to the linking stage, you have to provide implementation for symbols used in your code, or you won't get your executable.
Hope that makes sense!
The .a is the compiled version of the code.
The header files provided with a library are its public interface. They show what classes, methods, properties are available. They do not "communicate" with the binary code.
The compiler needs the headers to know that a symbol (a method name for example) is defined somewhere else. They are associated with the right "piece of code" in the library binary later during the "link" step.
I have some code written in C++ which I would like to use in my iPhone app. I added the source files to the XCode project, but I have problem with some parts of the source code, e.g. I have he following code:
#import <vector>
// (...) some other code
typedef std::vector<keypoint> keypointslist;
// (...) rest of the code
In the line with the typedef I'm getting:
Expected '=', ',', ';', 'asm' or 'attribute' before ':' token in
Exactly the same code was compiled with gcc on Linux machine, so I wonder why XCode has problem with it.
Any suggestions?
Are you sure, you told XCode to compile the file as C++ and not as C or Objective-C? Maybe you must use the file extension ".cpp".
It's called #include, not #import:
#include <vector>
+1 with Ludger,
youre adding that to an objective c app, you'll need to add a .cp/.mm/.cpp translation unit to the project to get that to compile - right click on project classes select add class make sure you add one from the cpp section.
If you include C++ code in a ".m" file you must rename the extension to ".mm" or ".M". this applies even if you reference a C++ class from an objective C one, the file must be renamed. Sometimes this renaming has a domino effect and so you end up with a lot of ".mm" files in your project.
Make sure you included <vector> header file before typedef. It looks like the compiler doesn't know std namespace.
You have to rename the ALL files that use the C++ code with .mm extension.
That includes your view controller files that could be importing the C++ class. eg. MyViewController.mm