Home > Blockchain >  How to fix undefined reference to pow in C Lion in Linux?
How to fix undefined reference to pow in C Lion in Linux?

Time:02-15

I have already taken a look at the question :Undefined reference to `pow' and `floor' but I'd like to know if there is anyway I could fix this permanently in my IDE which is Jet brains C Lion. I am using it on Pop OS. And I am very new to C as well as programming in general, it'd be better if I could just build and compile the program from my IDE rather than having to go to terminal every time.

Edit #1 : This is the code I am trying to compile rn. Its trying to find the reverse of a number rn.

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

int how_long(int x)
{
    int count = 0;

    while (x>0)
    {
        x /= 10;
        count  ;
    }

    return count;
}

int main()
{
    int n           = 1;
    int num            ;
    int reverse_num = 0;

    printf("Enter a number : ");
    scanf("%i",&num);

    int j = how_long(num);

    while (num>0)
    {
        reverse_num  = (num % 10) * (int)pow(10,j) ;
        num -= (num) * (int)pow(10,n-1);
          n;
        --j;
    }

    printf("%i",reverse_num);

    return 0;
}

And the error I get on C Lion is :

[1/1] Linking C executable 5_2
FAILED: 5_2 
: && /usr/bin/cc -g  CMakeFiles/5_2.dir/main.c.o -o 5_2   && :
/usr/bin/ld: CMakeFiles/5_2.dir/main.c.o: in function `main':
/home/koustubhjain/CLionProjects/Assignments/5-2/main.c:30: undefined reference to `pow'
/usr/bin/ld: /home/koustubhjain/CLionProjects/Assignments/5-2/main.c:31: undefined reference to `pow'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

Edit #2 : I found this old forum post on C Lion's website : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206607085-CLion-Enabling-math-h-for-C-projects

but idk if it still works, and if it works, how do I add that to my CMake file ?

Here is my CMakeLists.txt file for reference :

cmake_minimum_required(VERSION 3.21)
project(5_2 C)

set(CMAKE_C_STANDARD 99)

add_executable(5_2 main.c)

Edit #3 : Edit #2 seems to have fixed it

CodePudding user response:

Edit #2 in my original post seems to have fixed the problem

CodePudding user response:

You can do it simpler:

for(d=0;c;d =c,c/=10,d*=10);

At the end of this loop d/10 will be the reversed number (take care about the overflows).

  • Related