ld problem in sqrt function

This is a discussion on ld problem in sqrt function within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <math.h> int read_text(char *text, int text_max_size){ int i; char x; int length=0; scanf("%c", &x); ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    ld problem in sqrt function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int read_text(char *text, int text_max_size){
        int i;
        char x;
        int length=0;
            
            scanf("%c", &x);
            while(x != '"'){
                scanf("%c", &x);
            }
            for(i=0; i<text_max_size; i++){
                scanf("%c", &x);
                printf("x: %c\n", x);
                if( x == '"'){
                    break;
                }
                text[i] = x;
            }
    return i;    
    }
    
    void print_text(char *text, int n){
        int i;
            printf("Text: ");
            for(i=0; i<n; i++){
                printf("%c", text[i]);
            }
            printf("\n");
    }
    
    /*
    void store_text(char *text, int text_size, char **c_array, int n){
        int i;
    }
    */
    int main(void){
        int i, k, n;
        float root;
        char *plaintext, **ch, *ciphertext, *newplaintext;
            printf("Please type k (length): ");
            scanf("%d", &k);
            
            plaintext = (char *)malloc(k*sizeof(char));
            if(plaintext == NULL){
                printf("Error in allocating plaintext space.\n");
                exit(1);
            }
            printf("Type the text: ");
            k = read_text(plaintext, k);
            print_text(plaintext, k);
            
            if(k == 0){
                printf("There is no character...");
            }
            root = sqrt(k);
            
            if((root*2) == k){
                n = root;
            }else{
                n = ((int)root + 1);
            }
            printf("n: %d\n", n);
            
            ch = (char **)malloc(n*sizeof(char *));
            for(i=0; i<n; i++){
                ch[i] = (char *)malloc(n*sizeof(char));
            }
            
            if(ch == NULL){
                printf("Error in allocating ch space.\n");
                exit(1);
            }
            
            ciphertext = (char *)malloc((n^2)*sizeof(char));
            if(ciphertext == NULL){
                printf("Error in allocating ciphertext space.\n");
                exit(1);
            }
            
            newplaintext = (char *)malloc((n^2)*sizeof(char));
            if(newplaintext == NULL){
                printf("Error in allocating newplaintext space.\n");
                exit(1);
            }
            
            printf("Encoding in progres.....\n");
            //store_text(plaintext, k, ch, n);
    
    return 0;
    }
    and the output is something like this:
    Code:
    /tmp/ccWhTnJp.o: In function `main':
    ex1.c:(.text+0x1b2): undefined reference to `sqrt'
    collect2: ld returned 1 exit status
    I know that the wrong part is when calling the sqrt function...but i do not understand why math.h library does not support it...can anyone help? thanks guys...

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Add "-lm" in your commandline.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    thank you....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tough problem with function pointers
    By Montejo in forum C Programming
    Replies: 10
    Last Post: 12-22-2009, 01:17 AM
  2. Replies: 8
    Last Post: 10-29-2008, 07:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21