C Function Calling SystemVerilog Function problem

i’m trying to understand how to call functions between sv and c codes using questasim in window platform.
1- the below codes gave me the below error when compiling “#vlog -sv memory.sv -dpiheader dpi_header.h mem_build.cpp”

**# mem_build.cpp:23:2: warning: no newline at end of file

mem_build.cpp:5: error: new declaration ‘void mem_build(int)’

./dpi_header.h:33: error: ambiguates old declaration ‘svLogic mem_build(int)’**

but it is suppose that the mem_build function defined without a return value it is a void, i donot understand why the compiling make header by that way.

2- when i removed the dpi_header.h from the c code,and run “vsim work.memory” it give the below fatal error

# ** Fatal: ** Error: (vsim-3828) Could not link ‘dpi_auto_compile.dll’: cmd = ‘C:/questasim_10.3\gcc-4.2.1-mingw32vc9\bin\g++.exe -shared -o “C:\Users\LENOVO~1\AppData\Local\Temp\Lenovo-PC@MOUSTAFA_dpi_8060\win32_gcc-4.2.1\dpi_auto_compile.dll” C:/SV-training/sv-ch8/work_dpi\auto_compile@\win32_gcc-4.2.1*.o “C:\Users\LENOVO~1\AppData\Local\Temp\Lenovo-PC@MOUSTAFA_dpi_8060\win32_gcc-4.2.1\exportwrapper.o” -L"C:/questasim_10.3/win32" -lmtipli’

3- when reinserted the header to the c code and edited the header to make mem_build void as below
DPI_LINK_DECL DPI_DLLESPEC
svLogic
read_file(
const char
fname);
DPI_LINK_DECL void
mem_build(
int size);
#endif
*

it give me an error in the run step “#run” as below
Fatal: (vsim-160) memory.sv(2): Null foreign function pointer encountered when calling ‘read_file’

4- after i edited the cpp code to make the read_file function as the header declaration as below
*svLogic read_file(const char fname){

i got another error in the run step
Error: (vsim-3756) The DPI exported function ‘mem_build’ must be called from a context imported tf. A call from a non-context imported tf was detected. The caller’s scope is memory.

5- when i edited in the sv code to make the mem_build with context defintion as below:
export “DPI-C” context function mem_build; // No type or args

so it give compilation error in that line
**** Error: memory.sv(3): near “context”: syntax error, unexpected context, expecting function or task**

i want to underestand what is the problem.
thanks in advance

c code:

#include <stdio.h>
#include < malloc.h>
#include < veriuser.h>
#include < dpi_header.h>
extern void mem_build(int);
int read_file(char *fname){
int cmd;
FILE *file;
file = fopen(fname, "r");
while (!feof(file)) {
cmd = fgetc(file);
switch (cmd)
{
case 'M': {
int hi;
fscanf(file, "%d %d ", &hi);
mem_build(hi);
break;
}
}
}
fclose(file);
}

sv code:

module memory;
import "DPI-C" function read_file(string fname);
export "DPI-C" function mem_build; // No type or args
initial
read_file("mem.dat");
int mem[];
function mem_build(input int size);
mem = new[size]; // Allocate dynamic memory elements
endfunction
endmodule : memory

In reply to moustafaali:

You should never modify the automatically generated DPI header file. It’s purpose is to make sure SystemVerilog’s prototype definitions match your C code prototypes. A function with no return value needs to be declared with the void type. And it is the import statement that needs a context. Your SystemVerilog code should be

module memory;
import "DPI-C" context function void read_file(string fname);
export "DPI-C" function mem_build; // No type or args
initial begin
    read_file("mem.dat");
    int mem[];
  end
function void mem_build(input int size);
  mem = new[size]; // Allocate dynamic memory elements
endfunction
endmodule : memory

In reply to dave_59:

thanks a lot it is working now with the below code:

module memory;
import "DPI-C" context function void read_file(string fname);
export "DPI-C" function mem_build; // No type or args
initial
read_file("mem.dat");

int mem[];
function void mem_build(input int size);
mem = new[size]; // Allocate dynamic memory elements
endfunction
endmodule : memory