Re: extern typedef struct

Discussion in 'C Programming' started by Jens Thoms Toerring, May 18, 2010.

  1. Jeff <> wrote:
    > Last week I posted a question and you guys were very helpful. Thanks.
    > I decided to try again ;-)


    > I'd like to make an object global but my linker is playing hard to
    > get.


    > Here's a stripped down example:


    > obj.c ===============


    > struct object_t
    > {
    > int id;
    > char * name;
    > };



    > object
    > new_object()
    > {
    > object this;


    > this = calloc(1, sizeof(struct object_t));
    > /* [etc ..] */
    > return this;
    > }


    I guess you have stripped too much. This will already trip up
    the compiler since 'object' is not declared, defined or typedef'ed
    here. So aleady the line

    object new_object()

    should make the compiler complain unless you have snipped
    something like

    #include "obj.h"


    > obj.h ===============
    > #ifndef __object_h
    > #define __object_h
    > typedef struct object *object;


    > object new_object();


    > /* [etc...] */


    > #endif/*__object_h*/


    In C empty parentheses in a function declaration do not mean
    that the function does not take arguments but that you just don't
    want to tell the compiler. And the the compiler must assume that
    the function could take any number of arguments and thus can't
    warn you if you pass it the wrong ones. So better make that

    object new_object( void );

    Also a lot of people (including me) aren't very happy with
    tyoedef'ing pointers. I would recommend that at a maximum
    you just do

    typedef struct object object;

    object * new_object( );

    That makes it immediately clear that what the function
    returns is a pointer (and thus has to be treated as one)
    and not a simple variable. (And then some people are even
    more or less vehemently against typedef'ing structures...)


    > main.c ===============


    > include <object.h>


    > int
    > main(int argc, char *argv[])
    > {
    > object obj = new_object();
    > /* [etc ...] */
    > return 0;
    > }


    > setup.h ===============


    > extern object obj;


    > sock.c ===============


    > sock
    > new_socket()
    > {
    > int thing = object_get_something(obj); /* compiles but doesn't link
    > */
    > }


    > I get the following linking error:
    > sock.c:69: undefined reference to `obj'


    Again, this already should have gotten the compiler angry at
    you since 'obj' is nowhere defined or declared in sock.c.
    Also 'sock' is unknown.

    > If this is obvious, I apologize. I've been spending a lot of time in
    > php and my C has gotten rusty.


    I guess you better try again with a compilable (but not nece-
    sarrily linkable) program, because yours is obviously so
    stripped down that it's impossible to say for sure what the
    linker complains about in the real program. It might just be
    a forgotten '#include setup.h' in sock.c but that's pure guess-
    work and could be completely wrong.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 18, 2010
    #1
    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. DanielEKFA
    Replies:
    8
    Views:
    616
    DanielEKFA
    May 16, 2005
  2. Steven T. Hatton
    Replies:
    2
    Views:
    681
  3. Dirk Bruere at NeoPax

    How to declare extern typedef struct?

    Dirk Bruere at NeoPax, Dec 6, 2008, in forum: C Programming
    Replies:
    4
    Views:
    11,693
    Dirk Bruere at NeoPax
    Dec 7, 2008
  4. Seebs

    Re: extern typedef struct

    Seebs, May 18, 2010, in forum: C Programming
    Replies:
    3
    Views:
    574
    Jens Thoms Toerring
    May 18, 2010
  5. BruceS

    Re: extern typedef struct

    BruceS, May 18, 2010, in forum: C Programming
    Replies:
    6
    Views:
    1,037
    BruceS
    May 19, 2010
Loading...

Share This Page