0
0
VerilogHow-ToBeginner · 3 min read

How to Use `include` in Verilog: Syntax and Examples

In Verilog, you use the `include directive to insert the contents of another file into your source code. This helps reuse code like macros or common definitions by writing `include "filename.v" at the top of your Verilog file.
📐

Syntax

The `include directive tells the Verilog compiler to copy the contents of the specified file into the current file at that point. The syntax is:

  • `include "filename.v": Includes the file named filename.v.
  • The filename must be in double quotes.
  • The included file can contain any valid Verilog code like macros, parameters, or module definitions.
verilog
`include "filename.v"
💻

Example

This example shows how to include a file with a macro definition and use it in a module.

verilog
`include "macros.v"

module test;
  initial begin
    $display("Macro value is: %d", `MACRO_VAL);
  end
endmodule

// Content of macros.v
// `define MACRO_VAL 42
Output
Macro value is: 42
⚠️

Common Pitfalls

  • Forgetting the double quotes around the filename causes a syntax error.
  • Including the same file multiple times without guards can cause redefinition errors.
  • Relative paths must be correct; otherwise, the compiler won't find the file.
  • Included files should not contain module instantiations if they are meant only for definitions.
verilog
`include macros.v  // Wrong: missing quotes

`include "macros.v"  // Correct

// To avoid multiple inclusion, use guards inside macros.v:
`ifndef MACROS_V
`define MACROS_V
`define MACRO_VAL 42
`endif
📊

Quick Reference

Use `include "filename.v" to insert code from another file. Always use double quotes and check file paths. Use include guards in header files to prevent multiple inclusion errors.

DirectiveDescriptionExample
`includeIncludes another Verilog file`include "defs.v"
`ifndef / `define / `endifPrevents multiple inclusion`ifndef DEFS_V `define DEFS_V // code `endif

Key Takeaways

Use `include "filename.v"` to insert external Verilog code files.
Always enclose the filename in double quotes.
Use include guards (`ifndef/define/endif) to avoid multiple inclusion errors.
Check file paths carefully to ensure the compiler finds the included file.
Included files are best for macros, parameters, and definitions, not module instantiations.