From: Isomorphismus on
Hello everybody,
I am trying to do a bit of mixed language programming, using fortran
90 and c++, meaning,that I want to have a fortran main program,
calling a subroutine written in c++.
here are my codes:

the fortran main program:
subroutine maximum(max)
!DEC$ ATTRIBUTES STDCALL :: maximum
integer(4) max
end subroutine

program fc
implicit none

integer(4) ::a,b

write(*,*) "Bitte geben Sie zwei Zahlen ein: "
read(*,*) a
read(*,*) b

call maximum(a,b)

end program fc

and the c++-file:

int _stdcall maximum(int a, int b){ int max;
if(a<b) {max=b;
return max;
}
return max=b;
}

As you see, I use the calling convention stdcall, as it is recommended
in the fortran help menu of my compiler. I´m using Compaq Visual
Fortran 6.

Well, everytime I get no errors in the object-files, but the linker
error LNK2001, (followed by LNK1120, which results from the previous
error) and the message "unresolved external symbol maximum@8".

Does anyone have an idea what I can do to prevent this error?

greetings,
Iso
From: Craig Powers on
Isomorphismus wrote:
> Hello everybody,
> I am trying to do a bit of mixed language programming, using fortran
> 90 and c++, meaning,that I want to have a fortran main program,
> calling a subroutine written in c++.
> here are my codes:
>
> the fortran main program:
> subroutine maximum(max)
> !DEC$ ATTRIBUTES STDCALL :: maximum
> integer(4) max
> end subroutine
>
> program fc
> implicit none
>
> integer(4) ::a,b
>
> write(*,*) "Bitte geben Sie zwei Zahlen ein: "
> read(*,*) a
> read(*,*) b
>
> call maximum(a,b)
>
> end program fc
>
> and the c++-file:
>

You omitted:
extern "C"

> int _stdcall maximum(int a, int b){ int max;
> if(a<b) {max=b;
> return max;
> }
> return max=b;
> }

as without that directive, even with stdcall calling convention, I
believe that "maximum" would still be given C++ name decoration to allow
overloading resolution.

> As you see, I use the calling convention stdcall, as it is recommended
> in the fortran help menu of my compiler. I�m using Compaq Visual
> Fortran 6.
>
> Well, everytime I get no errors in the object-files, but the linker
> error LNK2001, (followed by LNK1120, which results from the previous
> error) and the message "unresolved external symbol maximum@8".
>
> Does anyone have an idea what I can do to prevent this error?

Make sure you know what the linkage name of maximum is. I think dumpbin
is the Visual Studio utility that will report on what is included in an
object file. With extern "C" __stdcall, I'd guess maximum would be
exported as "maximum@8", but you should check to make sure.
From: Sebastian Gallinat on
Iso,

maybe the name of your external procedure is not maximum, but maximum@8,
as the error message says. This is the stdcall style decoration of the name.
Beside I see a second problem: The fortran code defines maximum as a
subroutine with one integer argument, as the c function needs two; so
the stack will be corrupted running this program.
The third problem is a misunderstanding of the fortran procedure types
subroutine and function. You defined the c-integer function maximum,
where a fortran subroutine maximum has no return value - the equivalent
in c is a void function.
And then, maybe the c function maximum should return max = a in the last
line.

You should read carefully about mixed language programming in the manual
of your compiler.

Isomorphismus schrieb:
> Hello everybody,
> I am trying to do a bit of mixed language programming, using fortran
> 90 and c++, meaning,that I want to have a fortran main program,
> calling a subroutine written in c++.
> here are my codes:
>
> the fortran main program:
> subroutine maximum(max)
> !DEC$ ATTRIBUTES STDCALL :: maximum
> integer(4) max
> end subroutine
>
> program fc
> implicit none
>
> integer(4) ::a,b
>
> write(*,*) "Bitte geben Sie zwei Zahlen ein: "
> read(*,*) a
> read(*,*) b
>
> call maximum(a,b)
>
> end program fc
>
> and the c++-file:
>
> int _stdcall maximum(int a, int b){ int max;
> if(a<b) {max=b;
> return max;
> }
> return max=b;
> }
>
> As you see, I use the calling convention stdcall, as it is recommended
> in the fortran help menu of my compiler. I�m using Compaq Visual
> Fortran 6.
>
> Well, everytime I get no errors in the object-files, but the linker
> error LNK2001, (followed by LNK1120, which results from the previous
> error) and the message "unresolved external symbol maximum@8".
>
> Does anyone have an idea what I can do to prevent this error?
>
> greetings,
> Iso