Circular include best ways to avoid?

Just starting out? Need help? Post your questions and find answers here.
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

Circular include best ways to avoid?

Post by emperor-limitless »

so I have file a.pbi, it contains a structure that uses something from b.pbi as a list, then I have procedures that take pointers of a in b.pbi, as well as a prototype function. As you would have figured, I ran into a include sircular loop, if I use XIncludeFile, well, it will loop, and if I include somewhere else one of them will be included before the other so they just both break anyway
so any ideas? I would like to avoid putting 2 directly unrelated structures in one place, because I'll eventually have a messy bunch of files, in C/C++ this will be done by headers, but I'm not sure if this would work considering there are no structure declarations to my knowledge, only procedure declarations, which fixes half the problem.
I'm aware that I likely could go rewrite the code to avoid the problem, but I'm curious if there's no solution to that?
Kulrom
User
User
Posts: 16
Joined: Thu Sep 07, 2023 6:07 am

Re: Circular include best ways to avoid?

Post by Kulrom »

You already have spaghetti code. Make a separate file in which you declare all the structures.
I love programming languages that start with the letter "P":
Python, Pascal and ... PureBasic! :)
User avatar
spikey
Enthusiast
Enthusiast
Posts: 750
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Circular include best ways to avoid?

Post by spikey »

emperor-limitless wrote: Thu Nov 14, 2024 11:02 pm but I'm curious if there's no solution to that?
Not automagically no. Although PB doesn't specifically use a header file arrangement there's no reason why you can't use either a '.pb' or a '.pbi' file to contain header information like structures and enumerations, they don't have to include procedures too.

Personally I dislike multilevel including so I usually put something like this at the top of dependent files.

Code: Select all

CompilerIf Not Defined(SOMETHING, #PB_Structure)
  CompilerError "Structure SOMETHING is not defined. Forgot hdrSomething.pbi?"
CompilerEndIf
but otherwise you can happily XInclude 'hdrSomething.pbi' in a '.pbi' and 'b.pbi' right at the top and you'll get the outcome you're looking for.
Last edited by spikey on Fri Nov 15, 2024 10:57 am, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Circular include best ways to avoid?

Post by Fred »

Pointers doesn't need forward structure declaration, it's resolved when accessing the structure (so prototype and procedure declaration should work).
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

Re: Circular include best ways to avoid?

Post by emperor-limitless »

ah I see, certainly interesting. Most of my time I used C# which doesn't care about where something is defined so this is going to be an interesting experience to learn. I just hope no one get annoyed at me because I ask a lot of questions :)
Post Reply