Declaring a Macro as global

General FreeBASIC programming questions.
Post Reply
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Declaring a Macro as global

Post by Dinosaur »

Hi All

Never been a great user of macro's , but now simply cannot find how to make a macro visible
to all modules.

I wrote the macro in one of my ".bi" files.

Code: Select all

#macro MyMacro()
	asm
		asm statements
	end asm
#endmacro
Then referencing MyMacro in another module simply gets me an "Variable not declared" error.
There does not seem to be any examples anywhere I can find on how to make it visible globally.

Regards
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Declaring a Macro as global

Post by St_W »

Macros are basically simple text replacement done at compile time. So they aren't shared among modules.

Simply put them in a separate file and include that in every module.
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Declaring a Macro as global

Post by Dinosaur »

Hi All

Thanks for the response, but I thought that is what I did.
The macro is in a .bi file that is included with the Main file.
ie:
Module1 has a Project.bi as the only include.
Then Project.bi has:
#include once Declares.bi
#Include once Types.bi
#include once Module2.bas
etc
etc

The Types.bi has the macro.

Regards

EDIT:
Putting the macro in a separate .bi file and #include with every module works, but still can't see why my previous solution did not work.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Declaring a Macro as global

Post by MrSwiss »

The simplest method, I'm aware of is:
put the Macro straight into the MAIN-code-file ... (Prj_MAIN.bas),
that makes it GLOBAL (to all the other included files also).
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Declaring a Macro as global

Post by Dinosaur »

Hi All

Thanks for the responses.

Regards
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Declaring a Macro as global

Post by St_W »

I'm not sure whether we share a common understand of what a "module" is. I was referring to modules as separate compilation units, which is e.g. when you compile with "fbc main.bas module2.bas module3.bas". However, in that case you wouldn't include another module in the main module like it seems you did:
Dinosaur wrote:Then Project.bi has:
#include once Declares.bi
#Include once Types.bi
#include once Module2.bas
etc
Instead only "project.bi" should only contain:
#include once Declares.bi
#Include once Types.bi

And all modules (which need the project includes) should contain:
#include once "project.bi" (or a subset of specific header files, which would reduce compile time if your headers get large)
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Re: Declaring a Macro as global

Post by Dinosaur »

Hi All

St_W it appears I did not explain properly, as I thought my oversight was so simple that I felt dumb for asking the question.

In my projects I only compile the main basic module.

fbc Module1.bas

In Module1 the only include is:
#include once Project.bi

An example of one of my Project.bi files is:

Code: Select all

#Include once "allegro.bi"
#Include once "cgui.bi"
#include once "file.bi"
#include once "fbthread.bi"
#define IA3125                      ''If The IA3125 I/O Board is installed.
'#define AccesIO                    ''If the usb-IDIO-16 is installed.

#if defined(IA3125)
    #Include once "types.bi"
    #Include once "IA3125.bi"
    #Include once "declares.bi"
    #Include once "presets.bi"
    #Include once "IA3125.bas"
#Endif

#if defined(AccesIO)
    #Include once "libusb-1.0.bi"
    #Include once "types.bi"
    #Include once "AIO-16.bi"
    #Include once "declares.bi"
    #Include once "presets.bi"
    #Include once "AIO-usb.bas"
#endif

#Include once "Win01.bas"
#Include once "Win02.bas"
#Include once "WinHelp.bas"
#Include once "RunMod00.bas"
#Include once "RunMod01.bas"
Note that all variables in Types.bi are Shared.

So, I thought I was looking for a statement to Dim Share MyMacro ?
But obviously there isn't one.

Regards
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Declaring a Macro as global

Post by grindstone »

Not sure if it helps, but maybe try the include without "once"?
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Declaring a Macro as global

Post by St_W »

you're right, there's nothing like "dim shared" for macros, because they do not exist at runtime or even at link time.

With your understanding of modules I think the macro should be available on all lines following its initial definition. #include once should be fine too.
So if you #include .bas files in your main module after defining a macro (either in your main module or in any of its #include-d files) you should be able to use that macro there. Just make sure that you're actually building the main module; it won't work if you're trying to build your modules separately.
Post Reply