'main' module doesn't mean 'best, most important, first' module, it literally means the module where the compiler puts the startup code (ie the function called main).
All global code runs before the startup code starts and any in the 'main' module gets put into the startup code. Which if anything, guarantees it'll run last of all global code, not first.
In C, global code isn't guaranteed to run in any particular order (you can google 'static initialization order fiasco' for details), so it's generally not a good idea to rely on that, especially if it has side-effects or requires state from a different global thing you've got going on. Freebasic is the same.
You can use
Module Constructors which use a number to tell them in which order to run, and that works regardless of the main flag. But of course, the real solution to 'I want this to run first' is to, you know, call it first. Make an init function in your platform specific folder, call it from your crossplatform main and you get full control of the order while doing things you were probably doing anyway. For instance:
Code: Select all
'' cross platform main.bas of your dll/exe/whatever it is
Type ImportantContext as ImportantContext_
Declare Function SuperDuperPlatformInit(ByVal argc as Long, ByVal argv as ZString Ptr Ptr) As ImportantContext Ptr
Declare Sub DoImportantWorkLoop(ByVal as ImportantContext Ptr)
Declare Sub CryButAcceptItsOver()
Print("App has started")
Dim as ImportantContext Ptr pCtx = SuperDuperPlatformInit(__FB_ARGC__, __FB_ARGV__)
DoImportantWorkLoop(pCtx)
CryButAcceptItsOver()
Print("App is exiting")
Then you implement those functions in your Linux folder, your Windows folder, etc and it all works in the order you expect.
Code: Select all
'' windows/init/bas
dim as HWND g_mainWindow
Type ImportantContext as ImportantContext_
Function SuperDuperPlatformInit(ByVal argc as Long, ByVal argv as ZString Ptr Ptr) As ImportantContext Ptr
InitAudio()
InitVideo()
InitLogging()
g_mainWindow = CreateWindow()
Return Null '' context is a linux thing
End Function
But oh no, you need to capture some details if the audio init fails and you need the logger for that. Oh that's easy
Code: Select all
'' windows/init/bas
dim as HWND g_mainWindow
Function SuperDuperPlatformInit(ByVal argc as Long, ByVal argv as ZString Ptr Ptr) As ImportantContext Ptr
InitLogging()
InitAudio()
InitVideo()
g_mainWindow = CreateWindow()
Return Null '' context is a linux thing
End Function
And you're gucci