Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29279 ディスカッション

How do I export symbols to another DLL

Simon_Geard
新規コントリビューター I
1,741件の閲覧回数

The essence of the problem is shown below:

! In DLL 1 (Fortran)

! ==> File: my_mod.f90
module my_mod

    type my_type
    contains
    end type my_type

contains

    subroutine my_sub() bind(c,name='MY_SUB')
    !DEC$ ATTRIBUTES DLLEXPORT :: MY_SUB
    end subroutine my_sub

end module my_mod
! ==> End file	

! File: another.for
      subroutine another_sub
          use my_sub
	      type(my_type) :: t
      end subroutine another_sub
! ==> End file	
	  
! MY_SUB is also specified in the EXPORTS section of the .def file for the DLL

! =============================================================================


// In DLL 2 (C++)
// File: dll2.cpp
extern "C" {
    void MY_SUB();
};
	
void dll2_proc() {
	MY_SUB();
}
// ==> End file	

When I try to build DLL2 I get a link error that MY_SUB is missing unless I include the 'another.for' file in DLL1, so the behaviour is that the symbol MY_SUB is exported iff the module is used in DLL1; note that the subroutine another_sub has to be called in another part of DLL1 that gets used elsewhere. Adding the symbol to the .def file and DLLEXPORTS directive has no effect. I have tried this with VS2010 &IFV14, and VS2012 & IVF16 and the result is the same. Is this a known problem with a known solution?
 

0 件の賞賛
3 返答(返信)
mecej4
名誉コントリビューター III
1,741件の閲覧回数

When you built the first DLL, an import library was built (probably with the same name as the DLL and with ".lib" as suffix). Use that library in the command used to build the second DLL. That import library contains information regarding where the actual machine code for the function is to be found (in the first DLL, not in the import library) and how to link to it.

Simon_Geard
新規コントリビューター I
1,741件の閲覧回数

Thanks for that. I've tried it and it creates a different problem (multiple symbol definitions). I think this is probably a cosnequence ot the way we build our application.

The solution contains about 70 projects and comprises C++, C# and Fortran code. For the C# code there is a 1:1 correspondence with directories but the C++ and Fortran code are mixed together. Since it isn't possible to have C++ and Fortran in the same project file (is this a VS restriction?) we build a '.lib' file from the Fortran and include that in the DLL created from the C++. What is happening here is that the MY_SUB symbol is not used and so is never loaded from the '.lib' file.

I have found a solution: set Force Symbol References to MY_SUB in the Linker>Input section of the C++ project file and the Fortran project file  (DLL1 in my example).
 

mecej4
名誉コントリビューター III
1,741件の閲覧回数

There are a number of linker options (such as /force:multiple) that are intended to resolve problems of the type that you described, and I have had several occasions to use those options. I do my builds with makefiles or batch files, but I am sure that you can also specify such linker options in the VS project/solution property tabs.

Of course, if you have two different routines with the same linker name, whether in static or dynamic libraries, you have to be careful and make sure that the appropriate one is used in each linking step, but you are probably on top of such issues given the size of your project.

返信