如何使用编译器指令 (`ifdef) 和不同的 `define's 编译文件?
How to compile a file with compiler directives (`ifdef) and different `define's?
我有两个文件,文件a和文件b。文件 a 具有基于是否定义 'b' 的编译指令。
a.sv中的代码如下:
module a_module()
initial begin
`ifdef b
$display("This is compiled in file b");
`else
$display("This is compiled in file a");
`endif
end
endmodule: a_module()
b.sv中的代码如下:
`define b 1
`include a.sv
module b_module()
a_module a_module();
endmodule: b_module()
尽管在导入文件 a 之前定义了 'b',运行 两个文件都会输出“This is compiled in file a”。
这是为什么?我如何构建我的代码,以便 a.sv 两次独立编译?
Verilog在编译处理上与'c'不同。在 'c' 中,每个源文件都是一个编译单元并且是独立的。所有宏定义都包含在其中。
在 verilog 中,所有宏声明(以及系统 verilog 全局范围内的所有声明)都是粘性的。这意味着一个源文件中的宏定义也可以在其他带有声明的源文件中看到。
因此,在 verilog 中,如果您想包含具有不同宏定义的同一文件,则需要使用 `define 和 `undef 指令,例如,
`define b
`include "a.sv"
...
`undef b
`include "a.sv"
但是,请注意。在实际项目中,这种类型的包含是许多错误、不正确的编译和调试问题的根源。我建议你避免使用它。
我有两个文件,文件a和文件b。文件 a 具有基于是否定义 'b' 的编译指令。
a.sv中的代码如下:
module a_module()
initial begin
`ifdef b
$display("This is compiled in file b");
`else
$display("This is compiled in file a");
`endif
end
endmodule: a_module()
b.sv中的代码如下:
`define b 1
`include a.sv
module b_module()
a_module a_module();
endmodule: b_module()
尽管在导入文件 a 之前定义了 'b',运行 两个文件都会输出“This is compiled in file a”。
这是为什么?我如何构建我的代码,以便 a.sv 两次独立编译?
Verilog在编译处理上与'c'不同。在 'c' 中,每个源文件都是一个编译单元并且是独立的。所有宏定义都包含在其中。
在 verilog 中,所有宏声明(以及系统 verilog 全局范围内的所有声明)都是粘性的。这意味着一个源文件中的宏定义也可以在其他带有声明的源文件中看到。
因此,在 verilog 中,如果您想包含具有不同宏定义的同一文件,则需要使用 `define 和 `undef 指令,例如,
`define b
`include "a.sv"
...
`undef b
`include "a.sv"
但是,请注意。在实际项目中,这种类型的包含是许多错误、不正确的编译和调试问题的根源。我建议你避免使用它。