# Can I use .o files of compiled c files and use that to compile SV files for DPI

**URL:** <https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, DPI, functions-in-system-verilog, Questa-104\
**Created:** [August 10, 2015, 12:05pm UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946 "2015-08-10T12:05:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 10, 2015, 12:05pm UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/1 "2015-08-10T12:05:45Z")

</div>

I have created one QT based C application. It takes few inputs from GUI and based on that input I need to call SystemVerilog function.  
I checked examples of DPI but for that I need to compile .c files with .sv file. So Can I use compiled .o files with .sv files to communicate each other?

---

<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:** [August 10, 2015, 4:02pm UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/2 "2015-08-10T16:02:12Z")

</div>

The mechanism for compiling C code for use with the SystemVerilog DPI is going to be tool specific. But any tool should be able to provide the necessary switches to include your object on the command line, or use gcc independently to build a shared object that can be loaded into your simulator.

Regardless of how the DPI is built, SystemVerilog requires that any C code calling a SystemVerilog function must be from the same thread where SystemVerilog code called the C code first.

Basically, a SystemVerilog simulation appears as a single thread to the C code, and any interaction between the languages has to be within the same thread.

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 11, 2015, 7:09am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/3 "2015-08-11T07:09:23Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/2):_

Thanks Dave,

So I have created shared object file .so using gcc. Now I need to call C function of shared object from SV file.

I have created import DPI-C of that function in .sv file

How to compile and link that .so file in Questasim?

vlib work  
vlog -sv -l libshared\_code.so tb\_dpi.sv  
vsim -c tb\_dpi -do “run -all”

on compiling using above command I got error like  
Failed to find user specified function ‘getRandom’ in DPI C/C++ source files.

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 11, 2015, 10:53am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/4 "2015-08-11T10:53:22Z")

</div>

_In reply to [Ankit\_Patel](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/3):_

//Created shared library using below command  
gcc -shared -fPIC -Bsymbolic -o test.so test.c main.c

//Simulating shared library with Questasim  
vlib work  
vlog -sv tb\_dpi.sv  
vsim -sv\_lib test -c tb\_dpi -do “run -all”

//Created executable using below command  
gcc -o test test.so

Now problem here is my GUI sets some variable in test.c file but my systemverilog code dosen’t gets the updated value.  
I thing Questa dosen’t get’s updated from .so at runtime. So is there a way to run executable in Questasim so that sv code gets updated value or any other suggestions?

---

<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:** [August 12, 2015, 5:46am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/5 "2015-08-12T05:46:23Z")

</div>

_In reply to [Ankit\_Patel](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/4):_

It’s going to be very difficult to help you in this forum without explaining the complete flow of code execution. The only way to update a value on the SV side is when a thread cross the language boundary - when C calls an exported SV task or function and the SV arguments get assigned to SV variables, or when SV calls an inported C task or function, and that routine returns to update the output arguments.

I’m assuming your GUI has multiple threads running, and what I said earlier still holds - the DPI can only interact with a single thread. What you are trying to do may be too difficult to discuss in this forum and I suggest you seek out expert advice that may be more local to you.

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 12, 2015, 8:47am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/6 "2015-08-12T08:47:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/5):_

Hi Dave,

Thanks for your reply. I understood the concept and now able to do what I want.

But I faced few difficulties in calling SystemVerilog tasks from C functions to insert delay.

I am receiving Fatal error as below  
Fatal: (vsim-3743) DPI task ‘import\_task’ returned non-zero value indicating disable when thread was not disabled.

If I comment printf(" C: After calling export function\n"); of C\_file.c then it works.  
I am unable to execute the remaining lines of C function after calling SV task.

Below is the code that I am trying to run.

//C\_file.c

```auto
extern int export_task(int *t);
int import_task(int *t)
{ 
  printf("C : Before calling export\n");
  export_task(t);
  printf(" C: After calling export function\n"); 
}

```

//SV\_file.sv

```verilog
program main; 
export "DPI-C" task export_task; 
import "DPI-C" context task import_task(output int t); 

int a;
task export_task(output int t); 
  $display("SV: Entered the export function . wait for some time : %0d ",$time); 
  #100 t = $stime;
  $display("SV: After waiting %0d",$time);
endtask 

initial 
begin 
  $display("SV: Before calling import function %0d",$time); 
  import_task(a);
  $display("SV: After calling import function A=%0d Time=%0d",a,$time); 
end 
endprogram

```

//Run commands

```auto
vlib work
vlog SV_file.sv C_file.c
vsim -c main -do "run -all; exit"

```

---

<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:** [August 12, 2015, 2:27pm UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/7 "2015-08-12T14:27:25Z")

</div>

_In reply to [Ankit\_Patel](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/6):_

Your export\_task() is supposed to return an int, but you did not return anything (which means a garbage value got returned). See section _35.9 Disabling DPI tasks and functions_ of the 1800-2012 LRM.

Also please use the vlog -dpiheader switch and #include that file in your C file. You will thank me later.

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 13, 2015, 5:26am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/8 "2015-08-13T05:26:46Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/7):_

Thanks Dave, Now it works…

//C\_file.c  
int import\_task(int \*t)  
{  
printf(“C : Before calling export\n”);  
if(export\_task(t)) {  
svAckDisabledState();  
printf(“C : After calling export Return 1\n”);  
return 1;  
}  
printf(" C: After calling export Return 0\n");  
return 0;  
}

Is that mandatory to call SV task from imported C function only?

---

<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:** [August 13, 2015, 6:40am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/9 "2015-08-13T06:40:04Z")

</div>

_In reply to [Ankit\_Patel](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/8):_

The LRM requires that an SV exported task only be called from a C imported task, and an SV exported function can only be called from a C imported task or function

---

<div class="post-metadata">

**Author:** ![Ankit\_Patel](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ankit_patel/32/1302_2.png) [@Ankit\_Patel](https://verificationacademy.com/forums/u/Ankit_Patel)\
**Post date:** [August 13, 2015, 11:24am UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/10 "2015-08-13T11:24:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/9):_

Thanks Dave,

I found that I can’t delay the execution of C code which requires outside simulator inputs.  
i.e.  
//SV\_file.c

```auto
program main; 

import "DPI-C" context function void import_task();

initial 
begin 
  $display("SV: Print 1 %0t",$time); 
  #10000;
  $display("SV: Print 2 %0t",$time); 
  for(int i=0; i<3; i++) begin
    import_task();
  end
  $display("SV: Print 3 %0t",$time); 
end 

endprogram

```

//C\_file

```auto
#include <stdio.h>
#include "cimports.h"

void import_task()
{ 
  int a;
  printf("Please input an integer value: ");
  scanf("%d", &a);
  printf("\nYou entered: %d\n", a);
}

```

//Simulation OUTPUT

# run -all

1  
2  
3

# SV: Print 1 0

# SV: Print 2 10000

# Please input an integer value:

# You entered: 1

# Please input an integer value:

# You entered: 2

# Please input an integer value:

# You entered: 3

# SV: Print 3 10000

Here I have two display statements before calling C function though I have to enter 3 inputs before SV simulation begins.  
No doubt I got the outputs of C function on simulation time. But I have to delay execution of C function after displaying two statements. Is is possible?

---

<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:** [August 13, 2015, 2:49pm UTC](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/11 "2015-08-13T14:49:53Z")

</div>

_In reply to [Ankit\_Patel](https://verificationacademy.com/forums/t/can-i-use-o-files-of-compiled-c-files-and-use-that-to-compile-sv-files-for-dpi/30946/10):_

This is getting too tool specific to be able to help you here.
