creating dll to link with delphi program
Brad Finney
brad.finney@humboldt.edu
Sun Oct 23 18:57:00 GMT 2011
On 10/23/2011 11:21 AM, Steve Kargl wrote:
> On Sun, Oct 23, 2011 at 11:07:09AM -0700, Brad Finney wrote:
>>
>>
>> On 10/23/2011 7:18 AM, Tobias Burnus wrote:
>>> Brad Finney wrote:
>>>> SUBROUTINE PrintOutX(x)
>>>> !GCC$ ATTRIBUTES DLLEXPORT::PrintOutX
>>>> write(*,*)x
>>>> END SUBROUTINE PrintOutX
>>>>
>>>> if I use
>>>> gfortran -c -fno-leading-underscore -fno-underscoring testcase.f90
>>>>
>>>> The object file contains -export:"printoutx" which does not preserve
>>>> the case of the requested export name.
>>>
>>> In gfortran - contrary to ifort -, you have to use C binding, cf.
>>> http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html
>>>
>>> SUBROUTINE PrintOutX(x) bind(c, name="PrintOutX")
>>> !GCC$ ATTRIBUTES DLLEXPORT::PrintOutX
>>> use iso_c_binding
>>> real(c_float) x
>>> write(*,*) x
>>> end
>>>
>>>
>>> That should work without special options. Note: Depending on the caller,
>>> you might need to add "stdcall" - but the default "cdecl" could also be
>>> correct.
>>>
>>> Tobias
>>>
>>
>>
>> I guess I did not make myself clear enough with my example. The problem
>> is not with the argument x, it is with the leading and trailing
>> underscores added to the procedure names. If I use
>>
>> -fno-leading-underscore
>>
>> then the object code contains references to gfortran library routines
>> without the additional underscore and the linker can't resolve those
>> references. If I don't use the -fno-leading-underscore option, then my
>> procedure names have a leading underscore which the previously compiled
>> main program can't find since it is looking for names lacking the
>> leading underscore.
>>
>> What I need is a way to turn off leading (and trailing) underscores for
>> my routines but not gfortran library routine names. I am also concerned
>> that gfortran does not seem to be preserving the case of the requested
>> exported procedure names.
>
> Did you try Tobias's suggestion
>
> laptop:kargl[213] cat foo.f90
> subroutine foo(n) bind(c, name="Foo")
> use iso_c_binding
> integer(c_int) :: n
> n = n + n
> end subroutine foo
>
> subroutine bar(n)
> use iso_c_binding
> integer(c_int) :: n
> n = n + n
> end subroutine bar
> laptop:kargl[214] gfc4x -c foo.f90
> laptop:kargl[215] nm foo.o
> 00000000 T Foo
> 00000016 T bar_
>
> foo and bar are identical except for explicitly namng foo
> as Foo in the bind() expression. Note case is preserved and
> there is no trailing underscore.
>
Yes, I did try his suggestion, but missed the addition of the bind(c,
name= " "). Thanks for getting me to read more carefully! That does
correct the case issue, but not (for me) the underscore issue, which is
very puzzling. I took the exact code you listed and got a leading
underscore on Foo and bar.
F:\FlowReader>gfortran -c foo.f90
F:\FlowReader>nm foo.o
00000000 b .bss
00000000 d .data
00000000 r .eh_frame
00000000 t .text
00000000 T _Foo
00000016 T _bar_
This underscore carries over into a dll created with
gfortran -shared -o foo.dll foo.o
nm foo.dll
6ae81280 T _Foo
If I instead do
gfortran -fno-leading-underscore -c foo.f90
if get no leading underscore
nm foo.o
00000000 b .bss
00000000 d .data
00000000 i .drectve
00000000 r .eh_frame
00000000 t .text
00000000 T Foo
00000016 T bar_
but then creating the dll fails
F:\FlowReader>gfortran -shared -o foo.dll foo.o
Cannot export Foo: symbol not found
Cannot export bar_: symbol not found
collect2.exe: error: ld returned 1 exit status
F:\FlowReader>gfortran -shared -fno-leading-underscore -o foo.dll foo.o
Cannot export Foo: symbol not found
Cannot export bar_: symbol not found
collect2.exe: error: ld returned 1 exit status
Brad
More information about the Fortran
mailing list