-m compilation flag doesn't appear to respect modules that belong to subfolders

New to FreeBASIC? Post your questions here.
Post Reply
xX_Pokeman2003_Xx
Posts: 31
Joined: Mar 11, 2022 21:10

-m compilation flag doesn't appear to respect modules that belong to subfolders

Post by xX_Pokeman2003_Xx »

This somewhat circles back to another thread I wrote, but it's not really relevant. I have a file that looks like this that I'm feeding into FBC.exe with the @ compiler flag

Code: Select all

-x "Test.exe"
-m entry
-v
-s console
-i "N:\TestProject\Source\Interface"
-p "N:\TestProject\Source\Libraries"
-map "Test.map"
-b "N:\TestProject\Source\Backend\Files\INI.bas"
-b "N:\TestProject\Source\Windows\entry.bas"
-nostrip
Now, the two referenced bas files are just single line print commands. Not important what they actually are, all you need to know is that entry.base produces "FIRST" and INI.bas produces "SECOND" .
Now, for some reason, the output is "SECOND" followed by "FIRST". This is obviously not the behavior I want. I've tried all sorts of things. Direct referencing the file without the .bas extension, referencing the folder with both back and forward slashes, switching -m's position to the end of the script, but none of this seems to work. I'm not exactly sure why this is happening, but it's pivotal I get it working.
fxm
Moderator
Posts: 12467
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by fxm »

Have you tested this?

Code: Select all

-x "Test.exe"
-m N:\TestProject\Source\Windows\entry
-v
-s console
-i "N:\TestProject\Source\Interface"
-p "N:\TestProject\Source\Libraries"
-map "Test.map"
-b "N:\TestProject\Source\Backend\Files\INI.bas"
-b "N:\TestProject\Source\Windows\entry.bas"
-nostrip
xX_Pokeman2003_Xx
Posts: 31
Joined: Mar 11, 2022 21:10

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by xX_Pokeman2003_Xx »

fxm wrote: Jan 21, 2025 6:19 Have you tested this?
Yes, I have. I just noticed as well, it declares
N:\TestProject\Source\Windows\entry.bas -o N:\TestProject\Source\Windows\entry.c (main module)
in the compilation log, but it still does the same order of SECOND followed by FIRST despite saying that entry.c is the main module.
SARG
Posts: 1854
Joined: May 27, 2005 7:15
Location: FRANCE

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by SARG »

Why is it necessary to respect the order during the compilation ?
The linker should link the modules. Maybe declarations (eg extern) are missing to give enough information to the linker
Post the entire log.
dodicat
Posts: 8189
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by dodicat »

The -b flags are not needed since the files are .bas anyway.
Have you tried not using -b option?
adeyblue
Posts: 342
Joined: Nov 07, 2019 20:08

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by adeyblue »

'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
dodicat
Posts: 8189
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: -m compilation flag doesn't appear to respect modules that belong to subfolders

Post by dodicat »

Also, here, I cannot get a multiline file to work for @file, fb_main doesn't get put anywhere, so I error with no fb_main found.
OK if I keep everything on the one line with a space between options in the file.
And yes, as adeyblue states, my entry code via -m doesn't always appear as the first runner.
Win 11, fb 10.1
Post Reply