Usage of .so files

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:

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:


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:


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

i compile it with shell script:

#!/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

Thank you!

In reply to boryah:

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.

In reply to dave_59:

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

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.

In reply to boryah:

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.

In reply to dave_59:

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

In reply to boryah:

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

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:

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:

#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 :)