Can't build objective-c project when including Foundation - iphone

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

Related

In what file are __io_putchar() and __io_getchar() defined?

I've noticed that the syscalls.c file for an STM32F407-DISC project (Made through the STM32CubeIDE) has __io_putchar() and __io_getchar() as externs.
But I can't locate any file within the project where these functions are actually defined.
This is what is present in the syscalls.c file:
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
Does anyone know what file these externs are pulling from?
Thanks in advance!
They are in the standard library. So there is no code in your project. You need to download (clone) the source code of the standard library implementation used by the STMCubeIDE toolchain.
Simply write your own ones and they will replace those with the weak linkages.

Duplicate Interface Definition When Implementing FBConnect

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.

How to use Grand Central Dispatch in iOS in a .c file

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.

Typedef for STL Vector does not compile in XCode

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

Compile Arm Assembly directly in XCode

What is the best way to go about compiling arm assembly code into xcode. I have those assembly files which are generated. Is there a way i can just include the .s file directly into the c code that i have. Or i will need to run an preprocessor first which would generate the .o file which i can link with my files. If that is the case, how do you do it in XCode.
If you could post the exact compiler error xcode is spitting out then I might be able to come up with a better solution but my guess as of now is that you are forgetting to add the _ prefix do you functions in assembly.
.globl _add_in_asm
_add_in_asm:
add r0,r0,#1
bx lr
Now in the C source file
#include <stdio.h>
extern int add_in_asm(int i);
int main(int argc, char* argv[]) {
printf("result:%i\n", add_in_asm(10));
return 0;
}
The program should print
result:11
It appears that you just need to add the .s file into your project. Converting it into inline assembly for C is possible, but also fairly difficult, so I recommend against it.
If you are having linking problems, remember Mach-O will be looking for the assembly methods to be prefixed with underscore. #A Person's example shows this, but does not point it out.
C-declaration
extern int add_in_asm(int i);
links against ASM method:
_add_in_asm
That is the first google result and whilst the answer is good, the actual way for me to do it is (Xcode 7.3.1; using real device)
under Xcode add an empty assembly file and use the code as above but add align statement
.globl _add_in_asm
.align 4
_add_in_asm:
add r0,r0,#1
bx lr
in the AppDelegate.m
add
include <stdio.h> and
under the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
extern int add_in_asm(int i);
printf("result:%i\n", add_in_asm(10));
Having said that and after it was successfully run, it does not work after I close Xcode and restart it ... well one has to use real device and when restart, it would try to use emulator!!!!
`
/Users/xxx/Documents/myMac-git/testasm/my-testasm.s:18:11: error: invalid operand for instruction
add r0,r0,#1
^
/Users/xxx/Documents/myMac-git/testasm/my-testasm.s:19:7: error: unrecognized instruction mnemonic
bx lr
^
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1
`
If you see above message use real devices!