Undefined reference

Discussion in 'C Programming' started by wdh3rd@gmail.com, Apr 6, 2007.

  1. Guest

    I'm still new at C and can't solve this problem. I've looked through
    the FAQ and on the Web, but am not having luck.

    I'm getting an "undefined reference" error as well as a "Id returned 1
    exit status" error.

    I've pared down the code to a simple example:

    ---------------------------------square.c
    ---------------------------------------------------

    #include "square.h"

    main() {

    static int square (int a)
    {
    return a * a;
    }

    int sq_plus (int a, int b)
    {
    return square(a) - b;
    }

    } /* end main */

    -----------------------------------------------------
    square.h---------------------------------

    #include <stdio.h>

    int sq_plus(int a, int b);

    ----------------------------------------
    main.c----------------------------------------------------
    #include <stdio.h>
    #include "square.h"

    int main()
    {
    int a,b;

    printf( "Enter two digits: " );
    scanf( "%d%d", &a, &b );
    printf( "Given %d and %d, squarePlus is %d", a, b,
    square(a,b) );
    return 0;
    }

    ------------------------------------------------------------------------------------------------

    Some questions:

    (1) I thought that main() was only supposed to be in the main function
    file, but if I don't have a main() in square.c, I get errors.

    (2) I don't understand why I'm getting the undefined reference error.
    I'm using the Dev compiler and it seems that it is ANSI-compatible.
    There were supposedly issues with non-ANSI-compatible compilers, but
    that doesn't seem to be the issue. The problem may be as described in
    this FAS:

    "In the general case of calling code in an external library, using
    #include to pull in the right header file(s) is only half of the
    story; you also have to tell the linker to search the external library
    itself. The declarations in the header file only tell the compiler how
    to call the external functions; the header file doesn't supply the
    definitions of the external functions, or tell the compiler/linker
    where to find those definitions.

    In some cases (especially if the functions are nonstandard) obtaining
    those definitions may require explicitly asking for the correct
    libraries to be searched when you link the program. (Some systems may
    be able to arrange that whenever you #include a header, its associated
    library, if nonstandard, is automatically requested at link time, but
    such a facility is not widespread.)"

    If that is the solution to my problem, I'm still confused on how to
    "tell the linker to search the external library itself. "

    (3) Does the "ld returned 1 exit status" error go away when the
    undefined reference error is solved as I'm assuing it does?
    , Apr 6, 2007
    #1
    1. Advertising

  2. Guest

    The line in main.c should actually read:

    printf( "Given %d and %d, squarePlus is %d", a, b,
    sq_plus(a,b) );

    instead of

    printf( "Given %d and %d, squarePlus is %d", a, b,
    square(a,b) );
    , Apr 6, 2007
    #2
    1. Advertising

  3. Ian Collins Guest

    wrote:
    > I'm still new at C and can't solve this problem. I've looked through
    > the FAQ and on the Web, but am not having luck.
    >
    > I'm getting an "undefined reference" error as well as a "Id returned 1
    > exit status" error.
    >

    You'll have to read your compiler documentation to see how to compile
    more than one source module into a single executable. Generally,

    $CC=your compiler

    $CC main.c square.c

    should be enough to get you going.

    --
    Ian Collins.
    Ian Collins, Apr 6, 2007
    #3
  4. Guest

    On Apr 5, 6:26 pm, Ian Collins <> wrote:
    > wrote:
    > > I'm still new at C and can't solve this problem. I've looked through
    > > the FAQ and on the Web, but am not having luck.

    >
    > > I'm getting an "undefined reference" error as well as a "Id returned 1
    > > exit status" error.

    >
    > You'll have to read your compiler documentation to see how to compile
    > more than one source module into a single executable. Generally,
    >
    > $CC=your compiler
    >
    > $CC main.c square.c
    >
    > should be enough to get you going.
    >
    > --
    > Ian Collins.


    I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
    there. Shouldn't the IDE know what to do if it's compiling a Main
    file?

    Sice it's not command line compilation, I thought it would know what
    to do.
    , Apr 6, 2007
    #4
  5. Ian Collins Guest

    wrote:
    > On Apr 5, 6:26 pm, Ian Collins <> wrote:
    >
    >> wrote:
    >>
    >>>I'm still new at C and can't solve this problem. I've looked through
    >>>the FAQ and on the Web, but am not having luck.

    >>
    >>>I'm getting an "undefined reference" error as well as a "Id returned 1
    >>>exit status" error.

    >>
    >>You'll have to read your compiler documentation to see how to compile
    >>more than one source module into a single executable. Generally,
    >>
    >>$CC=your compiler
    >>
    >>$CC main.c square.c
    >>
    >>should be enough to get you going.
    >>

    *Please don't quote signatures.
    >
    > I'm using the Dev-C++ IDE. I'm just pressing the "Compile" button on
    > there. Shouldn't the IDE know what to do if it's compiling a Main
    > file?
    >

    Can't help you there, you had better ask somewhere where the IDE (or
    your platform) is topical.

    --
    Ian Collins.
    Ian Collins, Apr 6, 2007
    #5
  6. Guest


    > Can't help you there, you had better ask somewhere where the IDE (or
    > your platform) is topical.
    >
    > --
    > Ian Collins.


    I had gcc'd through telnet and gotten the same error, so I figured it
    was an error in what I'd coded in general, and not a compiler-specific
    issue.
    , Apr 6, 2007
    #6
  7. On 5 Apr 2007 16:09:42 -0700, wrote:

    >I'm still new at C and can't solve this problem. I've looked through
    >the FAQ and on the Web, but am not having luck.
    >
    >I'm getting an "undefined reference" error as well as a "Id returned 1
    >exit status" error.
    >
    >I've pared down the code to a simple example:
    >
    >---------------------------------square.c
    >---------------------------------------------------
    >
    >#include "square.h"
    >
    > main() {


    Delete this and its corresponding } because ...

    >
    > static int square (int a)
    > {


    You are not allowed to define one function within another. Did your
    compiler not issue a diagnostic here.

    > return a * a;
    > }
    >
    > int sq_plus (int a, int b)
    > {
    > return square(a) - b;
    > }
    >
    > } /* end main */
    >
    >-----------------------------------------------------
    >square.h---------------------------------
    >
    >#include <stdio.h>


    Why? Neither function in square.c needs it and you manually include
    it in main.c

    >
    >int sq_plus(int a, int b);
    >
    >----------------------------------------
    >main.c----------------------------------------------------
    >#include <stdio.h>
    > #include "square.h"
    >
    > int main()


    int main(void) is more complete.

    > {
    > int a,b;
    >
    > printf( "Enter two digits: " );
    > scanf( "%d%d", &a, &b );
    > printf( "Given %d and %d, squarePlus is %d", a, b,
    >square(a,b) );


    square is an unknown identifier at this point. square.h declares
    sq_plus but not square.

    > return 0;
    >}
    >
    >------------------------------------------------------------------------------------------------
    >
    >Some questions:
    >
    >(1) I thought that main() was only supposed to be in the main function
    >file, but if I don't have a main() in square.c, I get errors.


    It doesn't matter where it is defined but there should be only one
    main.

    What errors? Show the exact code and diagnostic.

    >
    >(2) I don't understand why I'm getting the undefined reference error.
    >I'm using the Dev compiler and it seems that it is ANSI-compatible.
    >There were supposedly issues with non-ANSI-compatible compilers, but
    >that doesn't seem to be the issue. The problem may be as described in
    >this FAS:
    >
    >"In the general case of calling code in an external library, using
    >#include to pull in the right header file(s) is only half of the
    >story; you also have to tell the linker to search the external library
    >itself. The declarations in the header file only tell the compiler how
    >to call the external functions; the header file doesn't supply the
    >definitions of the external functions, or tell the compiler/linker
    >where to find those definitions.
    >
    >In some cases (especially if the functions are nonstandard) obtaining
    >those definitions may require explicitly asking for the correct
    >libraries to be searched when you link the program. (Some systems may
    >be able to arrange that whenever you #include a header, its associated
    >library, if nonstandard, is automatically requested at link time, but
    >such a facility is not widespread.)"
    >
    >If that is the solution to my problem, I'm still confused on how to
    >"tell the linker to search the external library itself. "


    Since your square.c has major errors, there is no telling what is
    actually in the object file the compiler builds from it.

    How you tell your linker to find private object code is a detail of
    your implementation. Check your documentation, help file, man page,
    whatever.

    >
    >(3) Does the "ld returned 1 exit status" error go away when the
    >undefined reference error is solved as I'm assuing it does?


    Another detail of your implementation.


    Remove del for email
    Barry Schwarz, Apr 6, 2007
    #7
  8. Daniel Rudy Guest

    At about the time of 4/5/2007 4:09 PM, stated the
    following:
    > I'm still new at C and can't solve this problem. I've looked through
    > the FAQ and on the Web, but am not having luck.
    >
    > I'm getting an "undefined reference" error as well as a "Id returned 1
    > exit status" error.
    >
    > I've pared down the code to a simple example:
    >
    > ---------------------------------square.c
    > ---------------------------------------------------
    >
    > #include "square.h"
    >
    > main() {
    >
    > static int square (int a)
    > {
    > return a * a;
    > }
    >
    > int sq_plus (int a, int b)
    > {
    > return square(a) - b;
    > }
    >
    > } /* end main */
    >
    > -----------------------------------------------------
    > square.h---------------------------------
    >
    > #include <stdio.h>
    >
    > int sq_plus(int a, int b);
    >
    > ----------------------------------------
    > main.c----------------------------------------------------
    > #include <stdio.h>
    > #include "square.h"
    >
    > int main()
    > {
    > int a,b;
    >
    > printf( "Enter two digits: " );
    > scanf( "%d%d", &a, &b );
    > printf( "Given %d and %d, squarePlus is %d", a, b,
    > square(a,b) );
    > return 0;
    > }
    >


    Try this:

    #include <stdio.h>

    int square(int a);
    int sq_plus(int a, int b);


    int main(void)
    {
    int a, b;

    printf("Enter two digits: ");
    scanf("%d%d", &a, &b);
    printf("Given %d and %d, squarePlus is %d\n", a, b, sq_plus(a, b));
    return(0);
    }

    int square(int a)
    {
    return(a * a);
    }

    int sq_plus(int a, int b)
    {
    return(square(a) - b);
    }

    As for your specific environment, you will need to ask in a
    platform/compiler specific forum.

    --
    Daniel Rudy

    Email address has been base64 encoded to reduce spam
    Decode email address using b64decode or uudecode -m

    Why geeks like computers: look chat date touch grep make unzip
    strip view finger mount fcsk more fcsk yes spray umount sleep
    Daniel Rudy, Apr 6, 2007
    #8
  9. <> wrote in message
    news:...
    > I'm still new at C and can't solve this problem. I've looked through
    > the FAQ and on the Web, but am not having luck.
    >
    > I'm getting an "undefined reference" error as well as a "Id returned 1
    > exit status" error.
    >
    > I've pared down the code to a simple example:


    That example is very, very broken. Try this:

    /* square.h */
    int sq_plus (int a, int b);

    /* square.c */
    #include "square.h"

    static int square (int a) {
    return a * a;
    }

    int sq_plus (int a, int b) {
    return square(a) - b;
    }

    /* main.c */

    #include <stdio.h>
    #include "square.h"

    int main() {
    int a,b;

    printf( "Enter two digits: " );
    scanf( "%d%d", &a, &b );
    printf( "Given %d and %d, squarePlus is %d", a, b, sq_plus(a,b) );
    return 0;
    }

    /* end source files */

    How you compile these files together is platform-specific.

    <OT>If you're using GCC it'll go something like this:

    gcc -ansi -pedantic -W -Wall -c square.c
    gcc -ansi -pedantic -W -Wall -c main.c
    gcc square.o main.o -o square

    The -c option tells GCC not to link yet because you're compiling multiple
    source files; it will produce a .o file for the .c file it's given. The
    last line actually links the various .o files together (by calling ld,
    usually); since the default output file is "a.out" for historical reasons,
    you need the -o option to give the program a sensible name.</OT>

    > Some questions:
    >
    > (1) I thought that main() was only supposed to be in the main function
    > file, but if I don't have a main() in square.c, I get errors.
    >
    > (2) I don't understand why I'm getting the undefined reference error.
    > I'm using the Dev compiler and it seems that it is ANSI-compatible.


    This has nothing to do with ANSI; your code is broken, and on top of that
    you're not compiling/linking it correctly.

    > (3) Does the "ld returned 1 exit status" error go away when the
    > undefined reference error is solved as I'm assuing it does?


    Yes.

    BTW, why do you call the function "square plus" if you're _subtracting_ the
    second argument? Shouldn't it be "square minus"?

    S

    --
    Stephen Sprunk "Those people who think they know everything
    CCIE #3723 are a great annoyance to those of us who do."
    K5SSS --Isaac Asimov


    --
    Posted via a free Usenet account from http://www.teranews.com
    Stephen Sprunk, Apr 6, 2007
    #9
    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. Felipe
    Replies:
    3
    Views:
    440
    Felipe
    Jul 16, 2004
  2. Lu
    Replies:
    1
    Views:
    561
    Roel Schroeven
    Jul 10, 2003
  3. Mantorok Redgormor
    Replies:
    70
    Views:
    1,734
    Dan Pop
    Feb 17, 2004
  4. VK
    Replies:
    45
    Views:
    579
    Dr John Stockton
    Sep 12, 2006
  5. -Lost
    Replies:
    13
    Views:
    359
    Richard Cornford
    Jan 31, 2007
Loading...

Share This Page