# Necessity of writing 'include "uvm\_macros.svh"

**URL:** <https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448>\
**Category:** UVM\
**Tags:** UVM, uvm\_macrossvh\
**Created:** [May 12, 2018, 11:07am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448 "2018-05-12T11:07:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![EnRoute\_zt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@EnRoute\_zt](https://verificationacademy.com/forums/u/EnRoute_zt)\
**Post date:** [May 12, 2018, 11:07am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/1 "2018-05-12T11:07:39Z")

</div>

Hi, all

I have one question about 'include “uvm\_macros.svh”. Is it necessary to write it every time when importing uvm package? I found it’s already included at the beginning of uvm\_pkg.sv. if it’s necessary could you explain it with one example? or it’s only a preventive action?

```verilog

import uvm_pkg::*;
`include "uvm_macros.svh" // Is it necessary

```

Thanks a lot  
Tao

---

<div class="post-metadata">

**Author:** ![cgales](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/c/ccd318/32.png) [@cgales](https://verificationacademy.com/forums/u/cgales)\
**Post date:** [May 12, 2018, 11:22am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/2 "2018-05-12T11:22:02Z")

</div>

_In reply to [EnRoute\_zt](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448):_

[Read this.](https://blogs.sw.siemens.com/verificationhorizons/2010/07/13/package-import-versus-include/)

---

<div class="post-metadata">

**Author:** ![EnRoute\_zt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@EnRoute\_zt](https://verificationacademy.com/forums/u/EnRoute_zt)\
**Post date:** [May 12, 2018, 12:05pm UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/3 "2018-05-12T12:05:58Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/2):_

thanks. In fact, I’ve already read this article, and benefit a lot from this excellent article. but the uvm\_marcos.svh is a macros definition file, which is included out side the package declaration.

when first compile $\<uvm\_pkg\_path\>/uvm\_pkg.sv, all the macros in uvm\_macros.svh will be defined. So when we import uvm\_pkg, the macros inside uvm\_macros.svh have already been defined, so including uvm\_macros.svh once more will have no effect? I’m not sure about this.

Tao

---

<div class="post-metadata">

**Author:** ![chr\_sue](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/chr_sue/32/1199_2.png) [@chr\_sue](https://verificationacademy.com/forums/u/chr_sue)\
**Post date:** [May 12, 2018, 2:05pm UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/4 "2018-05-12T14:05:28Z")

</div>

_In reply to [EnRoute\_zt](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/3):_

Macros cannot made available through a package. You have to read in the corresponding definitions when you need them.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 12, 2018, 3:17pm UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/5 "2018-05-12T15:17:28Z")

</div>

_In reply to [EnRoute\_zt](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/4):_

Compiler directives like the `define macros to not exist in any scope like a package. There are processed before recognizing any SystemVerilog syntax. They only exist while the current unit of code is being compiled (the compilation unit).

So it the answer to your question depends on whether the uvm\_pkg was compiled as part of a separate compilation unit from where the code the imports the uvm\_pkg, or not. Usually people use pre-compiled libraries for the uvm\_pkg, so you need to do both the import and `include.

For the other case when everything is compiled as a single compilation unit, it doesn’t matter if you repeat the `include because there are compile guards in the uvm\_macros.svh file that prevent redefinition warnings.

So you might as well `include in both cases just to keep your code more adaptable.

---

<div class="post-metadata">

**Author:** ![EnRoute\_zt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@EnRoute\_zt](https://verificationacademy.com/forums/u/EnRoute_zt)\
**Post date:** [May 13, 2018, 2:33pm UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/6 "2018-05-13T14:33:04Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/5):_

thanks, Dave and Chr\_sue, seems clearer to me.

In uvm\_pkg.sv file, code is written as below:

```verilog

`include "uvm_macros.svh"

(* mti_design_element_load_message = "uvm-1.2 Built-in" *)

package uvm_pkg;
...
endpackage

```

`include “uvm\_macros.svh” is written outside the package … endpackage. so the scope is defined by file? like uvm\_pkg.sv here

Tao

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 14, 2018, 3:23am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/7 "2018-05-14T03:23:44Z")

</div>

_In reply to [EnRoute\_zt](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/6):_  
I’ll repeat; it does not matter if the   
`include “uvm\_macros.svh” appears inside the   
uvm\_pkg;, or outside. All that matters is a macro must be `defined within the compilation unit before it can be used in the same compilation unit. You can assume the compiler pre-processing the text macros and compiler directives knows nothing about SystemVerilog syntax.

SystemVerilog allows a number of different approaches to compilation units. Each file that appears on the compiler’s command line can be a separate compilation unit. Or you can have groups of files on the command line in multiple compilation units, or you can have all files in one compilation unit. And for tools that allow multiple compilation steps, each step can be a separate compilation unit.

In all these cases, any included file is part of the same compilation unit as the file with the `Include statement.

---

<div class="post-metadata">

**Author:** ![EnRoute\_zt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@EnRoute\_zt](https://verificationacademy.com/forums/u/EnRoute_zt)\
**Post date:** [May 15, 2018, 9:26am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/8 "2018-05-15T09:26:28Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/7):_

Hi, Dave, thank you for your detailed explanation. I know better about the macros and the compilation unit know.

Tao

---

<div class="post-metadata">

**Author:** ![haykp](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/0ea827/32.png) [@haykp](https://verificationacademy.com/forums/u/haykp)\
**Post date:** [October 29, 2018, 2:29am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/9 "2018-10-29T02:29:47Z")

</div>

_In reply to [cgales](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/8):_

Seems the link is broken

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 29, 2018, 2:43am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/10 "2018-10-29T02:43:09Z")

</div>

_In reply to [haykp](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/9):_

Fixed.

---

<div class="post-metadata">

**Author:** ![kjhhgt76](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@kjhhgt76](https://verificationacademy.com/forums/u/kjhhgt76)\
**Post date:** [March 1, 2024, 3:48am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/11 "2024-03-01T03:48:09Z")

</div>

So If a compilation unit consists of two files all of which `include "foo.svh"` which has `define FOO_SVH` and some classes declarations, FOO\_SVH will only be defined once in this compilation unit but not others? If I removed one of the `include "foo.svh"`, FOO\_SVH will still be defined in this compilation unit? The file which has removed `include "foo.svh"` will still have the class declarations and no compilation error will appear?

And if two files are separated to two compilation units, FOO\_SVH will be defined twice? If I removed one of the `include "foo.svh"`, The file which has removed `include "foo.svh"` will not have the class declarations and compilation error will appear?

---

<div class="post-metadata">

**Author:** ![kjhhgt76](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/94ad74/32.png) [@kjhhgt76](https://verificationacademy.com/forums/u/kjhhgt76)\
**Post date:** [March 1, 2024, 4:06am UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/12 "2024-03-01T04:06:40Z")

</div>

Can I have one file which has different compilation units?

For example  
pkgs.sv:

```auto
package a;
    `include "uvm_macros.svh"
    import uvm_pkg::*;
    `include "classes_a.svh"
endpackage

package b;
    `include "uvm_macros.svh"
    import uvm_pkg::*;
    `include "classes_b.svh"
endpackage

```

Will there be two different compilation unit?  
If that is the case, it is required to add two uvm\_macros.svh?  
Else, there will be one compilation unit for both packages? Any change to one package will result in recompilation of both packages file?

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [March 1, 2024, 3:41pm UTC](https://verificationacademy.com/forums/t/necessity-of-writing-include-uvm-macros-svh/34448/13 "2024-03-01T15:41:31Z")

</div>

A file can only belong to one compilation unit. Any other file ` `include`’d in that file must be part of the same compilation unit. Since the “uvm\_macros.svh” file contains only ` `define` macros, it should only be included once. Please reread this entire topic along with the links mentioned.
