How to compile MapBasic from TextPad

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
mapwiz
Posts: 4
Joined: Wed May 03, 2006 3:17 pm

How to compile MapBasic from TextPad

Post by mapwiz »

I posted this to the MapInfo Mailing List in response to a user's query regarding the use of MapBasic(R) with TextPad.

In 2003, Niels Fahse posted a useful MapBasic syntax definition file to the TextPad website, but there isn't a good description for compiling and linking MapBasic code.

MapBasic is a programming language for controlling the desktop mapping program, MapInfo Professional(R). It compiles source from its own specialized syntax into pseudo-code which MapInfo then executes.

MapBasic comes with a limited development environment and a lot of people turn to alternatives such as TextPad.

MapBasic allows for separate compilation of modules: Source files (with an extension of .mb) are compiled into intermediate object files (with an extension of .mbo). A "project" file (with an extension of .mbp) controls the linker, which produces the final pseudocode executable (with an extension of .mbx).

MapInfo has built a capability for compiling and linking MapBasic code on the command line of the MapBasic executable, so it's possible to create a TextPad tool utilizing this capability.

Although TextPad can capture output from a command-line tool, and parse it to locate where the compiler said the error occurred, MapBasic thwarts this capability, because the compiler always writes compilation errors to a file with an extension of .err, even when MapBasic is run from the command line. If there are no errors, there is no .err file when MapBasic finishes.

If the MapBasic compiler had a way to send errors to standard output instead of the .err file, you could capture the output in TextPad. But it doesn't, and MapInfo is unlikely to add this functionality.

The best you can do is to open the .err file in TextPad after compilation is finished. Unfortunately, there doesn't appear to be a way to make a TextPad "tool" perform two actions. TextPad's macro functionality appears to be limited to editing. So you'll have to make a DOS batch file like the following:

Code: Select all

@echo off
if %1 == %2 goto link
mapbasic -Nosplash -D %1
goto ckerr
:link
mapbasic -Nosplash -L %1
:ckerr
if exist %3 "C:\Program Files\TextPad 4\textpad" %3
Save this in a file named MBTextPad.bat in the same directory as Mapbasic.exe. Then add a tool to TextPad (Configure->Preferences..., Select "tools" on the right-hand side, then Add->Program...) that runs MBTextPad.bat. The parameters should be

Code: Select all

"$FileName" "$BaseName.mbp" "$BaseName.err"
including all of the quotes. Make sure you have allow multiple instances unchecked in Configure->Preferences->General or Textpad will open the .err file in a separate instance. This method will even work for MapBasic source files whose names have spaces in them (TextPad is a little too helpful putting quotes around parameter values with spaces in them). Perhaps someone is willing to write a VBScript to make this a bit less clunky.

Hope this helps
Spencer Simpson
Baltimore Metropolitan Council
mapwiz
Posts: 4
Joined: Wed May 03, 2006 3:17 pm

Post by mapwiz »

Gah. Some of the folks on Mapinfo-L gave me a gentle reminder about the DOS "type" command, which I'd forgotten about completely. This would turn MBTextpad.bat into

Code: Select all

@echo off
if %1 == %2 goto link
mapbasic -Nosplash -D %1
goto ckerr
:link
mapbasic -Nosplash -L %1
:ckerr
if exist %3 type %3
We still need to specify the .err file explicitly in order to handle files with spaces in their names correctly, so the parameters are still

Code: Select all

"$FileName" "$BaseName.mbp" "$BaseName.err"
You use the regular expression

Code: Select all

^(\([^):]+\): *\([0-9]+\))
to parse output, register 1 for the file name, and register 2 for the line number. Works like a charm!
Spencer Simpson
Baltimore Metropolitan Council
Post Reply