Question

I have a problem using sqrt() in c.

I'm trying to do something like this:

int a;

a = sqrt(9);

etc

I'm restricted to use:

gcc -ansi -Wall -pedantic oneFile.c anotherFile.c thirdFile.c -o outputFileName

How can I make this compile without using the -lm command?

Yes, I have #include !

Is there any way around this and still use sqrt()?

Thanks

Was it helpful?

Solution 2

just try with this function, If you don`t want to use library.

Sq_root(n)
{
    count=0;
    int i = 0;
    for(i=1;sum<=n;i+=2)
    {
        sum+=i;
        count++;
    }
    return (count);
}

This will work, without any math library.

OTHER TIPS

You can't make it compile without -lm. That's an instruction to the linker to compile against the built-in math library. It isn't enough to say #include <math.h>, because that's only a header file - there's no code in there, that simply tells the compiler what the functions you're using look like. You still need to actually get the implementation of that function into your code, and -lm basically tells the linker look in libm, check to see if it has any functions that we haven't found yet. If you don't look in there, you'll never be able to actually execute sqrt because the code simply isn't in your program.

If you're working on a homework assignment and are restricted to using that command line, then it's possible that part of your assignment is to not use anything from the math library, and so you may be expected to consider an alternate approach.

use #include <math.h> in header

else use user define function

int int_sqrt(int x){
    int s, t;

    s = 1;  t = x;
    while (s < t) {
        s <<= 1;
        t >>= 1;
    }

    do {
        t = s;
        s = (x / s + s) >> 1;
    } while (s < t);

    return t;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top