Thread: For Loop Question

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    5

    For Loop Question

    I am currently learning C and i came across an example on for loops but when i try to compile it, it gives me some weird errors, i wanted to know if someone can explain to me what these errors are and how to fix the code so that it works. Here is the code:
    Code:
    #include <stdio.h>
    
    int main(void){
    
         double angle;
         
         for(angle = 0.0; angle < 3.14159; angle += 0.2)
                                 printf("sine of %.1lf is %.2lf\n", angle, sin(angle));
    
    return 0;
    
    }
    
    
    
    
    Here is the error that i am getting when i try to compile it:
    
    /tmp/ccQOFy9U.o(.text+0x3f): In function `main':
    : undefined reference to `sin'
    collect2: ld returned 1 exit status
    Any help is very much appreciated.
    Last edited by Salem; 10-09-2004 at 02:24 PM. Reason: [code

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Go read the Announcements, and start using code tags.
    2) As the error states, it doesn't know what the functoin "sin" is. IE: You haven't written it, and you haven't linked to or included anything which references it. Read up on the function itself, and it will tell you what header it requires.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    gcc prog.c -lm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User orchid's Avatar
    Join Date
    Sep 2004
    Posts
    2
    : undefined reference to `sin'

    look at the arguments your passing to the printf function

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by orchid
    : undefined reference to `sin'

    look at the arguments your passing to the printf function
    The question has been answered. It's perfectly fine to pass sin, or any function which returns a value for that matter, to printf (or any other function wich takes arguments corresponding to the return value of the passed function:
    Code:
    #include <stdio.h>
    int foo( void )
    {
         return 5;
    }
    
    void bar( int x )
    {
        printf("The value of x is %d\n", x );
    }
    
    int baz( int x )
    {
        return  x;
    }
    
    int main( void )
    {
        printf("Like so: foo returns: %d\n", foo( ) );
        bar( foo( ) );
        bar( baz( foo( ) + foo( ) ) );
        return 0;
    }
    )

    The problem is, as stated: They have no sin function in their program. Hence, "undefined reference".

    [edit]Fixed momentary brain lapse. (Forgot what I was trying to show with baz.)[/edit]

    Quzah.
    Last edited by quzah; 10-09-2004 at 02:32 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User orchid's Avatar
    Join Date
    Sep 2004
    Posts
    2
    quzah, oh sorry, i didnt even see your first response :-\

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    Thank you all for your help, i found that i had to also include the math.h header file, but even when i did that i kept getting the same error, so i tried what salem said:

    gcc file.c -lm

    and it worked. What exactly does that "-lm" do?

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Can you do gcc /? or gcc -help ?

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    5
    I did that but i didn't see that option in there.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    -lm means link with math library.

    Hope that helps..

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For gcc it's "--help", which still doesn't say what -l is. Even using "-v --help" doesn't give it. What it does though, is tells the linker to link to the math library. How you're supposed to know that though...

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    -l isn't a compiler switch, it is a linker switch.
    The job of the compiler is to turn .c files into .o files (with help from the assembler).
    The job of the linker is to combine all your .o files with
    a) the startup code (which invokes your main())
    b) the standard library (say strlen())
    c) any other libraries you mention on the command line.

    Type
    ld --help
    to see all the linker command line options.

    Fortunately, you don't have to worry too much about all those options. The compiler driver will choose all the correct options for your environment.

    $ gcc -v -o myprog hello.c -lm
    Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
    Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
    --infodir=/usr/share/info --enable-shared --enable-threads=posix
    --disable-checking --host=i386-redhat-linux --with-system-zlib
    --enable-__cxa_atexit
    Thread model: posix
    gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/cc1 -lang-c -v -D__GNUC__=3
    -D__GNUC_MINOR__=2 -D__GNUC_PATCHLEVEL__=0 -D__GXX_ABI_VERSION=102 -D__ELF__
    -Dunix -D__gnu_linux__ -Dlinux -D__ELF__ -D__unix__ -D__gnu_linux__ -D__linux__
    -D__unix -D__linux -Asystem=posix -D__NO_INLINE__ -D__STDC_HOSTED__=1 -Acpu=i386
    -Amachine=i386 -Di386 -D__i386 -D__i386__ -D__tune_i386__ hello.c -quiet
    -dumpbase hello.c -version -o /tmp/ccsf4y3h.s
    GNU CPP version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (cpplib) (i386 Linux/ELF)
    GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) (i386-redhat-linux)
    compiled by GNU C version 3.2 20020903 (Red Hat Linux 8.0 3.2-7).
    ignoring nonexistent directory "/usr/i386-redhat-linux/include"
    #include "..." search starts here:
    #include <...> search starts here:
    /usr/local/include
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/include
    /usr/include
    End of search list.
    as -V -Qy -o /tmp/ccfyTDTs.o /tmp/ccsf4y3h.s
    GNU assembler version 2.13.90.0.2 (i386-redhat-linux) using BFD version
    2.13.90.0.2 20020802
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/collect2 --eh-frame-hdr -m elf_i386
    -dynamic-linker /lib/ld-linux.so.2 -o myprog
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crt1.o
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crti.o
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/crtbegin.o
    -L/usr/lib/gcc-lib/i386-redhat-linux/3.2
    -L/usr/lib/gcc-lib/i386-redhat-linux/3.2/../../.. /tmp/ccfyTDTs.o -lm -lgcc
    -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc-lib/i386-redhat-linux/3.2/crtend.o
    /usr/lib/gcc-lib/i386-redhat-linux/3.2/../../../crtn.o

    Your source file
    Your additional library
    The assembler file created by the compiler
    The object file created by the assembler
    The final name of your executable program
    The startup code which invokes main()

    "/usr/lib/gcc-lib/i386-redhat-linux/3.2/cc1" This is the compiler turning the .c file into a .s file
    "as" This is the assembler turning the .s file into a .o file
    "/usr/lib/gcc-lib/i386-redhat-linux/3.2/collect2" This is the linker mashing the whole lot together to produce a program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM