# Usage of .so files

**URL:** <https://verificationacademy.com/forums/t/usage-of-so-files/37473>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, DPI, modelsim-106d\
**Created:** [April 21, 2020, 2:45pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473 "2020-04-21T14:45:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![boryah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@boryah](https://verificationacademy.com/forums/u/boryah)\
**Post date:** [April 21, 2020, 2:45pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/1 "2020-04-21T14:45:12Z")

</div>

Hi, everyone!  
I’m trying to use DPI-C in my TB, and have some problems with adding .so files to my testbench  
my c code (i post partly, because it’s too big) loks like this:

```auto
void write_tx_output_c (
                        long* data_re_h,
                        long* data_re_l
                      ) {
  data_re_h= fir_40_l_out[HALF_UPPER];
  data_re_l= fir_40_l_out[HALF_LOWER];
}

```

i announce it in my code like this:

```verilog

package tx_fir_scoreboard_pkg;

  import uvm_pkg::*;

  import tx_fir_test_params_pkg::*;  
  import one_signal_seq_item_pkg::*;
  import fft_seq_item_pkg::*;

  import "DPI-C" context function void write_tx_output_c (
                                                          output longint data_re_h,
                                                          output longint data_re_l
                                                        );

  `include "tx_fir_scoreboard.svh"

endpackage: tx_fir_scoreboard_pkg

```

and use it like this:

```verilog

longint data_re_h, data_re_l;
write_tx_output_c (data_re_h, data_re_l);

```

i compile it with shell script:

```auto
#!/bin/csh 
set machine=`uname`;
echo $machine

if ($machine == "Linux") then
    limit stacksize u
    set STACK=;
# set GCCPATH = /usr/local/bin;
else
    set STACK = -Wl,--stack=800000000;
    set GCCPATH = /usr/bin;
endif
/cadtools/mentor/questa10.6d/questasim/gcc-5.3.0-linux_x86_64/bin/g++ -fPIC -g -W -shared -lstdc++ -g $STACK -lm -lstdc++ -std=c++14 -O3 -I. -I../../ ../c_model/c_model.cpp -o c_model.so -I /vlsi-opal/sdave/suman/questa_10.3_022415/questasim/include -DCOSIM

```

and compilation passes without errors, so i get file c\_model.so

after that i try to start simulation with this TCL (i use Modelsim) script:  
vsim {-voptargs=+acc} work.tx\_fir\_top +UVM\_TESTNAME=tx\_fir\_simple\_test -sv\_lib /eng/sandbox/bkhlopunov/standalone/tx/tx\_fir/scripts/c\_model

/eng/sandbox/bkhlopunov/standalone/tx/tx\_fir/scripts/ is the directory where c.model.so is stored.  
But after elaboration phase i get this warning in transcript:

Failed to find user specified function ‘write\_tx\_output\_c’ in DPI precompiled library search list “/eng/sandbox/bkhlopunov/standalone/tx/tx\_fir/scripts/c\_model.so”

Could anyone please tell me, what am I doing wrong? Right now i am not even sure, if this is SV problem or tool problem, that’s why i’m asking here.  
It looks to me that i’m doing everything as shown here:  
[https://verificationacademy.com/forums/systemverilog/can-i-use-.o-files-compiled-c-files-and-use-compile-sv-files-dpi](https://verificationacademy.com/forums/systemverilog/can-i-use-.o-files-compiled-c-files-and-use-compile-sv-files-dpi)

Thank you!

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 21, 2020, 4:32pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/2 "2020-04-21T16:32:22Z")

</div>

_In reply to [boryah](https://verificationacademy.com/forums/t/usage-of-so-files/37473):_

You are using the C++ compiler, but the DPI is a C interface. You need to follow the general rules for linking C with C++ code. Check your tool’s User Manual for details.

---

<div class="post-metadata">

**Author:** ![boryah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@boryah](https://verificationacademy.com/forums/u/boryah)\
**Post date:** [April 21, 2020, 7:49pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/3 "2020-04-21T19:49:41Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/usage-of-so-files/37473/2):_

Dave, thank you very much for your answer and you work here! I wonder, how many engineers have learned verification with you help.  
yes, what you say may be right, but could you please give example on how to do it modelsim 10.6 or a link where i can ask that? I do, of course, read manual but it takes days (or sometimes more) to find a soultion for every single mistake i make.  
so far i’ve found, that i should write "export “C” before every C++ function

```auto
extern "C"
int myimport(int i)
{
 vpi_printf("The value of i is %d\n", i);
}

```

is this what should be done?  
Thank you again, and I would really appreciate your help, because there are no many examples on stuff like this on the internet.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [April 21, 2020, 8:32pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/4 "2020-04-21T20:32:09Z")

</div>

_In reply to [boryah](https://verificationacademy.com/forums/t/usage-of-so-files/37473/3):_

This Mentor sponsored public forum is not for tool specific help. But…, I strongly recommend generating a header file (in Questa using the -dpiheader switch), and #include’ing that in your C++ file. That will take care of the majority of C interface mismatches.

---

<div class="post-metadata">

**Author:** ![boryah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@boryah](https://verificationacademy.com/forums/u/boryah)\
**Post date:** [April 21, 2020, 8:48pm UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/5 "2020-04-21T20:48:50Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/usage-of-so-files/37473/4):_

got it, thank you anyway)  
hopefully, ill solve this mysterious compilation flow someday)

---

<div class="post-metadata">

**Author:** ![boryah](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/b/c57346/32.png) [@boryah](https://verificationacademy.com/forums/u/boryah)\
**Post date:** [April 23, 2020, 6:17am UTC](https://verificationacademy.com/forums/t/usage-of-so-files/37473/6 "2020-04-23T06:17:47Z")

</div>

_In reply to [boryah](https://verificationacademy.com/forums/t/usage-of-so-files/37473/5):_

so, Dave was right (as always, i guess:) ). Problem was with DPI header.  
I’ve unitded all DPI-C imports in one package:

```verilog
package c_model_pkg;
  import "DPI-C" context function void write_coef_c (
                                                      input int coef_idx,
                                                      output real coef
                                                    );

  import "DPI-C" context function void read_coef_c (
                                                      input int coef_idx,
                                                      input real coef
                                                    );

  import "DPI-C" context function void write_tx_input_c (
                                                          input int data_idx,
                                                          output longint data_re,
                                                          output longint data_im
                                                        );

  import "DPI-C" context function void read_tx_input_c (
                                                        input int data_idx,
                                                        input longint data_re,
                                                        input longint data_im
                                                      );
  
  import "DPI-C" context function void write_tx_output_c (
                                                          input int data_idx,
                                                          output longint data_re_h,
                                                          output longint data_im_h,
                                                          output longint data_re_l,
                                                          output longint data_im_l
                                                        );

  import "DPI-C" context function void init_fir_c();
  import "DPI-C" context function void process_fir_c();

endpackage: c_model_pkg

```

then in modelsim i’ve executed this command to form header:

```auto
vlog -dpiheader dpiheader.h c_model_pkg.sv

```

(see page 742 in ModelSim® User’s Manual, v10.5c for details)  
and included it into my c\_model.cpp file:

```auto
#include "dpiheader.h"

```

after that i compiled c\_model.cpp with bash script that i wrote in my first message and ran the simulation with -sv\_lib flag(also wrote in my first message).  
I hope next DPI-newbie will find this topic and save a week or two :)
