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

Correctly using DLLEXPORT and ALIAS

Bennett_W_
Beginner
2,505 Views

Hello, I am attempting to make use of a number of fortran functions and subroutines in a C++ project by using a generated .lib file. When compiling my C++ project, I get a linker error:

error LNK2019: unresolved external symbol _Leakage_SPH referenced in function

The function is called with Leakage_SPH (note no underscore), so I think this is the root of my problem. When I check the fortran code for this subroutine, I see:

Subroutine Leakage_SPH(Nu0,FracLkgTot,tlkg,NHoles,
     +                       ObjDat, holeAreas, holeHeights)

!DEC$ATTRIBUTES DLLEXPORT, C :: Leakage_SPH
!DEC$ATTRIBUTES ALIAS : '_Leakage_SPH' :: Leakage_SPH
!DEC$ATTRIBUTES REFERENCE :: FracLkgTot

This appears to give an alias of _Leakage_SPH so that the definition can be found. But something isn't working. The code compiles in fortran and the library is generated and placed in the correct spot. One thing I noticed is this remark that I get when compiling the fortran code:

remark #5082: Directive ignored - Syntax error, found ':' when expecting one of: , <END-OF-STATEMENT> ;

This appears to be pointing to my ALIAS line. Can anyone see what error I've made? Apologies if I've messed up any formatting; this is my first time posting on this forum (and indeed, my first time working with fortran at all as I usually work in C++). Thanks.

0 Kudos
1 Solution
Steven_L_Intel1
Employee
2,505 Views

It looks as if you are using the obsolete fixed-form source. Also, there should be a blank between !DEC$ and ATTRIBUTES. If you put the blank in, you'll be ok. But let me recommend to you that you use the modern free-form source. Name the file with a .f90 file type. You can have longer line lengths and if you want to continue a line, add a & at the end of the line to be continued. For example:

Subroutine Leakage_SPH(Nu0,FracLkgTot,tlkg,NHoles, &
                            ObjDat, holeAreas, holeHeights)

!DEC$ ATTRIBUTES DLLEXPORT, C :: Leakage_SPH
!DEC$ ATTRIBUTES ALIAS : '_Leakage_SPH' :: Leakage_SPH
!DEC$ ATTRIBUTES REFERENCE :: FracLkgTot

You can also start code in column 1 with free-form source.

View solution in original post

0 Kudos
7 Replies
Steven_L_Intel1
Employee
2,506 Views

It looks as if you are using the obsolete fixed-form source. Also, there should be a blank between !DEC$ and ATTRIBUTES. If you put the blank in, you'll be ok. But let me recommend to you that you use the modern free-form source. Name the file with a .f90 file type. You can have longer line lengths and if you want to continue a line, add a & at the end of the line to be continued. For example:

Subroutine Leakage_SPH(Nu0,FracLkgTot,tlkg,NHoles, &
                            ObjDat, holeAreas, holeHeights)

!DEC$ ATTRIBUTES DLLEXPORT, C :: Leakage_SPH
!DEC$ ATTRIBUTES ALIAS : '_Leakage_SPH' :: Leakage_SPH
!DEC$ ATTRIBUTES REFERENCE :: FracLkgTot

You can also start code in column 1 with free-form source.

0 Kudos
Bennett_W_
Beginner
2,505 Views

Thanks, Steve. Adding the space did it. For whatever reason I just didn't notice that when I was searching for solutions earlier. Also, I am using free-form source, but I believe this code was originally written using the old form, which is why the formatting is a little odd. Regardless, it compiles fine now. Much appreciated!

0 Kudos
Steven_L_Intel1
Employee
2,505 Views

Well, that + in column 6 of the second line is definitely fixed-form source. The source you showed would not be accepted in free-form. Glad to hear it was something simple.

FWIW, the blank would be optional (though still recommended) if you used free-form source.

0 Kudos
JVanB
Valued Contributor II
2,505 Views

Another problem with old code is that it tends to assume 32-bit platforms. The above would fail for a 64-bit compile and link. If it were changed to

!DEC$ ATTRIBUTES DECORATE, ALIAS : 'Leakage_SPH' :: Leakage_SPH

I think it would be an improvement.

 

0 Kudos
Steven_L_Intel1
Employee
2,505 Views

Or if one wants to be more modern:

Subroutine Leakage_SPH(Nu0,FracLkgTot,tlkg,NHoles, &
                            ObjDat, holeAreas, holeHeights) BIND(C,NAME="Leakage_SPH")
Use, intrinsic :: ISO_C_BINDING
!DEC$ ATTRIBUTES DLLEXPORT :: Leakage_SPH

You'll then need to add the VALUE attribute to the arguments that are passed by value. You didn't include those, so it would look something like:

integer(C_INT), VALUE :: NHoles

Fortran has extensive C interoperability features and for best portability you should use them.

0 Kudos
DavidWhite
Valued Contributor II
2,505 Views

Steve,

Is the blank between !DEC$ and ATTRIBUTES only mandatory for fixed source files?  I have been using !DEC$ATTRIBUTES in my free form files for many years.

David

0 Kudos
Steven_L_Intel1
Employee
2,505 Views

David,

That is correct. Do note that we document that there should be a space.

0 Kudos
Reply