Does FreeBasic has a main() something like?

New to FreeBASIC? Post your questions here.
Post Reply
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Does FreeBasic has a main() something like?

Post by PeterHu »

Greetings!

Just wondering does freebasic has a entry function somthing like main() in C ?Just want to understand the program workflow.

Thanks in advance.

Peter Hu
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Does FreeBasic has a main() something like?

Post by fxm »

See the Executables documentation page.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Does FreeBasic has a main() something like?

Post by D.J.Peters »

You can call your own main function.

Joshy

Code: Select all

Function main(ByVal argc As Integer, ByVal argv As ZString Ptr Ptr) As Integer
  print "Hello, world!"
  print "press any key ..."
  sleep
  return 0
End Function

End main( __FB_ARGC__, __FB_ARGV__ )
marcov
Posts: 3455
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Does FreeBasic has a main() something like?

Post by marcov »

fxm wrote: Jul 26, 2022 6:54 See the Executables documentation page.
Paraphrasing:

"It may or may not have a main depending on (unspecified) options". Hmm. Yeah, that will put a beginner right on track :oops:
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Does FreeBasic has a main() something like?

Post by dodicat »

Freebasic needs no main.
The entry point is in the main module.(__FB_MAINPROC__)

Code: Select all


sub one 
    print __function__
end sub

sub two 
    print __function__
    one
end sub

sub three 
    print __function__
    two
end sub

print __function__
three
sleep

 
I avoid naming a function main in freebasic.
You can automate a sub to start without calling it, ie not start in __FB_MAINPROC__.
See constructor in the help files.
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Does FreeBasic has a main() something like?

Post by TeeEmCee »

Some of the above replies are confusing. If you name a sub/function 'main' in FB, the actual function name becomes 'MAIN' in the object file, so it's completely different to the automatic 'main' function generated by fbc. So 'main' is not a reserved keyword in FB.

In each module, the top-level code (outside any sub or function) is placed in a "module constructor", except for the main module, the top-level code of which becomes the main() function. fbc adds extra setup and teardown code to the beginning/end of main(), mainly to install signal (error) handlers and initialise the terminal. The module constructors all run *before* main() in an unspecified order. So it's a bad idea to do anything in the module constructors except variable initialisation.

So if you compile your program in two steps, .bas to .o/.obj object files, and then link together the objects, you need to tell fbc while compiling which module is the main module using the -m commandline flag.

Also, it is possible to make fbc use a function name other than 'main' by using the -entry flag. This is useful if your actual main() function is in elsewhere, written in C for example, and you want to do some setup before running your FB code. For example, SDL 1.2 (but not SDL 2.0) on macOS requires this.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Does FreeBasic has a main() something like?

Post by dodicat »

Please note I said that I avoid using main as a name, it is a personal preference.
fbmain or something similar I would prefer.
A while ago I got caught out in a compiler error (duplicate use of main), I think it was while messing about with dll files.
So I avoid using main as a sub or function now.
But you can use it if you want to an an auto entry point (using constructor)

Code: Select all


sub otherthings
      print __function__
      print "The time is ";time
      end sub

sub main constructor
      print __function__
      otherthings
end sub
print "Press any key to end"
sleep
 
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Does FreeBasic has a main() something like?

Post by PeterHu »

Thank you all for the explanation!

I am feeling good when I do something like this in C

Code: Select all

//main.c
#include 
//all headfiles needed

int main(int argc,char** argv)
{
	run();//run() may or may not in main.c
	return 0 ;
	
}
because the sturcture and logic is so clear no matter how many source files in the project.But I don't quite sure how to make it in FB.

Regards,
Peter
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Does FreeBasic has a main() something like?

Post by Vortex »

Here is another one :

Code: Select all

#include "windows.bi"
#include "crt.bi"

#ifdef __FB_64BIT__

    #define CallConvention

#else

    #define CallConvention Cdecl

#endif


Sub wmain CallConvention Alias "wmain" ()

Dim As Integer i

For i=1 To 3

   printf !"This is a test.\n"

Next i

   ExitProcess(0)

End Sub
Build.bat :

Code: Select all

\FreeBASIC\fbc -nodeflibs -c SmallExe.bas

IF EXIST \MinGW32\bin\objcopy.exe \MinGW32\bin\objcopy --remove-section .fbctinf --remove-section .ctors SmallExe.o
\FreeBASIC\bin\win32\ld -e _wmain -subsystem console -o SmallExe.exe SmallExe.o -L\FreeBASIC\lib\win32 -lkernel32 -luser32 -lgdi32 -lmsvcrt -s

\FreeBASIC\fbc -arch 64 -nodeflibs -c SmallExe.bas -o SmallExe64.o
IF EXIST \MinGW64\bin\objcopy.exe \MinGW64\bin\objcopy --remove-section .rdata$zzz --remove-section .ctors --remove-section .fbctinf SmallExe64.o
\FreeBASIC\bin\win64\ld -e wmain -subsystem console -o SmallExe64.exe SmallExe64.o -L\FreeBASIC\lib\win64 -lkernel32 -luser32 -lgdi32 -lmsvcrt -s
PeterHu
Posts: 148
Joined: Jul 24, 2022 4:57

Re: Does FreeBasic has a main() something like?

Post by PeterHu »

Thank you all!

I am learning on all these...
Post Reply