Thursday, September 6, 2012

Common Objective-C error!!


Recently I started learning Objective-C for my new project. Whatever the errors I am getting while compiling, I am sharing here, so that it will be helpfull to others. For Installing Objective-C in windows, see this post. Below are the some of the common errors you may face while compiling Objective-C program in winodws.

$gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries hello.m -o hello -lgnustep-base -lobjc
sh: gcc: command not found 
you may get above error, because system unable to get the gcc path. For this, we need to install devel part in GNUstep installation process (It contains three parts System, Core and devel). The devel binary sets the gcc compilar path, if you forgot to install the devel, you may see above problem.

$ gcc 'gnustep-config --objc-flags' -L /GNUstep/System/Library/Libraries hello.m -o hello -lgnustep-base -lobjc
gcc.exe: error: gnustep-config --objc-flags: No such file or directory
you may get this error, because instead of back-tick(`), you used single quotes(') in the gcc command for gnustep-config parameter.

$ gcc  -L /GNUstep/System/Library/Libraries hello.m -o hello -lgnustep-base -lobjc
hello.m:1:34: fatal error: Foundation/Foundation.h: No such file or directory compilation terminated
you may get the above error, becaus you have not specified the path for the Foundation.h file. we need to specify gnustep-config flag to find the path for the Foundation.h file.

$ gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries hello.m -o hello
C:\Users\CPASUM~1\AppData\Local\Temp\ccifGII6.o:hello.m:(.data+0x0): undefined reference to `__objc_class_name_NSConstantString'
C:\Users\CPASUM~1\AppData\Local\Temp\ccifGII6.o:hello.m:(.data+0x4): undefined reference to `__objc_class_name_NSAutoreleasePool'
AppData\Local\Temp\ccifGII6.o: In function `main':
hello.m:5: undefined reference to `objc_get_class'
hello.m:5: undefined reference to `objc_msg_lookup'
hello.m:5: undefined reference to `objc_msg_lookup'
hello.m:7: undefined reference to `_imp__NSLog'
hello.m:8: undefined reference to `objc_msg_lookup'
ccifGII6.o: In function `_objc_gnu_init':
hello.m:10: undefined reference to `__objc_exec_class'
collect2: ld returned 1 exit status
you may get the above error, because you not specified the library of the objectiveC. This is similar to specifying the math library when you use mathematical functions like sqrt library functions.


$ gcc `gnustep-config --objc-flags` hello.m -o hello -lgnustep-base -lobjc
c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lgnustep-base
c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lobjc
collect2: ld returned 1 exit status
you may get the above error, becaus you have not specified the gcc library. you need to specify this library becaus , GNUstep runs on MinGW.

These are the some of the common errors. I will update this, whenever I got new errors.

Wednesday, September 5, 2012

objective c compiler for windows

We can install the Objective-C compiler on windows platform using GNUstep. GNUstep is a free and open version of cocoa API's and tools for different platforms. GNUstep environment can be made in windows using a toolkit MinGW. MinGW stands for Minimal GNU for Windows. And it is a GNU compiler collection which includes Ojective-C compiler as well. GNUstep package itself contains MinGW package, so there is no need to install MinGW package.

Installation of GNUstep: 
  • Download the windows installation package which includes three System, Core and Dev exe files from official website.
  • Install the packages System, Core and Dev in order.
And thats it, you have done the GNUstep environment in your system. To run the Objective-C program/application, go to Programs->GNUStep and there you can find Shell, just click on Shell you will get the Comman Line Interface. This shell is based on MinGW package. So you can able to compile the Objective-C applications/programs from this shell.

Compiling and running Objective C program on Windows: Create the HelloWorld program with below code in any text editor.
#import <Foundation/Foundation.h> 
int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSLog (@"Hello World!");
  [pool drain];
  return 0;
}
save the file as hello.m and go to the shell and the compile the file with following command.
gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries 
hello.m -o hello -lgnustep-base -lobjc
If every thing goes fine, it will generate hello.exe file, because we specified the object file name as hello. If we are not using -o option, it will create a.exe file which is similar to a.out file. To execute the program, run the below command from the shell.
$ ./hello.exe
2012-09-05 11:57:49.615 hello[1168] Hello World!

Click here for what are the possible errors with gcc for objective-C and  why.

Monday, September 3, 2012

delete node from binary search tree!!

One of the complex operation on binary search tree is deleting a node. Insertion is easy by calling recursive insertion. But deletion wont work with recursive. We need to handle different cases for deletinga node. Lets see all possible cases one by one in detail.

Finding the node to delete: First step for deleting a node is to find the node in the tree. If the node is not in the tree, no need of delete. So below is the code snippet for finding the node.

current = root;
while(current!=NULL)
{
    if(current->info == key)
    {
        found = 1;
        break;
    }
    else
    {
        prev = current;
        if(current->info >= key)
            current = current->left;
        else
            current = current->right;
    }
}
Here we are maintaining two pointers prev and current, so that we can have the link to maintain. After finding the node to delete, there are four possible cases. See one by one.

Case1: Current node has zero child's as shown in below image. current node is either left or right child of the prev node.

deleting a node from binary search tree
we can simply remove the current node for the scenario which shown in the image. the code snippet is given below.
if((current->left == NULL) && (current->right == NULL))
{
    if(prev->left == current)
        prev->left = NULL;
    else
        prev->right = NULL;
    free(current);
}


Case2: Current node has one child either left or right.
Case2-A: Current node with right child as shown in the below image. There are two possibilities as shown in the below image.

delete node from binary search tree
See the sample code snippet below
if(current->left == NULL && current->right != NULL)
{
    if(prev->left == current)
        prev->left = current->right;
    else
        prev->right = current->right;
    free (current);
}

Case2-B: Current node with left child as shown in the below image. There are two possibilities as shown in the below image.
delete a node from binary search tree
See the sample code snippet below.
if(current->right == NULL && current->left!=NULL)
{
        if(prev->left = current)
            prev->left = current->left;
        else
            prev->right = current->left;
        free(current);
}

Case 3: Current node with two child's as shown in the below image. There are again four possibilities as shown in the below images. These four cases we need to handle carefully.
Case3-A: If the current node right child has no children. The scenario is shown below.

We need to copy the tmp node data to current node and delete the tmp node.
Case3-B: If the current node has left child. This is the tricky one. If the left child is available (we can ignore whether right child is available or not), we need to find the smallest element in the left side and replace it with current node and delete the smallest node. To find the smallest element, just find the left most node from the tmp node. This is to satisfy the binary search tree property.
The sample code snippet is given below.
Case3-C: If the current node has only right child. The scenario is as shown in the image.
delete node from binary search tree different cases
 We need to copy the tmp data to current node and delete the tmp node. the code snippet is given below.
if(current->left != NULL && current->right != NULL)
{
    struct bst *tmp = current->right;
 //case3-A
    if(tmp->left == NULL && tmp->right == NULL)
    {
        current->info = tmp->info;
        //current = tmp;
        free(tmp);
        current->right = NULL;
    }
 //case3-B
    else if(current->right->left != NULL)
    {
        struct bst *left_current = current->right;
        struct bst *left_current_prev = current->right->left;
        while(left_current->left != NULL)
        {
            left_current_prev = left_current;
            left_current = left_current->left;
        }
        current->info = left_current->info;
        free(left_current);
        left_current_prev->left = NULL;
    }
 //case3-C
    else
    {
        struct bst *temp;
        temp = current->right;
        current->info = temp->info;
        current->right = temp->right;
        free(temp);
    }
}

Click here for the complete Binary search tree C program.

Popular Posts