Skip to content

Getting started with Objective C and Cocoa

Ryan Grimm edited this page Aug 6, 2013 · 2 revisions

Disclaimer

The following steps were successfully performed on a x86_64 Ubuntu 10.04 install and vim in version 7.2.330, as well as on a x86 Ubuntu 10.10 install. Your mileage may vary depending on your distribution/operating system.

Installing Clang

See Home for a detailed description of installation options.

Installing Cocoa

You need the Cocoa framework (the GNUstep implementation), of course. Either get the prepackaged one (sudo apt-get install gnustep should be sufficient) or install from source (the option I chose) following the instructions at http://wwwmain.gnustep.org/resources/downloads.php.

Compiling test

Try to compile a simple program using clang and Cocoa, take the following 'test.m' for example:

#import <Foundation/Foundation.h>

int main()
{
   NSAutoreleasePool *pool = [NSAutoreleasePool new];
	
   NSString *aString = [NSString stringWithString:@"It works!"];
   NSLog(@"%@", aString);

   [pool drain];
   return 0;
}

Before compiling follow JordyD's instructions so that 'objc.h' may be found by clang:

http://ubuntuforums.org/showthread.php?t=1412542

Now, compile by:

clang -I/usr/GNUstep/Local/Library/Headers \
      -L/usr/GNUstep/Local/Library/Libraries \
      -lobjc -lgnustep-base -fconstant-string-class=NSConstantString test.m

(Note that your path for the -I and -L directives might differ.)

Install clang_complete

Once you're able to compile code with clang it's time to install Xavier's magic script: Get it from here

http://www.vim.org/scripts/script.php?script_id=3302

and follow the installation instructions (in essence: move it to '~/.vim/plugin'). Of course since your here you might as well grab the file 'clang_complete.vim' directly from the repository.

Let clang_complete know what you're doing

The last thing we need to do to is let clang_complete know what files we need to include in our program. Assuming you created the above 'test.m' file create a file named '.clang_complete' in the same directory and put the following in it:

-I/usr/GNUstep/Local/Library/Headers

(Note: this is the same include that was used to compile in step 3, so if you had to adjust it then, you need to adjust it now.)

Take it for a spin

Time for a test! Open 'test.m' (using vim of course) and insert

[aString l

somewhere after the NSString aString has been declared. Then press <C-X><C-U> and you should see all possible completions starting with 'l'.