我有两个文件,文件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在两次编译时都能独立编译?
发布于 2020-11-18 03:20:18
Verilog在编译过程中与'c‘不同。在“c”中,每个源文件都是一个编译单元,并且是自包含的。所有宏定义都包含在其中。
在verilog中,宏的所有声明(以及系统verilog全局作用域中的所有声明)都是粘性的。这意味着一个源文件中的宏定义也可以在带有声明的源文件之后的其他源文件中看到。
因此,在verilog中,如果你想用不同的宏定义包含相同的文件,您需要使用'define和'undef指令,例如,
`define b
`include "a.sv"
...
`undef b
`include "a.sv"
但是,需要注意的是。在实际项目中,这种类型的包含是许多错误、不正确编译和调试问题的根源。我建议您避免使用它。
https://stackoverflow.com/questions/64883916
复制相似问题