Objective-C environment setup tutorial in Windows

Source: Internet
Author: User

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:

 
 
  1. #import <stdio.h> 
  2. int main(int argc,const char *argv[]){  
  3. printf("hello world\n");  
  4. return 0;  

Compile and run:

 
 
  1. samsung@coco ~  
  2. $ gcc hello.m  
  3. samsung@coco ~  
  4. $ ls  
  5. a.exe  hello.m  
  6. samsung@coco ~  
  7. $ ./a.exe  
  8. hello world  
  9. samsung@coco ~ 

3. A more complex example:

Code: contains three files.

(1) Fraction. h:

 
 
  1. # Import <Foundation/NSObject. h>
  2.  
  3. @ Interface Fraction: NSObject {
  4. Int numerator;
  5. Int denominator;
  6. }
  7.  
  8. -(Void) print;
  9. -(Void) setNumerator: (int) d;
  10. -(Void) setDenominator: (int) d;
  11. -(Int) numerator;
  12. -(Int) denominator;
  13. -(Void) setNumerator: (int) n ddd: (int) d;
  14. -(Void) setNumerator: (int) n: (int) d :( int);
  15. // Here, three setNumerator functions are reloaded.
  16. @ End

(2) Fraction. m

 
 
  1. #import "Fraction.h"  
  2. #import <stdio.h> 
  3.  
  4. @implementation Fraction  
  5. -(void) print {  
  6.     printf( "%i/%i", numerator, denominator );  
  7. }  
  8.  
  9. -(void) setNumerator: (int) n {  
  10.     nnumerator = n;  
  11. }  
  12.  
  13. -(void) setDenominator: (int) d {  
  14.     ddenominator = d;  
  15. }  
  16.  
  17. -(int) denominator {  
  18.     return denominator;  
  19. }  
  20.  
  21. -(int) numerator {  
  22.     return numerator;  
  23. }  
  24.  
  25. -(void) setNumerator: (int) n ddd: (int)d {  
  26.     nnumerator = n;  
  27.     ddenominator = d;   
  28. }  
  29. -(void) setNumerator: (int)n : (int)d :(int) a {  
  30.         nnumerator = n;  
  31.         ddenominator = d;  
  32.         printf("+++++a = %d +++ \n", a);  
  33. }  
  34. @end 

(3) main. m

 
 
  1. # Import <stdio. h>
  2. # Import <Foundation/Foundation. h>
  3. # Import "Fraction. h"
  4.  
  5. Int main (int argc, const char * argv []) {
  6. // Create a new instance
  7. Fraction * frac = [[Fraction alloc] init];
  8. Int x;
  9. Int y;
  10.  
  11. // Set the values
  12. [Frac setNumerator: 1];
  13. [Frac setDenominator: 3];
  14.  
  15. // Print it
  16. Printf ("The fraction is :");
  17.  
  18. [Frac print];
  19. Printf ("\ n ");
  20. NSLog (@ "hello world !!! \ N "); // OK
  21. [Frac setNumerator: 34 ddd: 98];
  22. [Frac print];
  23. Printf ("\ n ");
  24. NSLog (@ "hello world !!! \ N "); // OK
  25. [Frac setNumerator: 44: 55: 66]; // OK
  26. [Frac print];
  27. Printf ("\ n ");
  28. Scanf ("% d", & x, & y); // test the scanf function, OK
  29. [Frac setNumerator: x ddd: y]; // OK
  30. [Frac print];
  31. // Free memory
  32. [Frac release];
  33. // [Frac release]; // the previous version has been release, so an exception occurs here: the program crashes.
  34. // Release the NULL pointer. Of course, it is not allowed.
  35. Return 0;
  36. }

Compilation Method:

(1) Compile main. m into main. o:

 
 
  1. gcc -fconstant-string-class=NSConstantString -c main.m -I /GNUstep/System/Library/Headers 

2) Compile Fraction. m into Fraction. o:

 
 
  1. gcc -c Fraction.m -I /GNUstep/System/Library/Headers 

3) Compile .ointo an executable program named main(the last generation is main.exe)

 
 
  1. 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:

 
 
  1. samsung@coco ~/objc/fraction  
  2. $ ./main.exe  
  3. The fraction is: 1/3  
  4.  
  5. 2010-08-13 16:29:01.515 main[1212] hello world!!!  
  6. 34/98  
  7.  
  8. 2010-08-13 16:29:01.515 main[1212] hello world world!!!  
  9. +++++a = 66 +++  
  10. 44/55  
  11.  
  12. 22 33  
  13. 22/33  
  14.  
  15. 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!

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.