Is it mandatory to call an export function/task from an import function/task?

i am working on an export function which should be called by C++ world. I am getting this error.

Error : DPI export function/task “DPI_Test” is illegally invoked (instead of from an
import function) from user/external C/C++ code, and thus has NULL scope.
Please check if the export function is called from an import function, or
the scope is properly set via ‘svSetScope’.

Is there a way to do it without using an import function ? Also, is there a way to be in the same scope so that I do not have to call getscope and setscope ?

thanks
Sid

Is it mandatory to call an export function/task from an import function/task??

Yes. See section 35.5.3 of the IEEE 1800-2013 LRM. If you are using Modelsim/Questa, see the section Making Verilog Function Calls from non-DPI C Models in the User Manual that allows calls to exported function calls, but never task calls. You must also use svSetScope to set a context.

If you “import “DPI-C” context” and call the imported C/C++ routine, then the C/C++ code calls an exported SV task/ function from the same scope, there is no need to use svSetScope.

The concept of a thread or process in the C/C++ world is very different from that in the SystemVerilog event-driven world. Or at least it is implemented very differently. And there is no concept of an elaborated scope in C/C++. (i.e. a module may define a single function, but the module may be instantiated multiple times, creating multiple instances of the function). The DPI’s import context combines the scope and thread information from SystemVerilog and uses it when the imported C++ routine calls the exported SV routine.

Without knowing more about what you are trying to accomplish, I can’t recommend the best course of action. You might want to read a DVCon paper I wrote about some of the issues integrating C/C++ models with SystemVerilog

Easy Steps Towards Virtual Prototyping using the SystemVerilog DPI