undefined refrence to a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bobrics

    undefined refrence to a function

    Hi,

    I have been trying to compile some source code that is supposed to be
    working and getting some compilation errors. The errors are with
    sqrt(), log() and round() functions. I have math.h included and made
    sure it's in the path. I have read on the forums that some have fixed
    the problem by telling GCC to link math libraries if it's not done by
    default.

    So, using gcc -lm helped to resolve the problems with defining the
    first two functions. However, round() is still undefined: "undefined
    reference to round()"

    Another suggestion that I have found is to check whether the compiler
    is using C99 VS C90 library. This is quite stange that I have faced
    these problems while using GCC 4.0.2.

    What do you think about this? Why is this a problem? As far as I
    understood, round() has been defined in C99 and gcc 4.0.2 should have
    come with C99 libraries.

    Thank you

  • Ian Collins

    #2
    Re: undefined refrence to a function

    bobrics wrote:[color=blue]
    > Hi,
    >
    > I have been trying to compile some source code that is supposed to be
    > working and getting some compilation errors. The errors are with
    > sqrt(), log() and round() functions. I have math.h included and made
    > sure it's in the path. I have read on the forums that some have fixed
    > the problem by telling GCC to link math libraries if it's not done by
    > default.
    >
    > So, using gcc -lm helped to resolve the problems with defining the
    > first two functions. However, round() is still undefined: "undefined
    > reference to round()"
    >
    > Another suggestion that I have found is to check whether the compiler
    > is using C99 VS C90 library. This is quite stange that I have faced
    > these problems while using GCC 4.0.2.
    >
    > What do you think about this? Why is this a problem? As far as I
    > understood, round() has been defined in C99 and gcc 4.0.2 should have
    > come with C99 libraries.
    >[/color]
    Try a gcc list, round should be there.

    --
    Ian Collins.

    Comment

    • Ian Collins

      #3
      Re: undefined refrence to a function

      Ian Collins wrote:[color=blue]
      > bobrics wrote:
      >[color=green]
      >>Hi,
      >>
      >>I have been trying to compile some source code that is supposed to be
      >>working and getting some compilation errors. The errors are with
      >>sqrt(), log() and round() functions. I have math.h included and made
      >>sure it's in the path. I have read on the forums that some have fixed
      >>the problem by telling GCC to link math libraries if it's not done by
      >>default.
      >>
      >>So, using gcc -lm helped to resolve the problems with defining the
      >>first two functions. However, round() is still undefined: "undefined
      >>reference to round()"
      >>
      >>Another suggestion that I have found is to check whether the compiler
      >>is using C99 VS C90 library. This is quite stange that I have faced
      >>these problems while using GCC 4.0.2.
      >>
      >>What do you think about this? Why is this a problem? As far as I
      >>understood, round() has been defined in C99 and gcc 4.0.2 should have
      >>come with C99 libraries.
      >>[/color]
      >
      > Try a gcc list, round should be there.
      >[/color]
      Or a list for your platform, I don't have this problem with gcc on Solaris.

      --
      Ian Collins.

      Comment

      • bobrics

        #4
        Re: undefined refrence to a function

        What do you mean by gcc list?

        By the way, I have tried compiling the file using just command prompt
        and not Eclipse build as I was doing before and I've just got the
        following errors. Otherwise it works. I think -lm actually has fixed
        the problem:

        $ sudo gcc -lm my_file.c -o my_file
        my_file.c: In function 'my_function':
        my_file.c:314: warning: incompatible implicit declaration of built-in
        function 'round'
        my_file.c:314: warning: incompatible implicit declaration of built-in
        function 'roundf'
        my_file.c:314: warning: incompatible implicit declaration of built-in
        function 'roundl'

        How can I make this work in Eclipse now?
        I was trying to change the parameters that Eclipse is using to run gcc.
        In project.propert ies.C/C++Build window, I've changed command from gcc
        to 'gcc -lt', but I am still getting undefined references. Here are the
        values in ALL options text box, which is below command text box: '-O0
        -g3 -Wall -c -fmessage-length=0'. It seems like I cannot modify that.

        Thank you !





        Ian Collins wrote:[color=blue]
        > bobrics wrote:[color=green]
        > > Hi,
        > >
        > > I have been trying to compile some source code that is supposed to be
        > > working and getting some compilation errors. The errors are with
        > > sqrt(), log() and round() functions. I have math.h included and made
        > > sure it's in the path. I have read on the forums that some have fixed
        > > the problem by telling GCC to link math libraries if it's not done by
        > > default.
        > >
        > > So, using gcc -lm helped to resolve the problems with defining the
        > > first two functions. However, round() is still undefined: "undefined
        > > reference to round()"
        > >
        > > Another suggestion that I have found is to check whether the compiler
        > > is using C99 VS C90 library. This is quite stange that I have faced
        > > these problems while using GCC 4.0.2.
        > >
        > > What do you think about this? Why is this a problem? As far as I
        > > understood, round() has been defined in C99 and gcc 4.0.2 should have
        > > come with C99 libraries.
        > >[/color]
        > Try a gcc list, round should be there.
        >
        > --
        > Ian Collins.[/color]

        Comment

        • Keith Thompson

          #5
          Re: undefined refrence to a function

          "bobrics" <bobrics@gmail. com> writes:[color=blue]
          > I have been trying to compile some source code that is supposed to be
          > working and getting some compilation errors. The errors are with
          > sqrt(), log() and round() functions. I have math.h included and made
          > sure it's in the path. I have read on the forums that some have fixed
          > the problem by telling GCC to link math libraries if it's not done by
          > default.
          >
          > So, using gcc -lm helped to resolve the problems with defining the
          > first two functions. However, round() is still undefined: "undefined
          > reference to round()"
          >
          > Another suggestion that I have found is to check whether the compiler
          > is using C99 VS C90 library. This is quite stange that I have faced
          > these problems while using GCC 4.0.2.
          >
          > What do you think about this? Why is this a problem? As far as I
          > understood, round() has been defined in C99 and gcc 4.0.2 should have
          > come with C99 libraries.[/color]

          The round() function is new in C99; it's not in C90.

          This is somewhat OT, but gcc typically doesn't come with *any*
          library. It uses whatever library is provided by the underlying
          system. (On my Solaris 9 box, gcc finds round() but Sun's cc doesn't,
          so I'm probably missing something here myself.)

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Keith Thompson

            #6
            Re: undefined refrence to a function

            "bobrics" <bobrics@gmail. com> writes:[color=blue]
            > What do you mean by gcc list?[/color]

            A mailing list that discusses gcc. See <http://gcc.gnu.org/>.
            There's also a gnu.gcc.help newsgroup.
            [color=blue]
            > By the way, I have tried compiling the file using just command prompt
            > and not Eclipse build as I was doing before and I've just got the
            > following errors. Otherwise it works. I think -lm actually has fixed
            > the problem:
            >
            > $ sudo gcc -lm my_file.c -o my_file
            > my_file.c: In function 'my_function':
            > my_file.c:314: warning: incompatible implicit declaration of built-in
            > function 'round'
            > my_file.c:314: warning: incompatible implicit declaration of built-in
            > function 'roundf'
            > my_file.c:314: warning: incompatible implicit declaration of built-in
            > function 'roundl'
            >
            > How can I make this work in Eclipse now?[/color]

            I have no idea what Eclipse is.

            Here's what I *think* is happening.

            gcc uses your operating system's libraries and header files. Your
            system's <math.h> header doesn't support C99, so it doesn't declare
            round(). Your library presumably doesn't provide an implementation of
            the function either. But gcc recognizes the round() function (though
            it's not required to do so); it might even generate inline code for
            the call rather than a function call. Since there is no declaration
            of round() in <math.h>, you have a call with no visible prototype; the
            compiler is required to assume that it returns int.

            Since this invokes undefined behavior, I suppose the compile *could*
            generate correct code anyway, but I wouldn't count on it.

            Consider using trunc() rather than round().

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • Keith Thompson

              #7
              Re: undefined refrence to a function

              "bobrics" <bobrics@gmail. com> writes:[color=blue]
              > What do you mean by gcc list?
              >
              > By the way, I have tried compiling the file using just command prompt
              > and not Eclipse build as I was doing before and I've just got the
              > following errors. Otherwise it works. I think -lm actually has fixed
              > the problem:[/color]
              [snip]

              I forgot to mention: please don't top-post.

              See <http://www.caliburn.nl/topposting.html >.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Nils O. Selåsdal

                #8
                Re: undefined refrence to a function

                bobrics wrote:[color=blue]
                > Hi,
                >
                > I have been trying to compile some source code that is supposed to be
                > working and getting some compilation errors. The errors are with
                > sqrt(), log() and round() functions. I have math.h included and made
                > sure it's in the path. I have read on the forums that some have fixed
                > the problem by telling GCC to link math libraries if it's not done by
                > default.
                >
                > So, using gcc -lm helped to resolve the problems with defining the
                > first two functions. However, round() is still undefined: "undefined
                > reference to round()"[/color]
                Perhaps you havn't included the needed header files. Show some source code.
                [color=blue]
                > Another suggestion that I have found is to check whether the compiler
                > is using C99 VS C90 library. This is quite stange that I have faced
                > these problems while using GCC 4.0.2.[/color]

                [color=blue]
                > What do you think about this? Why is this a problem? As far as I
                > understood, round() has been defined in C99 and gcc 4.0.2 should have
                > come with C99 libraries.[/color]

                gcc is just the compiler, the libraries are provided elsewhere.

                (OT: to throw gcc in c99 mode, use the -std=c99 flag)

                Comment

                • bobrics

                  #9
                  Re: undefined refrence to a function

                  Nils O. Selåsdal wrote:[color=blue]
                  > bobrics wrote:[color=green]
                  > > Hi,
                  > >
                  > > I have been trying to compile some source code that is supposed to be
                  > > working and getting some compilation errors. The errors are with
                  > > sqrt(), log() and round() functions. I have math.h included and made
                  > > sure it's in the path. I have read on the forums that some have fixed
                  > > the problem by telling GCC to link math libraries if it's not done by
                  > > default.
                  > >
                  > > So, using gcc -lm helped to resolve the problems with defining the
                  > > first two functions. However, round() is still undefined: "undefined
                  > > reference to round()"[/color]
                  > Perhaps you havn't included the needed header files. Show some source code.
                  >[color=green]
                  > > Another suggestion that I have found is to check whether the compiler
                  > > is using C99 VS C90 library. This is quite stange that I have faced
                  > > these problems while using GCC 4.0.2.[/color]
                  >
                  >[color=green]
                  > > What do you think about this? Why is this a problem? As far as I
                  > > understood, round() has been defined in C99 and gcc 4.0.2 should have
                  > > come with C99 libraries.[/color]
                  >
                  > gcc is just the compiler, the libraries are provided elsewhere.
                  >
                  > (OT: to throw gcc in c99 mode, use the -std=c99 flag)[/color]

                  //#include <iostream.h> // originally made with cpp template
                  #include <stdio.h>
                  #include <assert.h>
                  #include <tgmath.h>
                  #include <math.h>

                  #include <stdlib.h>
                  #include <time.h>
                  ....

                  double somefunction(do uble k, double r, ...){
                  ....
                  j = (int) (round ((double) k / r));
                  // in for loop:
                  mu[j - 1] += r * log (r / sig) / (double) k;
                  ... etc..
                  }


                  So, when I ran directly from a command line, it works now, when I am
                  using -lm.
                  $: sudo gcc -lm mycodefile.c -o mycodefile

                  However, I don't know where exactly to add -lm option within Eclipse
                  development environment. I've tried adding it to the command line, so
                  before it was 'gcc', and now 'gcc -lm', but that haven't solved it.

                  Thank you

                  Comment

                  • Keith Thompson

                    #10
                    Re: undefined refrence to a function

                    "bobrics" <bobrics@gmail. com> writes:[color=blue]
                    > Nils O. Selåsdal wrote:[color=green]
                    >> bobrics wrote:[color=darkred]
                    >> > Hi,
                    >> >
                    >> > I have been trying to compile some source code that is supposed to be
                    >> > working and getting some compilation errors. The errors are with
                    >> > sqrt(), log() and round() functions. I have math.h included and made
                    >> > sure it's in the path. I have read on the forums that some have fixed
                    >> > the problem by telling GCC to link math libraries if it's not done by
                    >> > default.
                    >> >
                    >> > So, using gcc -lm helped to resolve the problems with defining the
                    >> > first two functions. However, round() is still undefined: "undefined
                    >> > reference to round()"[/color]
                    >> Perhaps you havn't included the needed header files. Show some source code.
                    >>[color=darkred]
                    >> > Another suggestion that I have found is to check whether the compiler
                    >> > is using C99 VS C90 library. This is quite stange that I have faced
                    >> > these problems while using GCC 4.0.2.[/color]
                    >>
                    >>[color=darkred]
                    >> > What do you think about this? Why is this a problem? As far as I
                    >> > understood, round() has been defined in C99 and gcc 4.0.2 should have
                    >> > come with C99 libraries.[/color]
                    >>
                    >> gcc is just the compiler, the libraries are provided elsewhere.
                    >>
                    >> (OT: to throw gcc in c99 mode, use the -std=c99 flag)[/color]
                    >
                    > //#include <iostream.h> // originally made with cpp template
                    > #include <stdio.h>
                    > #include <assert.h>
                    > #include <tgmath.h>
                    > #include <math.h>
                    >
                    > #include <stdlib.h>
                    > #include <time.h>
                    > ...
                    >
                    > double somefunction(do uble k, double r, ...){
                    > ...
                    > j = (int) (round ((double) k / r));
                    > // in for loop:
                    > mu[j - 1] += r * log (r / sig) / (double) k;
                    > .. etc..
                    > }
                    >
                    >
                    > So, when I ran directly from a command line, it works now, when I am
                    > using -lm.
                    > $: sudo gcc -lm mycodefile.c -o mycodefile[/color]

                    <OT>Why on Earth are you using sudo?</OT>
                    [color=blue]
                    > However, I don't know where exactly to add -lm option within Eclipse
                    > development environment. I've tried adding it to the command line, so
                    > before it was 'gcc', and now 'gcc -lm', but that haven't solved it.[/color]

                    You're casting the result of round() to suppress the compiler's
                    warning. That's a really bad idea. It's likely that your <math.h>
                    header doesn't declare the round() function, and that your compiler is
                    generating code that assumes round() returns an int. It's also
                    possible that the compiler does the right thing, but you shouldn't
                    count on it.

                    If you want a solution that's likely to work for your specific
                    environment, you can try declaring round() yourself:

                    double round(double x);

                    On implementations that provide a round() function and a proper
                    declaration of it in <math.h>, this isn't necessary. On
                    implementations that don't provide round(), it won't do any good.

                    You're dealing with a broken implementation; parts of it support the
                    new C99 round() function, and parts of it don't. Consider avoiding
                    the problem altogether by modifying the code to use ceil() and/or
                    floor(), perhaps wrapping them in your own my_round() function.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • Barry Schwarz

                      #11
                      Re: undefined refrence to a function

                      On 9 Apr 2006 12:11:28 +0100, "Nils O. Selåsdal"
                      <noselasd@asgaa rd.homelinux.or g> wrote:
                      [color=blue]
                      >bobrics wrote:[color=green]
                      >> Hi,
                      >>
                      >> I have been trying to compile some source code that is supposed to be
                      >> working and getting some compilation errors. The errors are with
                      >> sqrt(), log() and round() functions. I have math.h included and made
                      >> sure it's in the path. I have read on the forums that some have fixed
                      >> the problem by telling GCC to link math libraries if it's not done by
                      >> default.
                      >>
                      >> So, using gcc -lm helped to resolve the problems with defining the
                      >> first two functions. However, round() is still undefined: "undefined
                      >> reference to round()"[/color]
                      >Perhaps you havn't included the needed header files. Show some source code.[/color]

                      The error message is from the linker, not the compiler. Header files
                      won't solve the problem.


                      Remove del for email

                      Comment

                      • bobrics

                        #12
                        Re: undefined refrence to a function


                        Barry Schwarz wrote:[color=blue]
                        > On 9 Apr 2006 12:11:28 +0100, "Nils O. Selåsdal"
                        > <noselasd@asgaa rd.homelinux.or g> wrote:
                        >[color=green]
                        > >bobrics wrote:[color=darkred]
                        > >> Hi,
                        > >>
                        > >> I have been trying to compile some source code that is supposed to be
                        > >> working and getting some compilation errors. The errors are with
                        > >> sqrt(), log() and round() functions. I have math.h included and made
                        > >> sure it's in the path. I have read on the forums that some have fixed
                        > >> the problem by telling GCC to link math libraries if it's not done by
                        > >> default.
                        > >>
                        > >> So, using gcc -lm helped to resolve the problems with defining the
                        > >> first two functions. However, round() is still undefined: "undefined
                        > >> reference to round()"[/color]
                        > >Perhaps you havn't included the needed header files. Show some source code.[/color]
                        >
                        > The error message is from the linker, not the compiler. Header files
                        > won't solve the problem.
                        >
                        >
                        > Remove del for email[/color]

                        What del are you referring to?

                        Comment

                        • Richard Heathfield

                          #13
                          Re: undefined refrence to a function

                          bobrics said:
                          [color=blue]
                          >
                          > Barry Schwarz wrote:[color=green]
                          >>
                          >> Remove del for email[/color]
                          >
                          > What del are you referring to?[/color]

                          He is referring to three spurious characters inserted into his email
                          address, confounding spammers, for the purpose of.

                          Ldiekle this, you see.


                          --
                          Richard Heathfield
                          "Usenet is a strange place" - dmr 29/7/1999

                          email: rjh at above domain (but drop the www, obviously)

                          Comment

                          Working...