1

Got my notebook reset today, re-installed Ubuntu and got this strange problem... Everytime I try to run pow, sin, cos and tan it gives me this warning in the terminal:

/c/"testmath
/usr/bin/ld: /tmp/ccauzwsW.o: in function `main':
testmath.c:(.text+0x34): undefined reference to `pow'
/usr/bin/ld: testmath.c:(.text+0x47): undefined reference to `sin'
/usr/bin/ld: testmath.c:(.text+0x5a): undefined reference to `cos'
/usr/bin/ld: testmath.c:(.text+0x6d): undefined reference to `tan'
collect2: error: ld returned 1 exit status

I have no idea what could've caused it. Also, when I insert a value directly in the function like y = pow(100, 2) it appears in the terminal perfectly, the problem really occurs when I reference other variable, like sqrt(x) for example.

Here's the code I'm using for tests:

#include <stdio.h>
#include <math.h>

int main(void){
    float z ,x = 9.0, y, k, l, m ;
    y = sqrt(9.0);
    z = pow(x , 2);
    k = sin(x);
    m = cos(x);
    l = tan(x);

    printf("\nThis is the sqrt: %f\n", y);
    printf("\nThis is the pow: %f\n", z);
    printf("\nThis is the sin: %f\n", k);
    printf("\nThis is the cos: %f\n", m);
    printf("\nThis is the tan: %f\n", l);


    return 0;
}

I'm currently running Ubuntu 20.04 x64 and VSCode with the C/C++ extensions, if that helps.

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
  • 1
    Put `-lm` _after_ all the object files you created. If you included the maths library, you probably included it _before_ the object files, which leads to the problem you're seeing. – Jonathan Leffler Nov 12 '20 at 22:06
  • @WhozCraig I did a cntrl+a cntrl+c cntrl+v in the code :(. Can you explain what this build line is, please ? – Leonardo Moreno Nov 12 '20 at 22:08
  • You need to add the maths library to the list of libraries used by your program/project. Quite how you do that requires knowledge of Visual Studio Code, though. The key sequence you describe has no meaning outside of the IDE you're using. – Jonathan Leffler Nov 12 '20 at 22:10

0 Answers0