Linking a lib written in C in XCode + Objective-C - objective-c

Is it possible to use a library I wrote in C (compiled with GCC on a Mac) on an Objective-C project under XCode?
The project is for a Mac OS X app, not iOS. The library was compiled via GCC on the command line and it is in C (as in C language) and the header for the library (.h file) is a simple C header with function declarations.
If yes, how? Furthermore, do I need to convert the .h files to an objective-c styled header?
Thank you!

You might take a look at Using static libraries with iPhone SDK.

Related

is static library with Objective C compatible with any version of XCode?

I am from c++ background and in c++ world, if I create an static library with any specific version of a compiler, then it is advisable to use same version of compiler for binary which will use this static library.
Is this statement true for Objective C static library as well? or if I have Objective C static library compiled with XCode X & and executable compiled with XCode Y still there will be no conflict?
Yes no conflicts. Migration of my legacy project on Objective-C to Xcode 12 was done without problems: just opened and compiled. Despite my colleagues: they had to continue working with Xcode 10 and 11 on swift projects.
Objective-C is ABI, Module and Source stable language like C, despite C++ and Swift

Swift Build Linux vs macOS <Foundation/Foundation.h> not found

I am trying to use an old Obj-C static library in a Swift package. I am able to get the package compiling successfully on my mac by using compiler flags in the swift build command. However when running on a Dockerfile running 5.1-xenial the header file's in the static library produce the following error:
#import <Foundation/Foundation.h> "Foundation.h" not found.
As I understand is the because Objective-C foundation library is not available on the machine ? Would I have to install something like GNUstep? Is Swift interoperable with Objective C (I saw that it might not be possible on another SO post) ?
Update on GNUStep approach:
I was able to install and run GNUStep allowing for Objective-C development tools (i.e the Linux Foundation framework) alongside Swift however during the swift build I end producing this error:
/usr/GNUstep/Local/Library/Headers/GNUstepBase/GSObjCRuntime.h:92:1: error: expected identifier or '('
#class NSArray;
^
I think this is because Swift build treats the header file as a purely C library. Any suggestions on fixing this problem ?

How can i use libshout library for os x project in XCode

there is library called libshout which provides functionality for connecting to icecast server. Could anyone tell how to include it to xcode project for os x application? Or how to compile it and include to the project.
Thank you for any help.
The answer is, that it is possible to install libshout by homebrew for example, then it will be in /usr/local/Cellar/ along with other libraries it needs: libogg, libvorbis, speex, theora. Then you can add all those libraries to xcode project (.h and .a fies or all files..) with libshout, then just include shout.h header to you source file and you can use it.

Cross platform C++ function in xcode to read files in windows, Linux and MAC OS

I have a C++ Project which reads file. I need to improve the project in a way that it also read files from Mac Os.
I created a Project in x-code and so far I decided to use #ifdef TARGET_OS_MAC and #import <Foundation/Foundation.h> to separate the code for windows and Mac. Now i when i write the code for MAC-Read file using NSString in my main.cpp file, it doesn't work and it gives me error that it does not know the NSString. I tried creating new project command line and Foundation type but got the same error.
How to combine these two?
You should include cocoa framework:
#import <Foundation/Foundation.h>

Compiling Objective-C files into object files (.o)

I have been tasked to create object .o files for an iOS project in Xcode 4. I have tried the following command which works on NSObject classes
gcc -c implemtationfile.m
but if I try to run the command on a class inherited from UIViewController for example I get a ton of errors starting with
UIKit/UIKit.h: No such file or directory
Secondly, since there are multiple files in the project, is it possible to create one .o file for all source files?
Not surprised you've got errors.
You are trying to compile iOS code from OSX. If you invoke GCC from the terminal, you'll get the Mac OS X compiler.
Even if it can produce .o files, those object files are Mach-O object files, meaning they are compiled as object-files against the Mac OS X compiler tool-chain.
No iOS here. Your object files will have the Intel (x86) or PPC architecture. iPhone is ARM, so you're wasted.
You should be able to proceed ARM object files using the right GCC compiler tool-chain.
Something like:
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-gcc-4.0.1
I haven't tested, but that's clear that you need to use the iPhone ARM version of GCC. Just invoking GCC will invoke the Mac OS X version (in /usr/bin/).
A compiler like GCC needs to be compiled for a host and a target architecture.
The host architecture is the architecture in which you will invoke the compiler.
The target one is the architecture for which you'll build binaries, or object files.
So try to compile your code with the correct version of GCC, which suits the iPhone target architecture.
Also note that, if compiling against a framework, you should use the -framework GCC argument.