-1

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

Taddis
  • 156
  • 2
  • 12
  • what error are you getting? – Dayal rai Jul 31 '13 at 11:34
  • 1
    What is the exact problem, other than having "a" problem? – Jongware Jul 31 '13 at 11:34
  • Remember that `sqrt` takes a `double` for argument, and returns a `double`. You're calling it with an integer, and assigning it to an integer variable. – Some programmer dude Jul 31 '13 at 11:35
  • 3
    If the problem is that you get an undefined reference to `sqrt` when linking, it's because you don't link with the math library with `-lm`. The only way to solve this is to link with the math library with `-lm`. There's really no other way of solving it otherwise, except to write your own function. – Some programmer dude Jul 31 '13 at 11:37
  • `echo "#include " > test.c ; echo "int main() { int a; a = sqrt(9); return 0; }" >> test.c ; gcc -ansi -Wall -pedantic test.c` compiles without a hitch on MacOS and Ubuntu. What are you using? – Joachim Isaksson Jul 31 '13 at 11:41
  • @JoachimPileborg So what is the problem with passing an int to sqrt or assigning sqrt's result to an int lvalue, as long as math.h is included to provide a prototype? – Pascal Cuoq Jul 31 '13 at 11:41
  • @JoachimIsaksson The OP is using a platform where, unlike Mac OS X, the `-lm` flag is necessary to link in the math library. – Pascal Cuoq Jul 31 '13 at 11:42
  • possible duplicate of [sqrt from math.h causes compile error](http://stackoverflow.com/questions/1711915/sqrt-from-math-h-causes-compile-error) – Pascal Cuoq Jul 31 '13 at 11:43
  • What is the _actual error_ the compiler is giving you? – Joachim Isaksson Jul 31 '13 at 11:44
  • @PascalCuoq Assigning it to an `int` variable truncates the returned value, so if it's used in further calculations those may actually end up wrong. – Some programmer dude Jul 31 '13 at 11:48
  • What's the reason `-lm` is off limits? It seems a rather boneheaded restriction, if you're expected to use math functions... – cHao Jul 31 '13 at 22:19

4 Answers4

2

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.

Dan
  • 10,531
  • 2
  • 36
  • 55
0

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.

someone
  • 1,638
  • 3
  • 21
  • 36
0

use #include <math.h> in header

else use user define function

The Ray of Hope
  • 738
  • 1
  • 6
  • 16
0
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;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70