Cross-platform fun with FreeBasic

Introduction

FreeBasic is an open-source, QuickBasic-compatible, BASIC compiler available for Windows, Linux, and MacOS. It seems less feature-rich than closed-source PureBasic and PowerBasic, but it has the advantage of being cross-platform and might be good enough for your needs.

As for May 2010, the latest official release dates back to 2008, but the SVN tree is very much active.

Setup

Windows

  1. The FreeBasic compiler, fbc.exe, requires the Microsoft C run-time library, msvcrt.dll.
  2. Download and run the installer, (un)checking libraries as you see fit. By default, FreeBasic is installed in C:\Program Files\FreeBASIC
  3. To compile "Hello, world!", double-click on the FreeBasic icon, and type "fbc examples\misc\hello.bas". This while generate hello.exe which you can try to check that FreeBasic works fine.
  4. Install an IDE such as FBEdit or

IDE

Compiling

-s <name>      Set subsystem (gui, console)

Files

Code Samples

Formatting a string

Using sprintf() from the C Standard Library Functions:

#include "crt/stdio.bi"
dim as zstring*17 NUMB
sprintf( @NUMB, "Just a %s string", "dummy" )
print NUMB

Extracting filename from path

Dim s As String = "c:\temp\myfile.exe"
Dim sf As String
 
sf = Mid(s, Instrrev(s, "\") + 1)
Print "'" & sf & "'"

Playing with parameters

Here's how to loop through the parameters passed to a program (source):

DIM i AS INTEGER
 
PRINT COMMAND$
 
PRINT "This program is named "; COMMAND$(0)
 
i = 1
DO WHILE(LEN(COMMAND$(i)))
    PRINT "The argument "; i; " is "; COMMAND$(i)
    i = i + 1
LOOP
 
FOR i = 0 TO __FB_ARGC__ - 1
        PRINT "arg "; i; " = '"; *__FB_ARGV__[i]; "'"
NEXT i

SQLite

"C:\Program Files\FreeBASIC\inc\sqlite3.bi"

#define SQLITE_VERSION "3.1.3"

Q&A

Freebasic vs. Purebasic?

http://www.dbfinteractive.com/forum/index.php?topic=5359.0

http://www.purebasic.fr/english/viewtopic.php?f=7&t=31678

http://en.wikipedia.org/wiki/PureBasic

Running an external application: Exec vs. Run vs. Shell

Exec

"Transfers control over to an external program. When the program exits, execution resumes immediately after the call to Exec."

http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgExec

Note: The first parameter must contain the name of the application, with its full path, and parameters, if any, must be passed trough the second+ parameter(s). Here's an example:

result = Exec("c:\windows\notepad.exe","c:\test.txt")

Run

"Transfers control over to an external program. When the program exits, execution will return to the system."

http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgRun

Shell

"Program execution will be suspended until the command interpreter exits."

http://www.freebasic.net/wiki/wikka.php?wakka=KeyPgShell

How to display a Windows message box?

FreeBasic doesn't include functions to build a GUI application. You'll either have to use a third-party GUI builder, or learn the Win32 API la Petzold.

Some samples:

The options are:

Here's an example of calling the Windows API to display a message box:

#include "windows.bi"
MessageBox(NULL, "There", "Hello", MB_OK)

Here's how to display a regular window:

How to hide the DOS box?

Compile the source file with "-s gui". Make sure the application doesn't include a call to "Sleep", as it prevents it from closing.

How to include an ICO file?

To replace the standard application icon with your own:

  1. Create a .RC text file with the absolute path to the ICO file:

    FB_PROGRAM_ICON ICON "C:\Project\Dummy\myicon.ico"
     
  2. Include this file when compiling the application:

    fbc.exe "C:\Project\Dummy\myrc.rc" -s gui "C:\Project\Dummy\mysrc.bas"

If you want to extract an ICO from an application, BeCyIconGrabber is free and works fine.

To read

http://freebasic.net/index.php/download

http://www.freebasic.net/index.php/link

http://www.freebasic.net/wiki/wikka.php?wakka=FBWiki

http://www.freebasic.net/wiki/wikka.php?wakka=DocToc

http://www.freebasic.net/wiki/wikka.php?wakka=CompilerRunning

http://www.freebasic.net/arch/category.php?id=1

http://www.freebasic.net/arch/category.php?id=2

http://www.freebasic.net/index.php/codelib?section=view_code&id=12

http://www.freebasic.net/forum/viewforum.php?f=2

http://www.freebasic.net/index.php/details?page=download&category=bin&id=1

http://www.freebasic.net/index.php/details?page=download&category=doc&id=1

http://radasm.cherrytree.at/fbedit/

http://www.planetsquires.com/jellyfishpro_freebasic.htm

Resources