How to Complier c and fortran mix program in PVF?

for example:
func.c

include <stdio.h>
void sum(int x,int y){
     return x+y;
}

main.f

.........
CALL sum(1,2)
.........

how to complier c and fortran mixing problem, please help.

There are two parts to this answer: first, how do you change your C code to be callable from Fortran, and second how do you organize your code in PVF?

(1) How do you change your C code to be callable from Fortran?

/*

  • C Code callable from Fortran
    • name must have trailing underbar
    • arguments must be passed by reference
    • subroutine is of type void
      */
      include <stdio.h>
      void sum_(int * x,int * y, int * ret){
      *ret = *x + *y;
      }

! Fortran Code calling C
! - call as usual
! - use external statement to declare

external sum

integer x
integer y
integer ret

call sum(x, y, ret)

This is documented in more detail in the online help and the PGI User’s Guide provided with your preview software.


(2) How do you organize your code to build it in PVF?

You must have Visual C++ installed. You cannot compile C source files in a PVF project.

  • Create a Visual C++ static library project containing your C files.
  • Create a PVF EXE project containing your Fortran files.
  • Use the project property pages for the PVF project to set the Linker:General:Additional Library Directories and Linker:Input:Additional Dependencies properties to point to your VC static library.
  • Right-click on the PVF project node in solution explorer and select “Project Dependencies”. Check the box next to your VC library project.
  • right-click on the PVF project node again and select “Set as Startup Project”.
  • then build and run!

thank you very much.
follow this way, I can complier it successfully.

When building a Release configuration of a mixed project like this (PVF main program, VC static library), be sure that the VC General:Whole Program Optimization property is set to “No Whole Program Optimization”. Otherwise you may get obscure linker errors like “unrecognized flag ‘-Og’ in ‘p2’”.