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

compiler produces wrong symbol in Fortran to C call

Intel_C_Intel
Employee
1,188 Views
I am calling a C function defined in a static library as:
extern "C" long getnumparams(long* modHandle);
Dumpbin shows the object name as:
_getnumparams
which is what I would expect.
My Fortran codedefinesthis interface:
interface
integer function getnumparams(id)
!dec$attributes c, alias : '_getnumparams' :: getnumparams
integerid
end function getnumparams
end interface
and calls the routine thus:
numparams = getnumparams(myId)
The linker complains that it can't find
Linking...
Creating library Debug/myLib.lib and object Debug/myLib.exp
myRoutine.obj : error LNK2001: unresolved external symbol _GETNUMPARAMS@4
In other words, for an interface declaring a C function, it seems that the compiler is not applying the C attribute, i.e. not removing the @argument size name decoration. Can anyone see what I'm doing wrong, or is this a compiler bug?
0 Kudos
8 Replies
Intel_C_Intel
Employee
1,188 Views
btw, I am using CVF 6.6b.
0 Kudos
Steven_L_Intel1
Employee
1,188 Views
I would say that the interface isn't visible where you're doing the call.Note that the lowercase alias isn't used either.
0 Kudos
Intel_C_Intel
Employee
1,188 Views
Thanks for the quick reply Steve. I did manage to fix it almost as soon as I posted the second message! I put the interface into a module and it linked nicely.
However, I am still a bit confused. You say the interface isn't visible when the call is made. The interface was declared with the variable declarations in the subroutine that the C function was called from. The subroutine is in a fixed format file (.for). Thenew module is contained in a free format file.
What part of this is the magic incantation that makes it work?
0 Kudos
Steven_L_Intel1
Employee
1,188 Views
I would have to see the actual source in order to comment intelligently.
0 Kudos
Intel_C_Intel
Employee
1,188 Views
Here is an approximation of the 'before' code :
Code:
	subroutine someroutine()
	implicit none
	interface
	integer function getnumparams(id)
	!dec$attributes C, alias : '_getnumparams' :: getnumparams
			integer id
		end function getnumparams
	end interface
	
	integer nparms
	integer myid
!
	myid = 12
	nparms = getnumparms(myid)

	end

Moving the interface from this method to a new module fixes the link errors.
0 Kudos
Steven_L_Intel1
Employee
1,188 Views
Got it. You went past column 72 in the directive, since you're using fixed source form.
0 Kudos
Intel_C_Intel
Employee
1,188 Views
D'oh! Is my face red! Thanks for your help Steve.
0 Kudos
Steven_L_Intel1
Employee
1,188 Views
Happens all the time... Things such as this are why I always use free-form in my own coding.
0 Kudos
Reply