Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

How to compile and use DLL

moropera
Beginner
3,007 Views

I would like to use DLLs in my programs.

In Visual Studio I compiled a simple main into his project, and in another project I compiled the function into DLL (see attached)

 

When I compile main I get: I:\FORTRAN_INTEL_TEST\Uso_DLL\BinDbg\DLL1.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2C8

According to me in the properties of the main and the DLL I have not introduced the right data. But I don't know what they are.

You can help me?

0 Kudos
1 Solution
MWind2
New Contributor III
2,971 Views

Dll1 code change:

 

integer function myfunction(a, b)
!DEC$ ATTRIBUTES DLLEXPORT::myfunction
implicit none
      integer, value :: a, b
      myfunction = a + b
end function myfunction

 

In a VS Solution like Dll1 with exe fmain:

<fmain project>->Linker->Input Dll1.lib
<fmain project>->Linker->Additional Library Depencies $(SolutionDir)Dll1\$(OutDir)
<fmain project>->Build Events->PreBuild Event->Command Line copy $(SolutionDir)Dll1\$(OutDir)\Dll1.dll $(SolutionDir)$(ProjectName)\$(OutDir)

View solution in original post

0 Kudos
8 Replies
mecej4
Honored Contributor III
2,983 Views

There are many settings and check boxes in Visual studio; you have not shown us what settings you used. The following commands, executed in an Intel OneAPI command window-X64, will suffice to show that there is no problem with the source files.

 

ifort /dll fun.f90 /link /export:MYFUNCTION
ifort main.f90 fun.lib

Note that the first command generates not only the DLL that is desired, but also the import library FUN.LIB, and it is that library file that is used in the next step. Based on the error message that you reported, I wonder if you entered "FUN.DLL" where you should have entered "FUN.LIB" in the Visual Studio settings.

moropera
Beginner
2,957 Views

Thanks mecej4. I solved my problem with the suggestion of MWind2.

I appreciated your support

0 Kudos
MWind2
New Contributor III
2,972 Views

Dll1 code change:

 

integer function myfunction(a, b)
!DEC$ ATTRIBUTES DLLEXPORT::myfunction
implicit none
      integer, value :: a, b
      myfunction = a + b
end function myfunction

 

In a VS Solution like Dll1 with exe fmain:

<fmain project>->Linker->Input Dll1.lib
<fmain project>->Linker->Additional Library Depencies $(SolutionDir)Dll1\$(OutDir)
<fmain project>->Build Events->PreBuild Event->Command Line copy $(SolutionDir)Dll1\$(OutDir)\Dll1.dll $(SolutionDir)$(ProjectName)\$(OutDir)

0 Kudos
moropera
Beginner
2,917 Views

Thanks a lot MWind2 your suggestions solved my problem.

A question: The command is essential: <fmain project>->Build Events->PreBuild Event->Command Line copy $(SolutionDir)Dll1\$(OutDir)\Dll1.dll $(SolutionDir)$(ProjectName)\$(OutDir)

because everything works even without it.

I take advantage of your kindness:

where can I find the contents of the variables $(SolutionDir), $(OutDir) etc

Thanks again for your availability

0 Kudos
Steve_Lionel
Honored Contributor III
2,907 Views

Those variables are defined by Visual Studio based on what you have specified in the solution and project properties. For example:

Steve_Lionel_0-1687870582626.png

 

0 Kudos
moropera
Beginner
2,895 Views

Thanks Lionel,

but I suppose I coud know which string is associated to $(ConfigurationName) and to all the others.

Btw in my Properties page I've not the "User Compiler" option.

 

Sorry for my insistence

0 Kudos
MWind2
New Contributor III
2,883 Views

Remember that there are different Platforms whose settings are independently set, too.

0 Kudos
Steve_Lionel
Honored Contributor III
2,892 Views

ConfigurationName is typically Debug or Release, and is selected by you before you build the project. I didn't mean to highlight "User compiler" in that screenshot.

In other words, these variables are taken from project and solution properties.

0 Kudos
Reply