How to install and use Objective-C in Ubuntu

Source: Internet
Author: User
Tags mathematical functions
Objective-C is the best class C and object-oriented programming language I have ever used. Objective-C is perfectly compatible with standard C, and on this basis, it adds the SmallTalk element that best interprets basic object-oriented concepts, making it concise and flexible, it is definitely the first programming language tool for commercial projects. It is closer to the underlying layer than Java. You can write inline assembly directly in it or directly connect with the Assembly file (because it is a C language, the message mechanism and OO mechanism of SmallTalk are extended based on C language)

Objective-C is the best class C and object-oriented programming language I have ever used. Objective-C is perfectly compatible with standard C, and on this basis, it adds the SmallTalk element that best interprets basic object-oriented concepts, making it concise and flexible, it is definitely the first programming language tool for commercial projects. It is closer to the underlying layer than Java. You can write inline assembly directly in it or directly connect with the Assembly file (because it is a C language, the message mechanism and OO mechanism of SmallTalk are extended based on the C language ). Compared with C ++, it is obviously much more concise. The magic horse in C ++ inherits and virtual inheritance, with countless pitfalls. Objective-C is very easy to use, and the syntax is not complex, so it won't lead to programmers to be too mixed, and the entire project is easy to maintain.

Therefore, whether in Unix/Linux or in OS X/iOS, using Objective-C programming is a pleasure. Next I will introduce how to install and compile Objective-C in Ubuntu (14.10) of the latest version.


As Ubuntu has installed the Objective-C compiler (gobjc), the steps for installing gobjc are saved, if the Ubuntu bypass system is not installed, run the following command to install it --

Sudo apt-get install gobjc

Next, we will install the gnustep library. Those who have programmed OS X or iOS should be familiar with the Foundation library. This is in the gnustep library. If this library is not installed, you cannot even use NSObject ~ Install gnustep first

Sudo apt-get install gnustep

After that, we will install gnustep-devel.

Sudo apt-get install gnustep-devel


In this way, the entire environment to be installed is installed. Below we can write a code segment for compilation.

Before compilation, go to the/usr/share/GNUstep/Makefiles directory to set the compiling environment and run the following command on the current Console (terminal:

Sudo bash/usr/share/GNUstep/Makefiles/GNUstep. sh


In this way, the GNUStep compiling environment of the current console is ready. Then we are going to create a project to create a folder. Then create a main. m file in it:

 

# Import

Int main (void)
{
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];

NSLog (@ "Hello, world! ");

Unichar c = u'gal ';

NSLog (@ "The character is: % C", c );

[Pool drain];
}

 

 

Create a make file named GNUmakefile.

 

GNUSTEP_MAKEFILES =/usr/share/GNUstep/Makefiles

Include $ (GNUSTEP_MAKEFILES)/common. make

ADDITIONAL_FLAGS + =-std = gnu11

TOOL_NAME = test
Test_OBJC_FILES = main. m

Include $ (GNUSTEP_MAKEFILES)/tool. make

 

Since we use the Unicode prefix literal expression -- u'gal' introduced in the C11 standard in the source code, it represents a UTF-16 character, therefore, the-std = gnu11 compilation option is added to GNUmakefile to enable the compiler to use the latest C11 standard and GNU standard syntax extension.

Note that for other Linux versions, the default installation path of GNUStep may not be in/usr/share, therefore, you need to set GNUSTEP_MAKEFILES according to the path of the current GNUStep/Makefiles. This variable must be defined before include.

The following TOOL_NAME specifies the final target executable file name after make. Name it test.


If we have executed GNUstep. sh before, you can directly press make and press Enter. The project is built. If there are errors such as "gcc: error trying to exec 'cc1obj ': execvp: No such file or directory", it indicates that gobjc must be installed.


Other reference links are provided below:

Http://www.techotopia.com/index.php/Installing_and_Using_GNUstep_and_Objective-C_on_Linux

Http://www.gnustep.org/resources/documentation/Developer/Base/ProgrammingManual/manual_1.html#SEC11


Note that for the first link, if you press gcc directly on the command line, the connection fails because the gnustep library cannot be found. Therefore, the best way is to use makefile to solve the problem ~


The following describes how to use Objective-C, pure C, and compilation. Since the makefile package provided by GNUStep only provides C, C ++, Objective-C, and Objective-C ++ programming languages, but does not support assembly languages, to use assembly in the GNUStep project, compile the Assembly file separately. o target file, and then connect to the target file compiled by other makefiles. The following lists the support for several source file types (the appname in italic is the name of the final output executable file ):

1. appname_C_FILES: C source file, usually. c

2. appname_OBJC_FILES: Objective-C source file, which is generally. m

3. appname_CC_FILES: C ++ source file, which is generally. cpp or. cc.

4. appname_OBJCC_FILES: Objective-C ++ source file, usually. mm

These variables follow the corresponding source file name to be compiled. Multiple source file names are separated by spaces.

For other available make variables, see the following link:

Http://www.gnu.org/software/gnustep/resources/documentation/Developer/Gui/ProgrammingManual/AppKit_4.html#Application%20Makefiles


The following example shows how to connect an Objective-C source file with an assembly file to a final executable file.

The Objective-C source file (main. m) is as follows:

 

# Import

Extern int _ attribute _ (fastcall) MyASMFunc (int a, int B );

Int main (void)
{
NSAID utoreleasepool * pool = [[NSAID utoreleasepool alloc] init];

NSLog (@ "Hello, world! ");

Unichar c = u'gal ';

NSLog (@ "The character is: % C", c );

NSLog (@ "The value is: % d", MyASMFunc (10, 20 ));

[Pool drain];
}

 

Here, __attribute _ (fastcall) enables the parameters and return values of the MyASMFunc function to be passed through registers, which facilitates the implementation of the assembly process.

The following is an assembly file (hello. s ):

 

. Text
. Align 2

. Globl MyASMFunc

MyASMFunc:

// ECX contains the first parameter
// EDX contains the second parameter
Mov % ecx, % eax
Add % edx, % eax
Ret

 

 

After writing the Assembly file hello. s, you can use gcc to compile it into the target file hello. o. Then we can write GNUmakefile:

 

GNUSTEP_MAKEFILES =/usr/share/GNUstep/Makefiles

Include $ (GNUSTEP_MAKEFILES)/common. make

ADDITIONAL_FLAGS + =-std = gnu11

TOOL_NAME = test
Test_OBJC_FILES = main. m

Include $ (GNUSTEP_MAKEFILES)/tool. make

ALL_LDFLAGS + = hello. o

 

Finally, after hello. o is added to the ALL_LDFLAGS flag, GNUmakefile connects hello. o and main. o to the final executable file test.


If you do not need makefile compilation, you can directly use the command line, for example:

Gcc 'gnustep-config -- objc-flags '-lgnustep-base hello. m-o hello

In addition, we can also directly use: gnustep-config -- objc-flags on the console to view the default compilation options for compiling Objective-C, so that we can make some adjustments.

If we want to use the Clang and Objective-C 2.0 libraries, refer to this link: http://wiki.gnustep.org/index.php/Building_GNUstep_with_Clang

Download other libraries can refer to this link: http://wwwmain.gnustep.org/resources/downloads.php? Site = ftp % 3A % 2F % 2Fftp.gnustep.org % 2 Fpub % 2 Fgnustep % 2F

Http://www.linuxidc.com/Linux/2014-03/97744.htm for all properties of @ property in Objective-C

Objective-C and Core Foundation object conversion Memory Management Summary http://www.linuxidc.com/Linux/2014-03/97626.htm

Http://www.linuxidc.com/Linux/2013-12/94309.htm of my views on Objective-C a year later

10 Objective-C basic questions, essential http://www.linuxidc.com/Linux/2013-07/87393.htm for iOS interviews

Objective-C applies to C mathematical functions Http://www.linuxidc.com/Linux/2013-06/86215.htm

Studious Objective-c hd pdf http://www.linuxidc.com/Linux/2014-09/106226.htm

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-12/126211.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.