No compile error for undefined function

Discussion in 'C Programming' started by sam.s.kong@gmail.com, Dec 22, 2005.

  1. Guest

    Hello!

    I have a very basic question.

    a.c:
    <code>

    #include <stdio.h>

    main()
    {
    printf("%d\n", foo(3));
    }

    </code>

    I compiled it with
    gcc -c -o a.o a.c

    I expected some compile error but it passed.
    If I do like 'gcc a.c', it gives an error.
    undefined reference to 'foo'.

    Can it be compiled but not linked?

    TIA.

    Sam
    , Dec 22, 2005
    #1
    1. Advertising

  2. wrote:
    > ...
    > I compiled it with
    > gcc -c -o a.o a.c
    > ...
    > Can it be compiled but not linked?
    > ...


    Yes. That's exactly what '-c' option does for GCC.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Dec 22, 2005
    #2
    1. Advertising

  3. Default User Guest

    wrote:

    > Hello!
    >
    > I have a very basic question.
    >
    > a.c:
    > <code>
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > printf("%d\n", foo(3));
    > }
    >
    > </code>
    >
    > I compiled it with
    > gcc -c -o a.o a.c
    >
    > I expected some compile error but it passed.
    > If I do like 'gcc a.c', it gives an error.
    > undefined reference to 'foo'.
    >
    > Can it be compiled but not linked?


    Right. It assumes a declaration for foo(), as you didn't provide one.
    Naturally there isn't one defined anywhere, so the link fails.



    Brian
    Default User, Dec 22, 2005
    #3
  4. Guest

    Thanks for the reply.

    > Right. It assumes a declaration for foo(), as you didn't provide one.
    > Naturally there isn't one defined anywhere, so the link fails.


    I have an additional question.
    If declaration is not needed for compilation, what are header files
    for?
    I thought that header files provide declarations of external functions
    and data types.
    Could you teach me about that?

    Thanks.

    Sam
    , Dec 22, 2005
    #4
  5. pete Guest

    wrote:
    >
    > Thanks for the reply.
    >
    > > Right. It assumes a declaration for foo(), as you didn't provide one.
    > > Naturally there isn't one defined anywhere, so the link fails.

    >
    > I have an additional question.
    > If declaration is not needed for compilation, what are header files
    > for?
    > I thought that header files provide declarations of external functions
    > and data types.
    > Could you teach me about that?


    In C99 a declaration *is* required.
    In C89, if no declaration is provided,
    then the complier acts as if the function has been declared
    as returning type int.
    Something that typically happens with C89 compilers
    when malloc is used without #include <stdlib.h>,
    is that a warning will appear,
    telling the programmer to cast the return value of malloc,
    because without the proper declaration in scope,
    the compiler thinks that malloc returns type int,
    instead of type pointer to void.

    --
    pete
    pete, Dec 22, 2005
    #5
  6. Michael Mair Guest

    wrote:
    >
    >>Right. It assumes a declaration for foo(), as you didn't provide one.
    >>Naturally there isn't one defined anywhere, so the link fails.

    >
    > I have an additional question.
    > If declaration is not needed for compilation, what are header files
    > for?
    > I thought that header files provide declarations of external functions
    > and data types.
    > Could you teach me about that?


    For one thing, the compiler spontaneously makes up the function
    signature and assumes that the function returns int -- and the
    compiler may not guess right.

    If you provide a prototype(*) then the compiler knows the parameter
    types and the return type and can issue diagnostic messages if
    something is wrong and it can convert the arguments in a function
    call correctly to the parameter types.

    Have you read the FAQ? It may have to say something which further
    enlightens you.

    Example, based on your own:
    $ cat fooT.c
    #include <stdio.h>

    main()
    {
    printf("%d\n", foo(3));
    }
    $ cat foo.c
    double foo (float bar)
    {
    return 5000.0/(1.0 + (double)bar*bar);
    }
    $ gcc -std=c89 -pedantic -c -o fooT.o fooT.c
    $ gcc -std=c89 -pedantic -c -o foo.o foo.c
    $ gcc -o foo fooT.o foo.o
    $ ./foo
    0

    Somewhat unexpected, eh?
    FWIW:
    $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c
    fooT.c:4: warning: return type defaults to `int'
    fooT.c: In function `main':
    fooT.c:5: warning: implicit declaration of function `foo'
    fooT.c:6: warning: control reaches end of non-void function



    Cheers
    Michael
    ____
    (*) Note: The ... part of a variable argument list function behaves
    a little bit different (in fact, it behaves quite like a function
    call without prototype would behave).
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Dec 22, 2005
    #6
  7. Michael Mair <> wrote:

    > For one thing, the compiler spontaneously makes up the function
    > signature and assumes that the function returns int -- and the
    > compiler may not guess right.


    In C89. (We know that, but OP might not.)

    > $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c


    <ot>
    My gcc man page says -std=c89 is identical to -ansi - is there a
    particular reason you chose the former?
    </ot>

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 22, 2005
    #7
  8. Targeur fou Guest

    Christopher Benson-Manica wrote:
    > Michael Mair <> wrote:


    Hello,

    > > For one thing, the compiler spontaneously makes up the function
    > > signature and assumes that the function returns int -- and the
    > > compiler may not guess right.

    >
    > In C89. (We know that, but OP might not.)
    >
    > > $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

    >
    > <ot>
    > My gcc man page says -std=c89 is identical to -ansi - is there a
    > particular reason you chose the former?
    > </ot>


    <ot reply>

    Yes 'c89' is one letter shorter than 'ansi' ;-) and perhaps much
    clearer regarding the intent of making a compilation that complies with
    an international standard.

    </ot>

    A+
    Regis
    Targeur fou, Dec 22, 2005
    #8
  9. On 2005-12-22 15:21:21 +0100, Christopher Benson-Manica
    <> said:

    > Michael Mair <> wrote:
    >
    >> For one thing, the compiler spontaneously makes up the function
    >> signature and assumes that the function returns int -- and the
    >> compiler may not guess right.

    >
    > In C89. (We know that, but OP might not.)
    >
    >> $ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

    >
    > <ot>
    > My gcc man page says -std=c89 is identical to -ansi - is there a
    > particular reason you chose the former?
    > </ot>


    <also ot>
    I'd imagine that it is likely that in some future version of gcc,
    "-ansi" may equate to "-std=c99", instead of "-std=c89" (i.e. when C99
    becomes the "standard" standard version of C).
    </also ot>

    --
    Clark S. Cox, III
    Clark S. Cox III, Dec 22, 2005
    #9
  10. Michael Mair Guest

    Christopher Benson-Manica wrote:
    > Michael Mair <> wrote:
    >
    >>For one thing, the compiler spontaneously makes up the function
    >>signature and assumes that the function returns int -- and the
    >>compiler may not guess right.

    >
    > In C89. (We know that, but OP might not.)
    >
    >>$ gcc -std=c89 -pedantic -Wall -O -c -o fooT.o fooT.c

    >
    > <ot>
    > My gcc man page says -std=c89 is identical to -ansi - is there a
    > particular reason you chose the former?
    > </ot>


    Sorry for taking so long to answer (had some days off):
    -std=c89 instead of -ansi is IMO more concise and certainly
    more consistent as I also use -std=c99 (or, way OT, -std=c++98).
    Essentially, it comes down to personal preference.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Jan 3, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mantorok Redgormor
    Replies:
    70
    Views:
    1,753
    Dan Pop
    Feb 17, 2004
  2. Nagaraj
    Replies:
    1
    Views:
    855
    Lionel B
    Mar 1, 2007
  3. eric
    Replies:
    4
    Views:
    592
    Victor Bazarov
    Jun 9, 2011
  4. VK
    Replies:
    45
    Views:
    591
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    370
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page