I think block is the Objective-C feature. Recently I am adding the block to some C source file and I found it just compile OK.
I get confused, for *.c file, should the compiler(Clang) consider it as the pure C file, why it can permit block?
Actually Clang introduces blocks
to C and, by extension, Objective-C, C++, and Objective-C++
As you can read here in the overview.
Of course, using blocks makes your source incompatible with C11 standard but, you know, as long as your code is for Mac OS X only...
EDIT: Blocks are also available in GCC, didn't know about it.
Blocks were added in such a manner that they are also a "C" feature in Apple's compilers.
From Apple's Blocks Programming Topics
Blocks are available in GCC and Clang as shipped with the OS X v10.6
Xcode developer tools. You can use blocks with OS X v10.6 and later,
and iOS 4.0 and later. The blocks runtime is open source and can be
found in LLVM’s compiler-rt subproject repository. Blocks have also
been presented to the C standards working group as N1370: Apple’s
Extensions to C. As Objective-C and C++ are both derived from C,
blocks are designed to work with all three languages (as well as
Objective-C++). The syntax reflects this goal.
Related
Can I compile an Objective-C program to C?
I'm interested if this can be done so that it can be compiled with other C compilers.
I am aware that GCC can compile Objective-C.
Sure. For example, POC compiles Objective C to plain C code, and the recommended way to use it is to then compile that C code with gcc.
Of course the generated C is an unreadable mess, consisting mostly of calls (through generated extern declarations) to objc_runtime calls, so you'll need to link that runtime to your compiled code.
There's also an LLVM backend that generates C code from any frontend. So, you can plug in the clang Objective C frontend and the C backend and build your own ObjC-to-C compiler. The result will probably be even more unreadable than what POC generates.
Of course most people don't want to compile Objective C*, they want to compile "Objective C 4.0".** POC won't do that; only clang and llvm-gcc will (and in the future, most likely only clang). And they don't just want an Objective C runtime, they want the OS X or the iOS runtime, or at least OpenStep, which again pretty much means clang, llvm-gcc, or gcc.
The big question is, why do you want to do this? You will need to have an Objective C runtime to link to, and your C code will have to be compiled in a way that's ABI-compatible with the way that runtime was compiled, so… I'm not sure what you're going to gain over just compiling from ObjC to native code with gcc or clang in the first place.
* As in the language defined in Brad Cox's 1986 book and/or in his 1991 followup.
** Note that Apple explicitly decided not to call their latest revisions to the language "Objective-C 4.0"… or anything else. The last version with a name, or a real specification, was "The Objective-C 2.0 Programming Language". But Apple has continued to add features, and the "unnamed feature set that came in with the LLVM 4.0 frontend for Objective-C 2.0" is a bit of a mouthful.
Technically, yes, but it'll be really ugly. LLVM had a C backend — i.e. it could compile to C — up to version 3.1. There have been efforts to revive it but I can't speak as to their progress. There's also a C++ backend that is better maintained but doesn't quite do the same thing.
However you'll have the obvious problem that none of the Foundation libraries will be available on your target platform. No NSString, no NSArray, etc. Once you've found a working implementation of those you'll also have found a working Objective-C compiler.
I'd like to ask if gnustep's toolchain is appropriate for netbsd development where one'd normally use plain C. I'm interested in the benefits of Obj-C only with basic APIs like NSObject's reference counting and dynamic stuff.
My question is twofold:
is gcc's Obj-C ABI compatible with gcc's C ABI? so that I can use regular C libraries
is Obj-C's runtime layer good to go where netbsd targets embedded?
Thank you in advance!
is gcc's Obj-C ABI compatible with gcc's C ABI? so that I can use regular C libraries?
This has nothing to do with the ABI at first glance. Objective-C is a strict superset of C, so it's true on every platform that you can use C code with Objective-C code. You can even call Objective-C methods from plain C code using the Objective-C runtime library.
is Obj-C's runtime layer good to go where netbsd targets embedded?
I don't exactly see what the question is here. Are you asking whether it is possible to port GNUstep to embedded platforms? If so, I'd say yes, it should normally be possible (with the appropriate constraints of an embedded system), but in my opinion, it's too heavyweight for embedded development.
If you aren't interested by AppKit, you may also take a look at https://webkeks.org/objfw/.
Runtimes may contain assembly bits that you will want to verify that they will actually work on specific CPU type. Old Foundation library like libFoundation may also suite your needs. If you want to use thing like Objective-C++ or Objective-C 2.0, I'd recommend clang instead of gcc.
It is possible to do OO programming in pure C.
Some strategies use pre-processor macros to make it easier and less error prone. Some strategies involve adding new syntax which is expanded to pure c by a pre-processor, along with a base object class and some methods for memory management.
It seems that Objective-C began as a project much like this
Do any tools exist that allow objective-c syntax to be processed to pure C?
Without having explored it, it seems do-able.
Just to clarify, I am not asking about compiling iOS code to other platforms, or asking about ports of the cocoa library to other platforms, I am wondering about ways of using oo techniques in pure-C, using Objective-C syntax and a preprocessor or precompilation step.
Portable Object Compiler. It's not capable of compiling modern Objective-C, but it sounds like it is perfect for what you're asking. Look here at a discussion of POC's shortcomings
The situation for C++ is more interesting. Cfront was the original C++ compiler that produced C code, but besides being long outdated it was commercial and cannot be (easily?) downloaded today. Fortunately, there is Comeau C/C++ which is supposedly very modern and standards compliant. It costs $50.
However, I wouldn't expect to get very readable C code from either of them (especially the full-featured Comeau).
It is possible to do oo programming in pure c?
Yes, as oo is a matter of philosophy. Look at glib and how you can do c style object: http://developer.gnome.org/glib/
Apple did it with Core Fundation: https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/CFDesignConcepts.html#//apple_ref/doc/uid/10000122i
By the way: Do any tools exist that allow objective-c syntax to be processed to object oriented pure c?
Yeah: gcc (GCC 4.6 according to Wiki, never actually checked ;)) and clang, tools that you are usin everyday.
This is the Objective-C Runtime who make the obj-c obj-cAble, so you need libobjc.A.dylib library too. You can write obj-c in pure c code, since all message '[]', '#' directive and other obj-c stuff are converted in c after compilation.
No it's not possible, as all special (non-identifier) symbols of Objective-C can not be used as preprocessor macros. At least not with the standard C preprocessor.
Other preprocessors may be able to define macros with non-identifier names, although I don't know of any.
When talking about preprocessors in the early days of Objective-C (and also about C++) it's probably (and in the case of C++, definitively) a custom made parser that instead of outputting assembler or objective code outputted C code.
Is Objective C an Apple language or can you run Objective C on other platforms like linux?
Objective-C is not an OS X/iOS specific language, eg. the GCC has its own, non Apple, ObjC runtime that also works on Linux (not sure about LLVM, but I guess they have their own runtime too).
However, the complete library (Cocoa, UIkit, etc) are Apple only. You will have to write your own NSObject and all subclasses if you want to use them (or use the GNUStep library for this).
You can run it on Linux using GNUStep and GCC as the compiler. By the way you will not be able to use a full Cocoa environment as a framework. Have a look at this compatibility list of AppKit
While it's that the language can be compiled and run on multiple other platforms (as others have said), in practice it's almost exclusively used for Mac OS X and iOS development.
Also checkout Cocotron.
I do not have a Mac, or an iPhone. However, the concept of taking C and making it more dynamic towards the idea of smalltalk, python or ruby is really attractive to me. I'd love to start on objective C.
Is objective C just a syntax superset of C or is it really like C. As in, can it be compiled with gcc etc..?
I do most of my programming in Ruby. Objective C seems so much more forgiving than C. You can write native extensions for Ruby in C. Can you write native extensions for Ruby in Objective C?
How can I get started with Objective C outside of owning an Mac/iPhone and having XCode?
Wikipedia says
Today it is used primarily on Apple's
Mac OS X and iPhone OS, two
environments based on, although not
compliant with, the OpenStep standard,
and it is the primary language used
for Apple's Cocoa API though it was
originally used as the main language
on NeXT's NeXTSTEP OS. Generic
Objective-C programs which do not make
use of these libraries can also be
compiled for any system supported by
gcc, which includes an Objective-C
compiler.
Objective-C today is often used in
tandem with a fixed library of
standard objects (often known as a
"kit" or "framework"), such as Cocoa
or GNUstep.
Maybe you should get a Linux vm if you are mainly running Windows and get GNUStep running.
apple contributed the objective c changes back to gcc (not that they had much choice, being GPL). So wherever you can set up gcc, you can set up an objective-c compiler.
Beyond that, the Gnustep environment can give you a bit of the flavor of the original OpenStep/NextStep environment.
Yes, you can write, compile and run objective c programs outside a MAC/IPhone
Please check the link below for details
http://www.otierney.net/objective-c.html#settingup
http://www.faqs.org/faqs/computer-lang/Objective-C/faq/
There's a difference between Objective-C the language, and the frameworks that Apple has built on top of it. Those frameworks consist of a bunch of objects which you can use in your code and are especially helpful if you're targeting the Mac or the iPhone. There are some similar objects for parts of it in GNUStep, but it's not a complete replacement for the Cocoa/etc library and especially doesn't cover anything iPhone related.
GNUStep and the Objective-C language (via gcc) are available for many platforms, and you can write your own programs that use Objective-C objects and interact with any C api from Objective-C.
Objective-C is a superset of C and so is not really more forgiving than C, but it is arguably more forgiving than C++ (an almost-superset of C). You can also mix Objective-C and C++ with GCC but that gets a little complicated, especially regarding exception handling.
For getting started and playing around with the language though, GNUStep and GCC would be a good way to go.
I don't see any compelling reason why you cannot use Objective C for native ruby extensions, except that you won't benefit much. You will still have to use the lower level C Api calls, since as far as i know, there aren't any ruby to objective-c bindings. Additionally, Objective C comes with a fairly substantial library of utility classes that, while helpful, won't easily justify their size and weight in the context of a ruby extension.
You can you this page for simple experiments with ObjC: http://www.codesign.cz/LearnObjC/