Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

External C++ compilation and DPI question

Status
Not open for further replies.

ghertz

Newbie level 5
Joined
Nov 1, 2019
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
97
Hi -

I'm having trouble creating a .dll file by using the gcc command on windows (MinGW). Here is the command I'm running:

command: gcc -shared -Bsymbolic -o mytest.dll mytest.cpp
error:: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: C:\Users\aashoka\AppData\Local\Temp\cc2UzHHe.o:mytest.cpp:(.text+0x3a): undefined reference to `Write' collect2.exe: error: ld returned 1 exit status

The .h file --- created using the command: vlog -sv ./mytb.sv -dpiheader ../src_cpp/experiment1.h -- is included in the mytest.cpp. And this file contains the Read and Write functions that is implemented in the mytb.sv. I also have the following lines in mytb.sv

export "DPI-C" task Write;
export "DPI-C" task Read;
import "DPI-C" context task mymain();

Any thoughts on how I can get past the dll creation step?

The files I'm using are attached.

Thanks,
Aditya
 

Can you paste the definition of Write from experiment1.h
 

If you are using a version of Questa/Modelsim within the last 8 years, you can put the C/C++ file on the vlog command line and skip the separate gcc step.
 

Here is the whole file:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#--------------------------------------------------------------------------------------------
#Experiment
#--------------------------------------------------------------------------------------------
 
#ifndef INCLUDED_EXPERIMENT1
#define INCLUDED_EXPERIMENT1
 
#ifdef __cplusplus
#define DPI_LINK_DECL extern "C"
#else
#define DPI_LINK_DECL
#endif
 
#include "svdpi.h"
 
DPI_LINK_DECL DPI_DLLESPEC
int 
mymain();
 
DPI_LINK_DECL int
Read(int addr,int* read_data);
 
DPI_LINK_DECL int
Write(int addr,int data);
 
#endif

 

@dave_59: Things work fine when I put c/c++ file in the command line. I want to get this working with the external GCC step.
 

Why do you have that requirement?

Huge code base. I don't think it makes to create a new work flow. It'd be nice to keep HW and software different. ModelSim User's Manual talks about using vlog to compile (default autocompile flow), and external compilation flow (gcc -> object file -> and link that file in modelsim). It looks like I'm following all the instructions. Can't seem to understand why things aren't working.....
 

Try
g++ -c -I<install_dir>\questasim\include mytest.cpp
g++ -shared -Bsymbolic -o mytest.dll mytest.o -L<install_dir>\questasim\win32
 

Try
g++ -c -I<install_dir>\questasim\include mytest.cpp
g++ -shared -Bsymbolic -o mytest.dll mytest.o -L<install_dir>\questasim\win32

Doesn't work. I still get an error when I try to create the .dll file

These are the commands I tried:
[1] g++ -c -IC:\intelFPGA\17.0\modelsim_ase\include -o ./mytest.o ./mytest.cpp
comments: I was able to execute this command without any issues

[2] g++ -shared -Bsymbolic -o ./mytest.dll ./mytest.o -LC:\intelFPGA\17.0\modelsim_ase\win32aloem
comments: I wasn't able to get past this step
error message: c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: ./mytest.o:mytest.cpp:(.text+0x2d): undefined reference to '`PrintHelloWorld'
collect2.exe:error:ld returned 1 exit status


#-----------------------------------------------------------------------------------
# file: mytest.cpp
#-----------------------------------------------------------------------------------

Code:
#include<stdio.h>
#include "experiment3.h"
int mymain() {
   printf("---starting test in c-domain---\n");
   PrintHelloWorld();
   return 0;
}

#-----------------------------------------------------------------------------------
# file: experiment3.h
#-----------------------------------------------------------------------------------

Code:
#ifndef INCLUDED_EXPERIMENT3
#define INCLUDED_EXPERIMENT3

#ifdef __cplusplus
#define DPI_LINK_DECL  extern "C" 
#else
#define DPI_LINK_DECL 
#endif

#include "svdpi.h"



DPI_LINK_DECL DPI_DLLESPEC
int
mymain();

DPI_LINK_DECL void
PrintHelloWorld();

#endif

#-----------------------------------------------------------------------------------
# file: mytb.sv
#-----------------------------------------------------------------------------------

Code:
module mytb;
   timeunit 1ns/1ps;
   export "DPI-C" function PrintHelloWorld;
   import "DPI-C" context task mymain();

   .<some stuff>
   .<some stuff>
   .<some stuff>
   .<some stuff>

   function void PrintHelloWorld();
      $display("HelloWorld\n");
   endfunction

[B][I]//start test[/I][/B]
initial begin
   repeat(10)@(posedge clk_reg);
   mymain();
end


endmodule
 
Last edited by a moderator:

Which object container has PrintHelloWorld() ?
 

Which object container has PrintHelloWorld() ?

PrintHelloWorld() is part of the SystemVerilog Testbench. GCC only knows about PrintHelloWorld() through the experiment3.h header file (the one generated by ModelSim)
 

The common point of all failing tests is that the functions exported from SV are not visible in the C name space. I'm not familiar with the DPI stuff, the examples I see at the web are looking slightly different, e.g. marking the SV symbols imported to C as extern. Seriously I don't even understand which file imports the SV originated symbols to the linker.
 

The common point of all failing tests is that the functions exported from SV are not visible in the C name space. I'm not familiar with the DPI stuff, the examples I see at the web are looking slightly different, e.g. marking the SV symbols imported to C as extern. Seriously I don't even understand which file imports the SV originated symbols to the linker.

You are right. The functions exported from SV is not visible to the C namespace. The experiment3.h header file does have the extern keyword. Can you elaborate on "Seriously I don't even understand which file imports the SV originated symbols to the linker" ?
 

If it is not able to find the function at link time it gives the error message.
 

I still haven't gotten this to work....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top