EETOP 创芯网论坛 (原名:电子顶级开发网)

标题: 关于DPI-C的使用方式 [打印本页]

作者: oscillator_cn1    时间: 2025-3-21 15:55
标题: 关于DPI-C的使用方式
各位:

      最近正在学习使用DPI-C。我有一个C文件,如下:


   

        

                
  1. #include <svdpi.h>
  2. #include <math.h>

  3. double sin(double r){
  4.     return sin(r);
  5. }

  6. int sum(int a, int b){
  7.     reurn a+b;
  8. }

  9.         

   

    复制代码
然后,在sv中,


   

        

                
  1. module dpi_test;
  2.      import "DPI-C" function real sin(real n2);
  3.      import "DPI-C" function int sum(int a, int b);
  4.      int a, b, c;
  5.      initial begin
  6.            $display("sin(0.73)=%f", sin(0.73));
  7.            a=2;
  8.            b=7;
  9.            c=sum(a,b);
  10.            $display("a+b=%d", c);
  11.     end
  12. endmodule
            

   

    复制代码
我首先是用 vcs 直接编译sv文件和C文件:


   

        

                
  1. vcs -full64 -sverilog +v2k +cli -kdb -o ./simv -l cmp.log dpi_test.sv c_func.c -debug_all +DPI
            

   

    复制代码
然后,运行./simv,得到STACK_OVERFLOW的报错:

   

        

                
  1. Info:[STACK_OVERFLOW] Stack Overflow Detected.
  2. Note:[STACK_INCREASED] Bumping stack from 8388608 to 18846720 bytes.
  3. .....
  4. -- Stack trace follows:
  5. Dumping VCS Annotated Stack:
  6. #0 0x00000036916ac89e in waitpid() from /lib64/libc.so.6
  7. #1 0x000000369163e4e9 in do_system() from /lib64/libc.so.6
  8. ......
            

   

    复制代码
然后,如果先用gcc编译C,

   

        

                
  1. gcc -I$VCS_HOME/nclude -shard -fPIC -o libdpi.so c_func.c -lm
            

   

    复制代码
然后再用上面的vcs编译命令(去掉c_func.c),再执行./simv
前面的sin计算倒是出来结果了,但是,后面的sum函数,则得到错误提示:

   

        

                
  1. Error - [DPI-DIFNF] DPI import function not found.
  2. The definition of DPI import function/task 'sum' does not exist. Please check the started DPI import function/task is defined, and its definition is either passed in a source file at cimpile-time, or provided in a shared library specified using the LRM Annex-J options at run-time.
            

   

    复制代码
我该怎么做,才能让这两个函数都能正常使用呢?



作者: encounter_alpha    时间: 2025-3-21 18:05
import "DPI-C" context function int sum(...);  // 声明为上下文相关‌:ml-citation{ref="6,8" data="citationList"} import sum的时候添加context试试呢?

作者: LLLT    时间: 2025-3-24 10:42
c中的math.h里面是有一个sin函数的,代码里面又定义一个sin,这里会递归调用用户自己定义的这个sin吧,最好换个名字试试看?
作者: LLLT    时间: 2025-3-24 10:44
参考这里https://blog.csdn.net/zhajio/article/details/109362377
从C向SV导入函数作为一个函数(function)使用一节,这里的定义也是用的”Sin“而不是”sin“
作者: oscillator_cn1    时间: 2025-3-26 17:34


   
LLLT 发表于 2025-3-24 10:42
c中的math.h里面是有一个sin函数的,代码里面又定义一个sin,这里会递归调用用户自己定义的这个sin吧,最好 ...


好的。谢谢!





欢迎光临 EETOP 创芯网论坛 (原名:电子顶级开发网) (https://bbs.eetop.cn/) Powered by Discuz! X3.5