Objective-C for Windows Hello, world!

Source: Internet
Author: User

Install gnustep

Gnustep Windows Installer provides a simulated development environment for objective-C on the Windows platform. There are four software packages, including gnustep system and gnustep core, gnustep devel and Cairo backend are optional. Render manager must be installed and installed at one time, so as to avoid future troubles.

Write Hello, world!

After the installation is complete, execute shell in the gnustep option in the Start menu to open the command line. Here, you can use VI to compile the object-C program, however, the operation is always complicated. In fact, you can directly enter c: \ gnustep \ home \ in windows \UsernameDirectory. Compile the object-C program with your favorite tools and then compile the program in shell.

The content of the helloworld. M file is provided directly, which is taken from programming in objective-C 2.0:

# Import <Foundation/Foundation. h>

Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
Nslog (@ "Hello world! ");
[Pool drain];

Return 0;
}

First compilation:

Gcc-O helloworld. m

The error message is displayed. the header file cannot be found:

Helloworld. M: 1: 34: Foundation/Foundation. h: no such file or directory
Helloworld. M: In function 'main ':
Helloworld. M: 4: Error: 'nonutoreleasepool 'undeclared (first use in this function)
Helloworld. M: 4: Error: (each undeclared identifier is reported only once
Helloworld. M: 4: Error: for each function it appears in .)
Helloworld. M: 4: Error: 'pool 'undeclared (first use in this function)
Helloworld. M: 5: Error: cannot find interface declaration for 'nsconstantstring'

Second compilation:

Gcc-O helloworld. m \
-I/gnustep/system/library/headers/

The result shows an error message. The interface declaration cannot be found:

Helloworld. M: In function 'main ':
Helloworld. M: 5: Error: cannot find interface declaration for 'nsconstantstring'

Third compilation:

Gcc-O helloworld. m \
-Fconstant-string-class = nsconstantstring \
-I/gnustep/system/library/headers/

The result shows an error message. The Link Library cannot be found:

Helloworld. m :(. Text + 0x33): Undefined reference to '_ objc_get_class'
Helloworld. m :(. Text + 0x45): Undefined reference to '_ objc_msg_lookup'
Helloworld. m :(. Text + 0x64): Undefined reference to '_ objc_msg_lookup'
Helloworld. m :(. Text + 0x80): Undefined reference to '_ nslog'
Helloworld. m :(. Text + 0x93): Undefined reference to '_ objc_msg_lookup'
Helloworld. m :(. Text + 0xbc): Undefined reference to '___ objc_exec_class'
Helloworld. m :(. Data + 0x74): Undefined reference to '___ objc_class_name_nsautoreleasepool'
Helloworld. m :(. Data + 0x78): Undefined reference to '___ objc_class_name_nsconstantstring'
Collect2: LD returned 1 exit status

Fourth compilation:

Gcc-O helloworld. m \
-Fconstant-string-class = nsconstantstring \
-I/gnustep/system/library/headers /\
-L/gnustep/system/library/libraries /\
-Lobjc \
-Lgnustep-Base

Note: helloworld. m must appear before-lobjc and-lgnustep-base; otherwise, an error occurs.

At this time, some info prompts will appear, but no problem occurs. Finally, an executable file is generated and the result is displayed./helloworld.exe is executed.

Shortcut:

If you need to enter such a long command every time you use GCC, it is undoubtedly very annoying. We can make a shortcut:

Edit the c: \ gnustep \ bin \ GCC. Sh file with the following content:

#! /Bin/sh

If [$ #-ne 1]; then
Echo "Usage: $0 name"
Exit 1
Fi

Gcc-g-o $1 $ 1.m \
-Fconstant-string-class = nsconstantstring \
-I/gnustep/system/library/headers /\
-L/gnustep/system/library/libraries /\
-Lobjc \
-Lgnustep-Base

Exit 0

Among them, GCC has added the-G parameter to facilitate GDB debugging, and it is very convenient to use. Be sure not to include the extension m:

Gcc. Sh helloworld

Original article: http://hi.baidu.com/thinkinginlamp/blog/item/c38ca14412ce3d8bb3b7dc31.html

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.