OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:48 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: I can't use my own type in the function prototype
PostPosted: Mon May 04, 2020 5:56 pm 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
Hi.
I try to use the structure in my project, but I run into problems.

I have 2 files, lfbmemory.h(also have lfbmemory.c) and more.h (also have more.c).
In lfbmemory.c this structure was created.
Code:
typedef struct ssfn_text_cursor {
    uint32_t x;
    uint32_t y;
    uint32_t context_index;
} __attribute__((packed)) ssfn_text_cursor_t;


In the file more.h I declare a function prototype that works with this structure.
Code:
extern void show_base_info(ssfn_text_cursor_t* text_cursor);

But I get the following error when compiling lfbmemory.c
Code:
Build ./source/lfbmemory/lfbmemory.c
In file included from ./source/lfbmemory/../memory/memmmu/../memdetect/memdetect.h:9:0,
                 from ./source/lfbmemory/../memory/memmmu/memmmu.h:9,
                 from ./source/lfbmemory/lfbmemory.h:9,
                 from ./source/lfbmemory/lfbmemory.c:1:
./source/lfbmemory/../memory/memmmu/../memdetect/../../more/more.h:13:28: error: unknown type name ‘ssfn_text_cursor_t’
extern void show_base_info(ssfn_text_cursor_t* text_cursor);

Here(see lfbmemory and more folders) you can find the full versions of these files, which may help.
Thanks.


Top
 Profile  
 
 Post subject: Re: I can't use my own type in the function prototype
PostPosted: Mon May 04, 2020 9:00 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
Oh, oh, you are creating a tangled web of dependencies. So more.h depends on lfbmemory.h (for the type declaration), but also, lfbmemory.h depends on more.h (because more.h is included in lfbmemory.h). This is a circular dependency you should cut ASAP. See, what happens is that lfbmemory.h includes memmu.h at the start, which includes memdetect.h, which includes more.h. more.h would like to include lfbmemory.h, but it can't, because the include guards are active. So that last include does nothing and the type remains undeclared by the time the compiler sees the prototype. Removing the include guards would not really help; you'd get double-definition errors instead. What you can try to do is restructure the header files so that you get a dependency tree again (instead of a cyclic graph), for instance by moving that structure definition into a new header file that both more.h and lfbmemory.h can depend on.

If the dependency structure truly must be this way, one workaround you have is to use incomplete types, which is enough for pointer declarations. So in more.h you write:
Code:
struct ssfn_text_cursor;
void show_base_info(struct ssfn_text_cursor*);
And, boom, at least the include dependency is gone.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: I can't use my own type in the function prototype
PostPosted: Tue May 05, 2020 7:21 am 
Offline
Member
Member
User avatar

Joined: Sun Jul 21, 2019 7:34 am
Posts: 293
nullplan wrote:
Oh, oh, you are creating a tangled web of dependencies. So more.h depends on lfbmemory.h (for the type declaration), but also, lfbmemory.h depends on more.h (because more.h is included in lfbmemory.h). This is a circular dependency you should cut ASAP. See, what happens is that lfbmemory.h includes memmu.h at the start, which includes memdetect.h, which includes more.h. more.h would like to include lfbmemory.h, but it can't, because the include guards are active. So that last include does nothing and the type remains undeclared by the time the compiler sees the prototype. Removing the include guards would not really help; you'd get double-definition errors instead. What you can try to do is restructure the header files so that you get a dependency tree again (instead of a cyclic graph), for instance by moving that structure definition into a new header file that both more.h and lfbmemory.h can depend on.

If the dependency structure truly must be this way, one workaround you have is to use incomplete types, which is enough for pointer declarations. So in more.h you write:
Code:
struct ssfn_text_cursor;
void show_base_info(struct ssfn_text_cursor*);
And, boom, at least the include dependency is gone.

Yes, it's all right now. Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: thewrongchristian and 32 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group