A macro file to prevent including a file multiple times

Started by jemin, August 03, 2005, 04:26:59 AM

Previous topic - Next topic

jemin


Purpose:
To prevent including a file multiple times.

It works like
   #ifndef _checkin_mac_
   #include checkin.mac
   #define _checkin_mac_
   #endif
in C.


Usage:
1. Add "include CHECKIN.MAC" before using macros "CHECK_IN" and "CHECK_OUT".
2. Add the macro "CHECK_IN" to the beginning of the file.
3. Add the macro "%CHECK_OUT" to the end of the file.


Files:
ReadMe.txt      this
CHECKIN.MAC   main file
TEST.ASM      example of step 1
TEST.INC      example of steps 2, 3
ml_msg.txt      the output messages of assembling TEST.ASM
TOOLS\*      an example to do steps 1, 2, 3 in a batch.



[attachment deleted by admin]

PBrennick

jemin,
This is a nice example, but I am curious, so I will ask this question.  I have been using include files for years.  Back in the real old days, ML.EXE 5.x for example, I used include.asm; now include files have taken a different turn but are still basically the same.  My question is this; since I have always used include files and never have had a problem with multiple occurences, obviously I must be missing something:

Under what circumstances would it be possible for me to have a problem with multiple occurences?

You do not explain this, only offer a solution.

Enlighten me.
Paul
The GeneSys Project is available from:
The Repository or My crappy website

QvasiModo

Assembly programs are usually so short that this problem never arises. It does happen on large C/C++ projects though. The usual solution are include guards:

#ifndef MY_INCLUDE_H
#define MY_INCLUDE_H

// ...stuff goes here...

#endif

The equivalent in MASM is IMHO the easiest solution... these macros seem to be doing pretty much the same thing, but in a rather obscure way. :|

hutch--

Easy,


IFNDEF YourEquate
  YourEquate equ <whatever>
  ; Slop all of the include data here
ENDIF


Do this in every include file and you will never get a duplicate include file included. I am much the same view as Paul, don't duplicate include files.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

jemin

Hi Paul,

One reason is: Sometimes, for reusing of the codes, I have to write code structure like this:

A1     B1    C1       <= the elementary layer
|       | |      |
____   ____
    |         |
   A2       B2

A2 will include A1 and B1. B2 will include B1 and C1.

If I have a *.asm which includes A2 and B2, then B1 will be included twice. It's easy to prevent that if I review all the dependence, but I'm lazy to check that.
I just include those necessary files in first upper layer.

Another reason is, the macro CHECK_IN can define its variable according to its file name automatically.
This save me a little time and I can rename the files anytime.

Thanks,
Jemin