Silverfrost Forums

Welcome to our forums

C_EXTERNAL with Checkmate Win32 and Debug Win32

15 Sep 2009 3:39 #4947

I have the following fortran program: program test

        C_EXTERNAL CTEST 'ctest' (REF) : INTEGER*4

      	character*11 string
        integer*4 returnval
        data	string  /'test phrase'/
        write(*, 102) string
        returnval = CTEST(string)
        write(*, 103) returnval

102 format('here is the string written from fortan code: ', a11) 103 format('here is the returned valued from c code: ', i8)

  	end

And the following C program: #include <stdio.h> #include<stdlib.h>

extern 'C' long int ctest (char *temp, long int ctestLength) { printf('Here is the string written from c code: %s\n', temp); return 1; }

In Plato, when building with the Checkmate Win32 option, I get the link warning: WARNING the following symbols are missing: ctest C:\forttest\CheckMate\Win32\fortcode.obj (C:\FORTTEST\FORTCODE.FOR)

With the Debug Win32 option, no warnings are issued, and the program runs fine. What differences between Checkmate Win32 and just Debug Win32 could cause this problem?

Thanks

18 Sep 2009 7:41 #4977

When using /check etc extra arguments are passed so you can get problems if the linker wants to match the argument list as well as the function name. I don't know what the problem is here because you are using extern 'C' as required. You can use the MAP option with SLINK in order locate the mismatch.

Another thing to try is to use the same /check option when compiling the Fortran and the C but again this should not cause a problem.

21 Sep 2009 9:29 #4999

I do not know wheter I should start a new topic or perhaps continue here. The principle of the problem here is mixed programming with a very simple getting started example.

Both sample programms complie. My question is as follows: I cannot figure out how to actually 'mix' the languages. Must I add something to the fortran project source files, include files or references. Or must I add something to to linking process?

21 Sep 2009 1:28 #5000

Typically in mixed language programming you will have a Fortran main program that accesses a function written in C in a separate file.

You need to link everything together using SLINK. There are various ways to do this. One way would take the following form of SLINK commands...

SLINK lo main lo func file

Here main.f90 has been compiled to main.obj usinf FTN95 and func.c has been compiled to func.obj using SCC.

However, if you add your main.f90 and func.c to a Plato project then there is a reasonable chance that this will all be done for you when you click on Build.

21 Sep 2009 1:42 #5001

I tried to include both the .f90 and .c file into my project. It worked out fine. This confirms your comment that Plato does in fact take care of the linking automatically.

I must admit that this is a very cool capability 😄 of Silverfrost and Plato.

22 Sep 2009 8:35 #5008

According to the Plato online help C_EXTERNAL in the Fortran program can be replaced by STDCALL (see example above). I tried it and got the following error message:

Compiling file: test_F.f90 Linking... WARNING the following symbols are missing: ctest H:\FTN95\scratch\Debug\Win32\test_F.obj (H:\FTN95\SCRATCH\TEST_F.F90) Creating executable: Debug\Win32\test_F.exe

What is the reason for the missing symbol. How should I change the code to use STDCALL ('stdcall' convension) instead of C_EXTERNAL ('cdecl' convension)?

22 Sep 2009 12:01 #5011

You can use STDCALL provided that the function that you are calling is defined using the same calling convention, i.e. you will also need to use STDCALL in the definition of the function.

Please login to reply.