Re: Forward declaration issue (error: previous declaration of 'tst' was here)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well, I wish I could use classes :)

I'm trying to compile an existing project (written on C) on GCC... the
project is originally compiles on Metaware which allows this kind of forward
declaration... so I won't be able to change the code and use classes...

But thanks anyway... that really helped me


Hani Ayoub wrote:
> 
> I see... 
> 
> Anyway... I've tried to compile the same code on g++ (instead of gcc), and
> I got no error :)
> Don't really know why... but works fine...
> 
> 
> Baurzhan Ismagulov wrote:
>> 
>> On Tue, Dec 02, 2008 at 07:30:02AM -0800, Hani Ayoub wrote:
>>> >>>> tst.h
>>> >>>> =======
>>> >>>> typedef struct _tst{
>>> >>> ...
>>> >>>> } tst;
>>> >>>
>>> >>>> tst2.h
>>> >>>> =======
>>> >>>> #include "tst.h"
>>> >>>>
>>> >>>> int foo(tst t);
>>> >>>
>>> >>>> tst.c
>>> >>>> =======
>>> >>>> #include <stdio.h>
>>> >>>> #include "tst2.h"
>>> >>>>
>>> >>>> int foo(tst t){ return 1; };
>> ...
>>> What I'm trying to do is "forward declaration"; means to hide tst struct
>>> definition from source or header files that includes tst2.h file.
>> 
>> Then you'll want to remove #include "tst.h" from tst2.h -- struct _tst
>> is exactly what you want to hide from tst2.h and tst.c -- and make
>> typedef struct _tst tst available somewhere.
>> 
>> I personally would put typedef struct _tst tst in tst.h and hide struct
>> _tst declaration within something like #ifdef INTERNAL_TST ... #endif:
>> 
>> tst.h
>> =====
>> #ifdef INTERNAL_TST
>> struct _tst {
>> ...
>> };
>> #endif
>> 
>> typedef struct _tst tst;
>> 
>> user.h
>> ======
>> #include "tst.h"
>> int foo(tst t);
>> 
>> user.c
>> ======
>> #include "user.h"
>> int foo(tst t){ return 1; }
>> 
>> This would provide the same header for both API-providing and API-using
>> modules.
>> 
>> With kind regards,
>> -- 
>> Baurzhan Ismagulov
>> http://www.kz-easy.com/
>> 
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Forward-declaration-issue-%28error%3A-previous-declaration-of-%27tst%27-was-here%29-tp20755376p20837113.html
Sent from the gcc - Help mailing list archive at Nabble.com.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux