gcc in linux

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

    gcc in linux

    Hi again,

    I am new to compiling C programs in Linux/Unix. I wanted to compile a
    program by using gcc, but it turned out that gcc does not know where to
    locate the library being called.

    The program is as follows,

    #include <math.h>
    int main()
    {
    double ss;
    double ff;
    ss=1.0;
    ff=sin(ss);
    printf("%6.4f\n ",ff);
    return 0;
    }

    The error message is,

    /tmp/ccIV3yAb.o: In function `main':
    /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'

    The command I used to compile is
    gcc test.c

    Could anyone please let me know where I did wrong? Thanks!

    Fan


  • Nils O. Selåsdal

    #2
    Re: gcc in linux

    Fan Zhang wrote:
    [color=blue]
    > The error message is,
    >
    > /tmp/ccIV3yAb.o: In function `main':
    > /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'
    >
    > The command I used to compile is
    > gcc test.c
    >
    > Could anyone please let me know where I did wrong? Thanks![/color]
    You need to link with the math library which is called libm
    so,
    gcc test.c -lm
    comp.os.linux.d evelopment.apps or comp.unix.progr ammer
    is probably more appropriate for this Q though.

    Comment

    • Arthur J. O'Dwyer

      #3
      Re: gcc in linux


      On Tue, 12 Oct 2004, Fan Zhang wrote:[color=blue]
      >
      > /tmp/ccIV3yAb.o: In function `main':
      > /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'
      >
      > The command I used to compile is
      > gcc test.c[/color]

      This is practically word-for-word a FAQ.



      Read the whole thing.

      -Arthur

      Comment

      • E. Robert Tisdale

        #4
        Re: gcc in linux

        Fan Zhang wrote:[color=blue]
        >
        > I am new to compiling C programs in Linux/Unix.
        > I wanted to compile a program by using gcc
        > but it turned out that gcc does not know
        > where to locate the library being called.
        >
        > The program is as follows,
        >
        > #include <math.h>
        > int main()
        > {
        > double ss;
        > double ff;
        > ss=1.0;
        > ff=sin(ss);
        > printf("%6.4f\n ",ff);
        > return 0;
        > }
        >
        > The error message is,
        >
        > /tmp/ccIV3yAb.o: In function `main':
        > /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'
        >
        > The command I used to compile is
        > gcc test.c
        >
        > Could anyone please let me know where I did wrong? Thanks![/color]

        gcc test.c -lm

        Comment

        • Brian Gough

          #5
          Re: gcc in linux

          "Fan Zhang" <fanzhang@sas.u penn.edu> writes:
          [color=blue]
          > I am new to compiling C programs in Linux/Unix. I wanted to compile a
          > program by using gcc, but it turned out that gcc does not know where to
          > locate the library being called. The command I used to compile is
          > gcc test.c. Could anyone please let me know where I did wrong? Thanks![/color]

          See http://www.network-theory.co.uk/docs...cintro_16.html
          for information on linking with external libraries with gcc.

          --
          Brian Gough

          Network Theory Ltd,
          Publishing "An Introduction to GCC" --- http://www.network-theory.co.uk/gcc/

          Comment

          • Dan Pop

            #6
            Re: gcc in linux

            In <87u0szvsd8.fsf @network-theory.co.uk> Brian Gough <bjg@network-theory.co.uk> writes:
            [color=blue]
            >"Fan Zhang" <fanzhang@sas.u penn.edu> writes:
            >[color=green]
            >> I am new to compiling C programs in Linux/Unix. I wanted to compile a
            >> program by using gcc, but it turned out that gcc does not know where to
            >> locate the library being called. The command I used to compile is
            >> gcc test.c. Could anyone please let me know where I did wrong? Thanks![/color]
            >
            >See http://www.network-theory.co.uk/docs...cintro_16.html
            >for information on linking with external libraries with gcc.[/color]

            Except that the sin function has nothing to do with *any* external
            library: it's part of the standard C library.

            Dan
            --
            Dan Pop
            DESY Zeuthen, RZ group
            Email: Dan.Pop@ifh.de
            Currently looking for a job in the European Union

            Comment

            • Jack Klein

              #7
              Re: gcc in linux

              On Tue, 12 Oct 2004 18:25:30 -0400, "Fan Zhang"
              <fanzhang@sas.u penn.edu> wrote in comp.lang.c:
              [color=blue]
              > Hi again,
              >
              > I am new to compiling C programs in Linux/Unix. I wanted to compile a
              > program by using gcc, but it turned out that gcc does not know where to
              > locate the library being called.[/color]

              Linux is not UNIX, UNIX is not Linux.
              [color=blue]
              > The program is as follows,
              >
              > #include <math.h>
              > int main()
              > {
              > double ss;
              > double ff;
              > ss=1.0;
              > ff=sin(ss);
              > printf("%6.4f\n ",ff);
              > return 0;
              > }
              >
              > The error message is,
              >
              > /tmp/ccIV3yAb.o: In function `main':
              > /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'
              >
              > The command I used to compile is
              > gcc test.c
              >
              > Could anyone please let me know where I did wrong? Thanks!
              >
              > Fan[/color]

              Despite the fact that Linux is not UNIX, gcc on Linux continues the
              old UNIX tradition of pretending that part of the standard C library
              is not part of the standard C library.

              UNIX elitists do not like to make things easy for the uninitiated,
              unwashed masses, even when the reason for a particular piece of
              stupidity has been obsolete for 20 years.

              --
              Jack Klein
              Home: http://JK-Technology.Com
              FAQs for
              comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
              comp.lang.c++ http://www.parashift.com/c++-faq-lite/
              alt.comp.lang.l earn.c-c++

              Comment

              • Darklight

                #8
                Re: gcc in linux

                Jack Klein wrote:
                [color=blue]
                > On Tue, 12 Oct 2004 18:25:30 -0400, "Fan Zhang"
                > <fanzhang@sas.u penn.edu> wrote in comp.lang.c:
                >[color=green]
                >> Hi again,
                >>
                >> I am new to compiling C programs in Linux/Unix. I wanted to compile a
                >> program by using gcc, but it turned out that gcc does not know where to
                >> locate the library being called.[/color]
                >
                > Linux is not UNIX, UNIX is not Linux.
                >[color=green]
                >> The program is as follows,
                >>
                >> #include <math.h>
                >> int main()
                >> {
                >> double ss;
                >> double ff;
                >> ss=1.0;
                >> ff=sin(ss);
                >> printf("%6.4f\n ",ff);
                >> return 0;
                >> }
                >>
                >> The error message is,
                >>
                >> /tmp/ccIV3yAb.o: In function `main':
                >> /tmp/ccIV3yAb.o(.tex t+0x1e): undefined reference to `sin'
                >>
                >> The command I used to compile is
                >> gcc test.c[/color][/color]

                try this gcc -Wall -lm test.c

                Comment

                Working...