WindowsLowerObjective-CThis document describes how to build an environment.Objective-CFor details about the development environment, see this article.
1. Install four files. Please contact me directly.
2. test:
After the installation is complete, go to "start-Program-GNUstep-Shell". The window that appears is the shell window. You can edit vi/vim) and compile gcc) object-C code.
The default path of this shell is \ home \ <username>, for example,/home/samsung /.
In addition, I directly used devc ++ and UE to edit software editing programs and put them under/home/samsung/for compilation and operation.
The following is my code and running result:
Source code of hello. m:
- #import <stdio.h>
- int main(int argc,const char *argv[]){
- printf("hello world\n");
- return 0;
- }
Compile and run:
- samsung@coco ~
- $ gcc hello.m
- samsung@coco ~
- $ ls
- a.exe hello.m
- samsung@coco ~
- $ ./a.exe
- hello world
- samsung@coco ~
3. A more complex example:
Code: contains three files.
(1) Fraction. h:
- # Import <Foundation/NSObject. h>
-
- @ Interface Fraction: NSObject {
- Int numerator;
- Int denominator;
- }
-
- -(Void) print;
- -(Void) setNumerator: (int) d;
- -(Void) setDenominator: (int) d;
- -(Int) numerator;
- -(Int) denominator;
- -(Void) setNumerator: (int) n ddd: (int) d;
- -(Void) setNumerator: (int) n: (int) d :( int);
- // Here, three setNumerator functions are reloaded.
- @ End
(2) Fraction. m
- #import "Fraction.h"
- #import <stdio.h>
-
- @implementation Fraction
- -(void) print {
- printf( "%i/%i", numerator, denominator );
- }
-
- -(void) setNumerator: (int) n {
- nnumerator = n;
- }
-
- -(void) setDenominator: (int) d {
- ddenominator = d;
- }
-
- -(int) denominator {
- return denominator;
- }
-
- -(int) numerator {
- return numerator;
- }
-
- -(void) setNumerator: (int) n ddd: (int)d {
- nnumerator = n;
- ddenominator = d;
- }
- -(void) setNumerator: (int)n : (int)d :(int) a {
- nnumerator = n;
- ddenominator = d;
- printf("+++++a = %d +++ \n", a);
- }
- @end
(3) main. m
- # Import <stdio. h>
- # Import <Foundation/Foundation. h>
- # Import "Fraction. h"
-
- Int main (int argc, const char * argv []) {
- // Create a new instance
- Fraction * frac = [[Fraction alloc] init];
- Int x;
- Int y;
-
- // Set the values
- [Frac setNumerator: 1];
- [Frac setDenominator: 3];
-
- // Print it
- Printf ("The fraction is :");
-
- [Frac print];
- Printf ("\ n ");
- NSLog (@ "hello world !!! \ N "); // OK
- [Frac setNumerator: 34 ddd: 98];
- [Frac print];
- Printf ("\ n ");
- NSLog (@ "hello world !!! \ N "); // OK
- [Frac setNumerator: 44: 55: 66]; // OK
- [Frac print];
- Printf ("\ n ");
- Scanf ("% d", & x, & y); // test the scanf function, OK
- [Frac setNumerator: x ddd: y]; // OK
- [Frac print];
- // Free memory
- [Frac release];
- // [Frac release]; // the previous version has been release, so an exception occurs here: the program crashes.
- // Release the NULL pointer. Of course, it is not allowed.
- Return 0;
- }
Compilation Method:
(1) Compile main. m into main. o:
- gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers
2) Compile Fraction. m into Fraction. o:
- gcc -c Fraction.m -I /GNUstep/System/Library/Headers
3) Compile .ointo an executable program named main(the last generation is main.exe)
- gcc -o main main.o Fraction.o -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base
Note: There will be a warning, but you don't need to worry about it. After all, our executable program has been compiled.
Running result:
- samsung@coco ~/objc/fraction
- $ ./main.exe
- The fraction is: 1/3
-
- 2010-08-13 16:29:01.515 main[1212] hello world!!!
- 34/98
-
- 2010-08-13 16:29:01.515 main[1212] hello world world!!!
- +++++a = 66 +++
- 44/55
-
- 22 33
- 22/33
-
- samsung@coco ~/objc/fraction
4. Conclusion:
1. You can also use cygwin + GNUstep for development;
2. Objective-C is suffixed with m and compiled using gnu gcc;
3. The entire environment is similar to that of linux;
Summary:WindowsLowerObjective-CThe environment setup tutorial is complete. I hope this article will help you!