Does FreeBasic has a main() something like?
Does FreeBasic has a main() something like?
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
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
Re: Does FreeBasic has a main() something like?
See the Executables documentation page.
-
- Posts: 8586
- Joined: May 28, 2005 3:28
- Contact:
Re: Does FreeBasic has a main() something like?
You can call your own main function.
Joshy
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__ )
Re: Does FreeBasic has a main() something like?
Paraphrasing:
"It may or may not have a main depending on (unspecified) options". Hmm. Yeah, that will put a beginner right on track
Re: Does FreeBasic has a main() something like?
Freebasic needs no main.
The entry point is in the main module.(__FB_MAINPROC__)
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.
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
You can automate a sub to start without calling it, ie not start in __FB_MAINPROC__.
See constructor in the help files.
Re: Does FreeBasic has a main() something like?
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.
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.
Re: Does FreeBasic has a main() something like?
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)
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
Re: Does FreeBasic has a main() something like?
Thank you all for the explanation!
I am feeling good when I do something like this in C
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
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 ;
}
Regards,
Peter
Re: Does FreeBasic has a main() something like?
Here is another one :
Build.bat :
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
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
Re: Does FreeBasic has a main() something like?
Thank you all!
I am learning on all these...
I am learning on all these...