- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
David,
That is correct. Do note that we document that there should be a space.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page