0

Possible Duplicate:
Including files in C

I am using RunC to write a simple function which requires pow and floor/truncate. I included math.h. When I use the functions in the main, there is no problems. However, once I try to make a separate int function, suddenly RunC does not have the pow and floor functions and gives me an error. Any help?

Here is the code: main() works, but if I were to switch it to use the function above it doing the exact same thing, it will not work

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

int sumofsquares(int x){
   int counter = 0;
   int temp = x;

   while (temp != 0 || counter == 100){
      //temp = temp - (int)pow(floor(sqrt(temp)), 2);
      //temp = temp - pow(temp, 0.5);
      printf("%d\n", temp);
      counter = counter + 1;
   }

   /*while(temp != 0){
      temp = temp - (int)pow(floor(sqrt(temp)), 2);
      counter ++;
   }*/
    return counter;
}

int main(void){
   printf("%d", (int)pow(floor(sqrt(3)), 2));
}

doing this:

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

int sumofsquares(int x){
   int counter = 0;
   int temp = x;

   while(temp != 0){
      temp = temp - (int)pow(floor(sqrt(temp)), 2);
      counter ++;
   }
    return counter;
}

int main(void){
   printf("%d", sumofsquares(3));
}

returns this error:

/tmp/cctCHbmE.o: In function `sumofsquares':
/home/cs136/cs136Assignments/a04/test.c:9: undefined reference to `sqrt'
/home/cs136/cs136Assignments/a04/test.c:9: undefined reference to `floor'
collect2: ld returned 1 exit status
Community
  • 1
  • 1
  • Could you add what you've done so far ? – Emil Dumbazu Feb 22 '12 at 20:35
  • 2
    Are you not essentially asking the same thing here are you are in [your previous question](http://stackoverflow.com/questions/9399480/including-files-in-c)? – Bart Feb 22 '12 at 20:43
  • It would appear that your RunC environment is hosed. Might I ask why you use RunC rather than using the gcc that already exists on your ubuntu vm? – David Heffernan Feb 22 '12 at 21:00
  • It's a part of my university course. We are taught C in the RunC environment and are expected to use it. Currently, I have to use a VM for Ubuntu so I can code on my Windows laptop. – user1222282 Feb 22 '12 at 21:04
  • possibly an issue with RunC on Ubuntu? http://stackoverflow.com/questions/7824439/c-math-linker-problems-on-ubuntu-11-10 – ardnew Feb 22 '12 at 21:49

2 Answers2

0

Use gcc to compile the program:

gcc -lm -o foo foo.c
gmol
  • 15
  • 3
0

In your working main function, you have

printf("%d", (int)pow(floor(sqrt(3)), 2));

Note that the arguments here are constants. An optimising compiler will typically evaluate the expression at compile time and thus eliminate the calls to the math.h functions, thus it will work even without linking the math library. However, if the computation involves variables, it can usually not be evaluated at compile time, thus the calls to the math.h functions remain and without linking in the math library, linking would fail. Try

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

int main(int argc, char *argv[]) {
    // don't really use atoi, it's here just for shortness
    int num = argc > 1 ? atoi(argv[1]) : 3;
    printf("%d\n", (int)pow(floor(sqrt(num)),2));
    return EXIT_SUCCESS;
}

That should fail to link too if the math library is not specified to be linked in in the compiler command line.

In gcc, the command line should be

gcc -O3 -Wall -Wextra -o foo foo.c -lm

The libraries to be linked in should go last in the command line, since for many versions it won't work if they are specified before it's known which symbols are needed from them.

Unfortunately, I don't know RunC at all, so I can't tell you yet how to link in the math library with that, I'm trying to find out.

My google-fu is too weak. I haven't found any useful documentation on RunC, and I'm not going to install Ubuntu to check the tool itself.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431