logarithm of a variable in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James P
    New Member
    • Dec 2011
    • 5

    logarithm of a variable in C

    Hey, I have a problem with following code:


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

    int main() {
    int x;
    x = 10;
    printf("%.2f\n" , log(x));
    }

    when I try to compile it in GCC it shows an error message:
    In function `main':
    logarithm.c:(.t ext+0x19): undefined reference to `log'

    how can I fix it?

    Thans James
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    It compiles for me (using MinGW) which suggests a problem either with your compiler command line or with missing standard libraries or with the path to the libraries being wrong.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Are you using C or C++?
      Change the definition of x so that it is a double.

      C++ supports three different functions all named log. It uses magic to determine which of the three to use based on the type of the argument. If you're using C++ then perhaps passing an int argument fooled the magic into looking for a nonexistent fourth version of log.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        C++ would automatically convert the int to a float so I suspect Banfa is correct and a library has been left out of the build.

        Comment

        • James P
          New Member
          • Dec 2011
          • 5

          #5
          I'm using C, probably there's problem with compiler, when I try to compile and run the program in codeblocks everything was good, no errors

          Thanks alot for your responses

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Try adding the -lm switch to the compiler command line. This tells it to link in library m, which is actually a file named libm.a This is where the math library is typically located.

            Comment

            • James P
              New Member
              • Dec 2011
              • 5

              #7
              It doesn't work :(, if it helps I've got Ubuntu 11
              I don't know what's wrong

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Have you installed the build-essential package ?

                sudo apt-get install build-essential

                Comment

                • James P
                  New Member
                  • Dec 2011
                  • 5

                  #9
                  I haven't, thank you, I'll try and see if it will help

                  Comment

                  • James P
                    New Member
                    • Dec 2011
                    • 5

                    #10
                    it didn't help :(, but thank you very much for your responses

                    Comment

                    • nubera
                      New Member
                      • May 2013
                      • 1

                      #11
                      I don't know exactly why it helps, but it does: use an optimization flag (-O1..O3) when compiling:

                      Code:
                      gcc -O1 myprogram.c
                      Hope that helps in your case too. Cheers.

                      Comment

                      • johny10151981
                        Top Contributor
                        • Jan 2010
                        • 1059

                        #12
                        compile with -lm parmater
                        gcc -lm myprogram.c

                        somebody did the advice already :)

                        Comment

                        Working...