Objective c with linux

Objective-C и Linux

Objective-C и Linux

Хотелось бы поделиться опытом разработки на Objective-C на linux.

Я использовал Archlinux, но и с другими дистрибутивами проблем быть не должно.

Все подробности смотрите под катом.

Причины для подобной связки могут быть разные, и сразу скажу такую вещь. Все, с кем я общался, рекомендуют использовать Mac. В моем случае, я всего лишь хотел познакомиться с языком, поэтому приобретение еще одного компьютера посчитал излишним.

Необходимые программы

Первым делом, нам нужно установить компилятор GCC

На Archlinux это делается следующим образом:

pacman -S gcc
Вам могут понадобиться права root: sudo pacman -S gcc, в данном случае

После этого, необходимо установить Obkective-C фронтенд для GCC
pacman -S gcc-objc

Казалось бы, что все в порядке, приступим к написанию кода.

В качестве редактора я использую Gedit, в котором подсветка синтаксиса есть «из-коробки», так же я установил набор плагинов, в число которых входит встроенный терминал, сниппеты и прочие вкусности.

//main.m
int main( int argc, const char * argv[]) <
return 0;
>

gcc main.m -lobjc

На данном этапе все должно работать.

Первая проблема

Поигравшись некоторое время с кодом, я обнаружил, что библиотеки OpenStep отсутствуют

Для решения нужно написать в терминале следующее:
pacman -S gnustep

Pacman предложит вам выбрать из 4 пакетов, выбирайте все.

Проверим следующий код:

int main( int argc, const char * argv[]) <
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@»Hello, World»);
[pool drain]
return 0;
>
Тут же компилятор выдает ошибку, NXConstantString ему не знаком. Так и должно быть, ведь мы не сказали компилятору о том, где нужно искать новые библиотеки.

Чтобы не писать их каждый раз, я предлагаю создать Makefile

//Makefile
CC = gcc
CFLAGS = -lobjc -std=c99 -I /usr/lib/GNUStep/System/Library/Headers \
-L /usr/lib/GNUStep/System/Library/Libraries -lgnustep-base \
-fconstant-string-class=NSConstantString
OBJECTS = main.o

main.exe : $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) — o main.exe

main.o : main.m
$(CC) $(CFLAGS) -c main.m

Обратите внимание, что вместо четырех пробелов, нужно использовать один символ табуляции
Набираем в консоли: make
У меня этот код скомпилировался, но NSLog выдало некую ошибку, исправить которую можно следующим образом:

TZ=’WS-U’
export TZ

Поздравляю, теперь вы можете смело писать программы на Objective-C!

Источник

Compiling Objective-C project on Linux (Ubuntu)

How to make an Objective-C project work on Ubuntu?

I’ve tried two approaches to compile it:

I created a GNUmakefile Makefile:

So in both cases compiler gets stuck at

Do you have and idea how to resolve this issue?

6 Answers 6

It’s right. In both cases you did not include Fraction.m in your list of files to be compiled, so it can’t find the implementation of the class Fraction

From the comment, this command works

I am not an expert at writing the make files like that, I find simply typing the following works on ubuntu quite well:

I am using it on this project:

If the above GCC command does not work you have not installed enough packages, use apt-cache to search for more gcc and objective c packages to install (I just installed more packages that looked relevant at random until it worked)

the make file:

The approach I just got working was (in Ubuntu, which is closely related to Debian):

  • Use Synaptic to install all likely-looking GnuStep packages;
  • Source ( . ) the GnuStep startup script, /usr/share/GNUstep/Makefiles/GNUstep.sh (this can go into .profile or .bashrc or something so you don’t have to do it manually every time)
  • Create a GNUmakefile according to the instructions in A First Tool

This allowed me to successfully build command line programs.

Источник

Objective-C in Linux [duplicate]

I know that developing iPhone apps (painlessly, at least) pretty much requires a Mac. However, is it possible to develop normal desktop applications using Objective-C in Linux? For example, could I make a game using a development library for Objective-C, and release it for Linux, rather than iOS?

1 Answer 1

There are a lot of normal desktop applications which use GNUstep (free version of Cocoa API) on Linux.

For games you can try sdlobjc — SDL binding for Objective-C.

There is even a Linux distribution called Étoilé which uses GNUstep based user environment and all own GUI applications writen in Objective-C.

If you want to learn how to program in Objective-C using GNUstep on Linux (or cygwin) there are some possible problems:

Читайте также:  Watermark delete windows 10

You must use cygwin in windows to build the application. Which means that it:

a) some applications can be slowed down because of cygwin’s translations of POSIX API calls to Win32 API calls. For example fork() call will be translated in Win32’s CreateProcess call and some others and will be less efficient than in UNIX.

b) your application must be distributed with cygwin’s dll

c) your application can’t be 64bit (at least for now)

d) you application will see all your windows disk drives as a part of unix filesystem hierarchy (c: and d: will be /cygdrive/c and /cygdrive/d) and you will have /bin /tmp /usr /etc avialable under / as well.

There’s not up-to-date books about GNUstep or about programming Objective-C not using Mac OS. Thre is Stephen Kochan’s book «Programming in Objective-C 2.0 (2nd Edition)» where he unfortunately ommits explaination of how to build even basic examples under Linux or Windows. I hope it is fixed in 3rd edition.

GNUstep has own themes so apps may be themed differently than GTK Linux applications in Linux or usual themed application in Windows.

Источник

Objective-C in Linux [duplicate]

I know that developing iPhone apps (painlessly, at least) pretty much requires a Mac. However, is it possible to develop normal desktop applications using Objective-C in Linux? For example, could I make a game using a development library for Objective-C, and release it for Linux, rather than iOS?

1 Answer 1

There are a lot of normal desktop applications which use GNUstep (free version of Cocoa API) on Linux.

For games you can try sdlobjc — SDL binding for Objective-C.

There is even a Linux distribution called Étoilé which uses GNUstep based user environment and all own GUI applications writen in Objective-C.

If you want to learn how to program in Objective-C using GNUstep on Linux (or cygwin) there are some possible problems:

You must use cygwin in windows to build the application. Which means that it:

a) some applications can be slowed down because of cygwin’s translations of POSIX API calls to Win32 API calls. For example fork() call will be translated in Win32’s CreateProcess call and some others and will be less efficient than in UNIX.

b) your application must be distributed with cygwin’s dll

c) your application can’t be 64bit (at least for now)

d) you application will see all your windows disk drives as a part of unix filesystem hierarchy (c: and d: will be /cygdrive/c and /cygdrive/d) and you will have /bin /tmp /usr /etc avialable under / as well.

There’s not up-to-date books about GNUstep or about programming Objective-C not using Mac OS. Thre is Stephen Kochan’s book «Programming in Objective-C 2.0 (2nd Edition)» where he unfortunately ommits explaination of how to build even basic examples under Linux or Windows. I hope it is fixed in 3rd edition.

GNUstep has own themes so apps may be themed differently than GTK Linux applications in Linux or usual themed application in Windows.

Источник

Thread: Objective C on linux?

Thread Tools
Display

Objective C on linux?

Hey. I’m about to start interning with a start-up company whos entire model is currently based off of Objective-C. What I was wondering is if this language will run on other platforms, namely ubuntu and windows. Thanks!

Re: Objective C on linux?

Objective-C can also be compiled by gcc on Linux and Windows.

Re: Objective C on linux?

Cool. But I’m being told that the existing platform that I’m going to be working with cannot be compiled on linux because there are «mac os calls» that won’t run on linux. Is there a way around this?

Re: Objective C on linux?

If they are Mac OSX specific calls then chances are there isn’t a way around this natively. It might be worth looking into GNUStep. Theoretically you could install Mac OSX into a virtual machine and run it there — but (to the best of my knowledge) doing so is in violation of the OSX EULA which I believe states that you can only run OSX in virtual machines on a machine already running OSX (or something similar).

Re: Objective C on linux?

Using Mac-specific stuff, you’re going to have to be running in a Mac environment. Unless the application is compatible with GNUStep (which should be encouraged, but what kind of company listens anyway), you’re out of luck, and are going to have to use a Mac to develop.

Re: Objective C on linux?

Let’s see very carefully. If you want to use Objective-C, just follow this How-To http://ubuntuforums.org/showthread.php?t=1064045 That’s all you need to compile an Objective-C program.

But, you surely know ObjC is quite useless without the OpenStep libraries. OpenStep is an open standard and Apple NeXTStep is just one implementation (though the original which lead to the standard), just like GNUStep is. So, are you sure those «OSX calls» you mean aren’t just calls to the OpenStep libraries? Ok, you won’t have NeXTStep in GNU/Linux, but you can use GNUStep and it should work. For that, do:

Читайте также:  Windows 10 we have fixed

(Or install gnustep, though that will install the whole environment not just the libraries and will clutter your GNOME menus a lot. )

The docs (/usr/share/doc/gnustep-base-doc) have enough information to compile GNUStep-based programs.

But, if your code is actually interfacing to Mac OS X’s API (e.g. Cocoa), then, you are pretty much lost without a Mac OS X system. It’s like trying to access Windows’s API in a GNU/Linux system. Or trying to use Linux’s API in a Mac. Also, the executable format will be different (OS X: Mach-O; GNU/Linux: ELF)

Re: Objective C on linux?

In order to build existing Objective-C code, you need three components:

1) an Objective-C compiler
2) an Objective-C runtime
3) the class libraries which the code you want to port is using

The compiler is the easiest part. There are a number of Objective-c compilers around, all of which are cross-platform:

— the objective-c front-end in GCC

this can target any platform which is supported by CGG as a target

— Clang, a new fast compiler for C, Objective-C and C++ for LLVM

this can target any platform which is supported by LLVM as a target

— Portable Object Compiler, aka PoC

this can target any platform for which an ANSI-C compiler is available, it generates C code

— the original Stepstone compiler

this, too was a preprocessing compiler for C, I heard it can still be found, maybe google will find it for you

Note, that the compiler back-end will take care of executable formats (e.g. MachO versus ELF) for you, so that is of no concern to you whatsoever.

The runtime isn’t much of an issue either. There is an open source runtime for the GCC compiler but that requires GCC to build, so on Windows you would need something like MinGW to build and use it. But of course that won’t be an issue when you target Linux as the GCC toolchain is default anyway. IIRC the PoC compiler has its own runtime, too.

The class libraries, called Frameworks in Objective-C lingo, are most likely the biggest challenge when porting MacOS X code. In principle, GNUstep will provide you with all the classes provided by Cocoa and they are designed to be drop-in compatible. In practise, however, Apple has many more developers than the GNUstep team and they keep adding new classes. That means that right after a new major SDK release from Apple, GNUstep will lack the classes which Apple has just added, and it will take a little while until the GNUstep team can add those to GNUstep.

The OpenStep API, on which Cocoa and GNUstep is based was designed to be platform independent and that hasn’t really changed. The frameworks hide all the platform specific details from your Objective-C code. Unfortunately though, many MacOS X applications are not written with a policy only to use the Cocoa API. Every once in a while a developer may use a system call from another API called Carbon, which is Apple’s legacy API, initially provided to make it easier for developers to bring their code from the classic MacOS to MacOS X.

If the code base you want to port contains many such Carbon API calls, then you will need to replace them with Cocoa equivalents or with Posix system calls, etc etc.

There is also another slight difference between Cocoa and GNUstep. OpenStep was designed with two user layers only, the Foundation and Application layer. GNUstep follows this. However, when Apple was working on the early MacOS X, it was a big challenge to bring all the developers along who had all their code using the classic Macintosh API (called the Macintosh Toolbox). Mr. Jobs wanted to get rid of this in a clean sweep, but most of the developers were up in arms. So a compromise was found in the aforementioned Carbon API, which contains about 60 or 70% of the old Toolbox API calls. Yet, understandably and sensibly, Apple didn’t want to waste development resources and so they decided to build a low level API called Core Foundation which was to form the basis for both Carbon and Foundation (the lower user layer of Cocoa, just as in OpenStep). A number of Foundation calls can be mapped 1:1 to Core Foundation calls. Apple calls this «toll-free bridged». Any CF API calls for APIs which are toll-free bridged can easily be replaced with Foundation calls.

Читайте также:  Самая быстрая windows 10 home

The code you want to port might be using non-toll-free bridged Core Foundation calls, though. Those are not available in GNUstep. So, even if your colleagues say «No, we don’t use Carbon here» there may still be some API calls which are not strictly OpenStep. Ask if they are using Core Foundation calls directly. If they did, check if there are any non-toll-free-bridged ones. Eeven then, there are some ways around that, too.

Apple had released an open source package called CFLite (Core Foundation Lite), it looks like it has since been abandoned but it should still be around somewhere, Core Foundation API calls don’t change, so even an older copy should be alright. If I am not mistaken, Apple has since licensed Core Foundation under their APSL license, which is like a BSD license but with restrictions that you can’t use Apple’s brand and product names for your own stuff, in practise this will not be a problem but legally the clauses that establish this are incompatible with the GPL. This is a little silly, because the restrictions don’t really limit your freedom to use and modify the software, and even with the GPL, you can’t use Red Hat trademarks for example, regardless of the GPL. The difference is that in the case of the APSL, the restrictions are written into the license, in the case of the GPL, the restrictions are imposed by trademark law, which the GPL does not circumvent. The net effect is the same, but legally it makes the APSL incompatible with the GPL, although I am not a lawyer, so don’t take my assessment of what the APSL means for granted, ask your legal people.

There is also an open source project called libFoundation which provides a BSD licensed API that is compatible with Foundation and IIRC this has drop in replacements for CF calls, at least some, or so I think. You may want to check out how much libFoundation will be of help to your effort.

However, don’t be discouraged. There are quite a few applications which can be compiled without modifications on Linux under GNUstep and MacOS X under Cocoa and even on Windows under Cocotron (an OpenStep implementation for Windows). So, this is being done all the time and it is probably worthwhile throwing out the legacy Mac API calls anyway.

Note, that Apple is phasing out the Carbon API. They have announced that they will not make Carbon 64bit, so all the Carbon based applications will be stuck with 32 bit forever. Chance is that this is only the tip of the iceberg and once enough developers have converted there code to Cocoa, then Mr. Jobs may pull the plug on Carbon altogether. Apple has a history of dropping things more radically than most other vendors: No floppy disks when the iMac was introduced. No more ADB keyboards and mice when USB was introduced. Initially, they didn’t even want to support the classic Mac API at all. In other words, if that code base contains any legacy calls, you probably want to get rid of them anyway.

Last but not least, there is a mailing list called cocoa-dev, which is a very good resource even if you want to port away from the Mac OS X. Of course there is also the GNUstep-developer mailing list, which is a good resource too, but fewer subscribers. I recommend you subscribe to both lists.

Last edited by UBForty; May 1st, 2009 at 07:23 AM . Reason: typos fixed and a disclaimer on license stuff

Источник

Оцените статью