cwindowsfortranpgi

Problem compiling C/Fortran code together with pgi on windows: "main already defined"


Below is a toy example of the C/Fortran files I want to compile together.

The C file

void testfunc();

int main(void)
{
    testfunc();
}

The Fortran file

subroutine testfunc() bind (C, name = "testfunc")
    write(*,*) "Hello World!"
end subroutine

Using gcc, I can generate a binary with the command

gfortran -o my_prog main.c testfunc.f90

However, when I try the same with pgf90

pgf90 -o my_prog main.c testfunc.f90

I get the following error message:

main.obj : error LNK2005: main already defined in f90main.obj
f90main.obj : error LNK2019: unresolved external symbol MAIN_ referenced in function main

Is there a standard procedure for compiling C+Fortran with pgi on Windows?


Solution

  • Add the flag "-Mnomain" to the link to have the compiler not include the F90 main object to the link and instead use the user supplied C main.