+ Post New Thread
Results 1 to 15 of 15
  1. #1
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    External C++ compilation and DPI question

    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:myt est.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

  2. #2
    Advanced Member level 1
    Points: 5,642, Level: 17

    Join Date
    May 2005
    Posts
    460
    Helped
    105 / 105
    Points
    5,642
    Level
    17

    Re: External C++ compilation and DPI question

    Can you paste the definition of Write from experiment1.h



    •   AltAdvertisement

        
       

  3. #3
    Advanced Member level 3
    Points: 6,551, Level: 19
    Achievements:
    Created Blog entry 7 years registered

    Join Date
    Dec 2011
    Location
    Fremont, CA, USA
    Posts
    794
    Helped
    355 / 355
    Points
    6,551
    Level
    19
    Blog Entries
    4

    Re: External C++ compilation and DPI question

    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.
    Dave Rich
    Senior Verification Consultant
    Mentor Graphics Corporation



  4. #4
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    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



  5. #5
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    @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.



    •   AltAdvertisement

        
       

  6. #6
    Advanced Member level 3
    Points: 6,551, Level: 19
    Achievements:
    Created Blog entry 7 years registered

    Join Date
    Dec 2011
    Location
    Fremont, CA, USA
    Posts
    794
    Helped
    355 / 355
    Points
    6,551
    Level
    19
    Blog Entries
    4

    Re: External C++ compilation and DPI question

    Quote Originally Posted by ghertz View Post
    @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?
    Dave Rich
    Senior Verification Consultant
    Mentor Graphics Corporation



    •   AltAdvertisement

        
       

  7. #7
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    Quote Originally Posted by dave_59 View Post
    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.....



  8. #8
    Advanced Member level 3
    Points: 6,551, Level: 19
    Achievements:
    Created Blog entry 7 years registered

    Join Date
    Dec 2011
    Location
    Fremont, CA, USA
    Posts
    794
    Helped
    355 / 355
    Points
    6,551
    Level
    19
    Blog Entries
    4

    Re: External C++ compilation and DPI question

    Try
    g++ -c -I<install_dir>\questasim\include mytest.cpp
    g++ -shared -Bsymbolic -o mytest.dll mytest.o -L<install_dir>\questasim\win32
    Dave Rich
    Senior Verification Consultant
    Mentor Graphics Corporation



  9. #9
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    Quote Originally Posted by dave_59 View Post
    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
    
    //start test
    initial begin
       repeat(10)@(posedge clk_reg);
       mymain();
    end
    
    
    endmodule
    Last edited by ads-ee; 4th November 2019 at 22:52. Reason: added tags



  10. #10
    Advanced Member level 5
    Points: 12,779, Level: 27
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,938
    Helped
    416 / 416
    Points
    12,779
    Level
    27

    Re: External C++ compilation and DPI question

    Which object container has PrintHelloWorld() ?



  11. #11
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    Quote Originally Posted by srizbf View Post
    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)



  12. #12
    Super Moderator
    Points: 263,357, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,008
    Helped
    13993 / 13993
    Points
    263,357
    Level
    100

    Re: External C++ compilation and DPI question

    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.



  13. #13
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

    Quote Originally Posted by FvM View Post
    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" ?



    •   AltAdvertisement

        
       

  14. #14
    Advanced Member level 5
    Points: 12,779, Level: 27
    Achievements:
    7 years registered

    Join Date
    Apr 2010
    Posts
    1,938
    Helped
    416 / 416
    Points
    12,779
    Level
    27

    Re: External C++ compilation and DPI question

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



  15. #15
    Newbie level 5
    Points: 60, Level: 1

    Join Date
    Nov 2019
    Posts
    10
    Helped
    0 / 0
    Points
    60
    Level
    1

    Re: External C++ compilation and DPI question

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



--[[ ]]--