How to compile & Run freebasic code

New to FreeBASIC? Post your questions here.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to compile & Run freebasic code

Post by kcvinu »

Hi all,
I am using VS Code to write freebasic code. I am able to compile my code in vs code, with the help of an extension called "code-runner". But I need to compile & run my code when I click the run button on vs code. I checked the fbc manual for a compiler switch but didn't find. Please help.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to compile & Run freebasic code

Post by dodicat »

I dowloaded a .zip version of VS Code.
Nice editor but I note that (under configuration for code-runner):

"Make sure the executor PATH of each language is set in the environment variable"

So that is me stopped, my fbc.exe in use is not on a system path.
Good luck.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

@dodicat,
So you mean, there is no compiler switch for running program right after compiling ?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to compile & Run freebasic code

Post by dodicat »

Here are the compiler options:

Code: Select all


-a < name >
Add an object file to linker's list
-arch < type >
Set target architecture (default: 486)
-asm < format >
Sets the assembler format for Asm block
-b < name >
Add a source file to compilation
-c
Compile only, do not link
-C
Do not delete the object file(s)
-d < name=val >
Add a preprocessor's define
-dll
Create a DLL, including the import library. (Same as -dylib)
-dylib
Create a DLL, including the import library
-e
Add error checking
-earray
Enable array bounds checking
-eassert
Enable assert() and assertwarn() checking
-edebug
Enable __FB_DEBUG__
-edebuginfo
Add debug information
-elocation
Enable full error location reporting
-enullptr
Enable null-pointer checking
-ex
Add error checking with RESUME support
-exx
Same as -ex, plus array bounds, null-pointer, and error location reporting
-export
Export symbols for dynamic linkage
-forcelang <name>
Select language compatibility, overriding #lang/$lang in code
-fpmode < type >
Select between fast and accurate floating-point operations (default: PRECISE)
-fpu < type >
Set the floating point arithmetics unit (default: FPU)
-g
Add debug info, enable##__FB_DEBUG__##, and enable asserts
-gen < backend >
Sets the compiler backend (default is 'gas')
-i < name >
Add a path to search for include files
-include < name >
Include a header file on each source compiled
-l < name >
Add a library file to linker's list
-lang < name >
Select language compatibility: fb, fblite, qb, deprecated
-lib
Create a static library
-m < name >
Main file without extension, the entry point (default is the first .bas file on the command line)
-map < name >
Save the linking map to file name
-maxerr < val >
Only stop parsing if <val> errors occurred
-mt
Link with thread-safe runtime library
-nodeflibs
Do not include the default libraries
-noerrline
Do not show source line where error occurred
-noobjinfo
Do not read/write compile-time info from/to .o and .a files
-nostrip
Do not strip symbol information from the output file
-o < name >
Set object file path/name (must be passed after the .bas file)
-O < level >
Set the optimization level (-gen gcc)
-p < name >
Add a path to search for libraries
-pic
Generate position-independent code (non-x86 Unix shared libs)
-pp
Emit the preprocessed input file only, do not compile
-prefix < path >
Set the compiler prefix path
-print < option >
Let the compiler display certain information (fblibdir, host, target, x)
-profile
Enable function profiling
-r
Compile into *.asm/*.c/*.ll file(s) only, do not assemble or link
-R
Preserve intermediate *.asm/*.c/*.ll file(s) generated by compilation
-rr
Compile into *.asm file(s) only, do not assemble or link
-RR
Preserve intermediate *.asm files generated by compilation
-s < name >
Set subsystem (gui, console)
-showincludes
Display a tree of file names of #included files
-static
Prefer static libraries over dynamic ones when linking
-strip
Omit all symbol information from the output file
-t < value >
Set stack size in kbytes (default: 1M)
-target < platform >
Set the target platform for cross compilation
-v
Be verbose
-vec < level >
Set level of vector optimizations enabled by the compiler (default: 0)
-version
Show compiler version
-w < value >
Set min warning level: all, pedantic or a value
-Wa < opt >
Pass options to GAS (separated by commas)
-Wc < opt >
Pass options to GCC (separated by commas)
-Wl < opt >
Pass options to LD (separated by commas)
-x < name >
Set executable/library path/name
-z < value >
Sets miscellaneous or experimental options

 
They are not really taylored for any particular editor, they are for the command line.
OK, ide's can be set with them for the reasons as shown, an ide just passes the command line instructions to the compiler.
you can do some things inside your code, e.g. set a temporary path to your compiler.

Code: Select all


dim as string mypath="C:\Users\Computer\Desktop\fb\FreeBASIC-1.07.2-win64\FreeBASIC-1.07.2-win64"

SetEnviron "path="+mypath
print

print
shell "fbc.exe"
sleep
 
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: How to compile & Run freebasic code

Post by srvaldez »

you could probably make a batch file that would invoke fbc.exe and then after successful compile run the executable if it was produced, you will have to do a bit of searching but it should be doable, your IDE would invoke your batch file instead of fbc.exe
Xusinboy Bekchanov
Posts: 783
Joined: Jul 26, 2018 18:28

Re: How to compile & Run freebasic code

Post by Xusinboy Bekchanov »

You can see this topic:
viewtopic.php?f=17&t=29117
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to compile & Run freebasic code

Post by jj2007 »

srvaldez wrote:you could probably make a batch file that would invoke fbc.exe and then after successful compile run the executable if it was produced, you will have to do a bit of searching but it should be doable, your IDE would invoke your batch file instead of fbc.exe
Hi kcvinu,
There are many ways to do it with a batch file, this is just one example:

Code: Select all

@echo off
set options=-gen gcc -Wc -O2 -s console
if exist %~pn1.exe del %~pn1.exe

set path=C:\TDM-GCC-64\bin\
echo *** compiling %~nx1 with %options% ***

set rcfile=
if exist TmpFb.rc set rcfile=TmpFb.rc

<your path>FreeBasic\fbc64.exe %options% %rcfile% "%1"
echo.
if exist %~pn1.exe (
	echo *** running %~n1.exe %2 ***
	start %~pn1.exe %2
) else (
	echo *** you got a problem, %~pn1.exe was not created ***
)
First argument is the path as you drag it over the *.bat file, e.g. C:\mypath\myfile.bas
Second argument is what you pass to the running executable (if needed).
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

Thank you all for the replies.
@jj2007,
I failed to run your bat file. However, that bat file idea gave me a new direction and I created this AutoIt script for that.

Code: Select all

Local $FileName = $CmdLine[1] '\\ First command line parameter
Local $WorDir = $CmdLine[2]
Local $compRes = ShellExecuteWait ("fbc", $FileName & ".bas", $WorDir)
If $compRes = 0 Then
	ShellExecuteWait($FileName, "", $WorDir)
Else
	MsgBox(0, "FbcRunner", "Compilation Failed...!!!")
EndIf
Exit
Now, all I need writhe vs code settings like this.

Code: Select all

"freebasic" : "FbcRunner $fileNameWithoutExt $dirWithoutTrailingSlash"
Though, It's a success but there is a problem in this approach. If compilation failed, I didn't get any error messages. Because, ShellExecuteWait only returns the exit code of the running program aka fbc. So I think I need to take this little script to another level.
For that, I am planning to create a little program in VB.Net since it is the only language I know how to work with processes. In VB.Net, I can use the process class and get the error messages which fbc emits.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

Hi all,
This is a ThinBasic script for this purpose. It will get the file name and working directory from cmd args and run the fbc compiler. Then if any errors occurs, It will show the fbc error message in cmd window. If compilation is success, It will run the new exe file and display the program results in cmd window. I am posting the script because some will get benefit from this.

Code: Select all

Uses "Console"
Uses "OS"
 

Dim rawFileName As String = OS_GetCommand(1)  '\\ Here we get the first cmd args
Dim LastCmd as String = OS_GetCommand(0)	'\\ this is a hack bcz, thinbasic won't read a quoted string.So we get the full args.
Dim WorkingDir As String = GRAB$(LastCmd, Chr$(34), Chr$(34)) '\\ Now we grab the string in between two quotes

Dim BasFile As String = rawFileName
Dim ExeFile As String = rawFileName & ".exe"
Dim SQuote As String = Chr$(34)
Dim QuotedBasFile As String = SQuote & BasFile & SQuote
Dim QuotedExeFile As String = SQuote & ExeFile & SQuote
Dim QuotedWorDir As String = SQuote & WorkingDir & SQuote
Dim FbcPath As String = "C:\FreeBasic\FreeBASIC-1.07.3-win64\fbc.exe"
Dim QuotedFBC as String = SQuote & FbcPath & SQuote
Dim FullFile As String = WorkingDir & "\" & BasFile
Dim QuotedFullFile as String = SQuote & FullFile & SQuote
Dim QuotedFullExe As String = SQuote & WorkingDir & "\" & ExeFile & SQuote

Dim pID = OS_Shell(QuotedFBC & " " & QuotedFullFile, %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC) '\\ here is the compilation
'\\ The magic is here, If compilation ends up in an error, thinbasic will show us the results.
if pID = 0 Then '\\ Exit code = 0 then run the generated exe file.
  OS_Shell(QuotedFullExe, %OS_WNDSTYLE_NORMAL, %OS_SHELL_SYNC)
EndIf
WaitKey
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to compile & Run freebasic code

Post by jj2007 »

kcvinu wrote:Dim WorkingDir As String = GRAB$(LastCmd, Chr$(34), Chr$(34)) '\\ Now we grab the string in between two quotes
Funny name for this function, I had to google it to confirm it's really part of the language. My own baby is called Replace$()
kcvinu wrote:@jj2007,
I failed to run your bat file
Did your adjust your paths? I see no other reason why it should fail...
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

@jj2007,
Yeah, Grab is a funny name. But it's very handy.
I have completed that script and started using it in my laptop & PC. Now I can code FreeBasic in VS Code.
Did your adjust your paths? I see no other reason why it should fail...
I have changed the path but my path contains some spaces. I think that was the reason. I know very little about vb script but I was sure that I can solve that problem with languages like AutoIt, ThinBasic, Vb.Net, Nim.
BTW, I see your Replace implementation in MasmBasic. Is it active now ? I have an urge to learn those languages which have basic in name or syntax.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: How to compile & Run freebasic code

Post by srvaldez »

@kcvinu
would you care to share how you managed to setup VScode to compile for FB?
no matter what I tried I could never get it to work
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: How to compile & Run freebasic code

Post by jj2007 »

kcvinu wrote:BTW, I see your Replace implementation in MasmBasic. Is it active now ? I have an urge to learn those languages which have basic in name or syntax.
Since October 2009, yes ;-)
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

@srvaldez
would you care to share how you managed to setup VScode to compile for FB?
no matter what I tried I could never get it to work
Sure.
1. Install lang-freebasic extension by mudhairless.
2. Install Code Runner extension by Jun Han
Now you need to tweak some options of Code runner.
1. Open VS Code Settings.
2. Open JSON Settings page
3. Find "code-runner.executorMap"
4. Add a new value like this -
"freebasic" : "FbcRunner $fileNameWithoutExt $dirWithoutTrailingSlash" // Here, FbcRunner is my program.
5. Find ""code-runner.executorMapByFileExtension"
6. Add a new value like this -
".bas" : "FbcRunner $fileNameWithoutExt $dirWithoutTrailingSlash"
7. Find this -- "code-runner.languageIdToFileExtensionMap"
8. Check if this value is there -- "freebasic" : ".bas". (If not, add it)
9. Now find this -- "files.associations"
10. Add this value -- "*.bas": "freebasic"
That's all.
kcvinu
Posts: 232
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to compile & Run freebasic code

Post by kcvinu »

@jj2007,
I 've heard about HLA (High Level Assembly) programming language few years ago. I don't know what's status of it. But it was a nice blend of assembly and high level features.
1. MasmBasic is not BASIC: mov ebx, eax is not part of any BASIC dialect.
2. MasmBasic is assembly, i.e. it will flawlessly assemble with Microsoft Macro Assembler (MASM, version 6.15 upwards) or, alternatively, with Jwasm
I took this warning seriously :)
Post Reply