f90main.o error with undefined main under linux

Hello,

I try to call a C function without using the underscore in a Fortran program, but I got a error regarding a undefined main.

I would like to know if I don’t miss any compilation switches.

Here the example, I try to compile:

foo.c:
#include <stdio.h>

void cprint(char* string){
printf("%s", string);
}
main.f90:
program main
implicit none

call cprint(" Hello world!\n"// char(0))

end program main

% gcc -g -c foo.c
% pgf90 -Mnounderscoring -g -c main.f90
% pgf90 -pgf90libs -g -o bar.out main.o foo.o
/opt/pgi/linux86/6.2/lib/f90main.o(.text+0x77): In function main': : undefined reference to MAIN_’

Regards,

Maxime

Hi Maxime,

The most portable solution to the Fortran to C underscoring problem is to write a Fortran interface wrapper function. While a little extra coding, it will save you countless headaches in the future.

For example, you can add the following:

% cat foo.c
#include <stdio.h>
#ifdef DOUBLE_UNDERSCORE
#define FFUNC(X, Y) X##__ (Y var) { X(var); }
#define FFUNC2(X, Y, Z) X##__ (Y var, Z var2) { X(var, var2); }
#else
#define FFUNC(X, Y) X##_ (Y var) { X(var); }
#define FFUNC2(X, Y, Z) X##_ (Y var, Z var2) { X(var, var2); }
#endif

void cprint(char* string){
printf("%s", string);
}

void FFUNC(cprint, char*)

% gcc -g -c foo.c
% pgf90 -V6.2-4 -g -c main.f90
% pgf90 -V6.2-4 -o bar.out main.o foo.o
% bar.out
 Hello world!

Hope this helps,
Mat

It’s working great!

Thank you for your time, Mat.