Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: extern typedef struct

Reply
Thread Tools

Re: extern typedef struct

 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      05-18-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: extern typedef struct BruceS C Programming 6 05-19-2010 04:38 PM
Re: extern typedef struct Seebs C Programming 3 05-18-2010 05:17 PM
How to declare extern typedef struct? Dirk Bruere at NeoPax C Programming 4 12-07-2008 01:54 AM
typedef struct {} SName; vs. struct SName{}; Steven T. Hatton C++ 2 08-03-2005 09:59 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57