Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28382 Discussions

fortran run time on Linux (when using -nofor-main option)

abhimodak
New Contributor I
1,066 Views

Hi 

On Linux OS:

I see that COMMAND_ARGUMENT_COUNT and  functions like that don't work when the main program is in C. My guess is that when -nofor-main option is used, the Fortran runtime library needs to be specifically included. Below is an example.

Main C program:-

#include <stdio.h>
extern void ForMethod();
int main() {
    ForMethod();
    return 0;
}

The Fortran subroutine is:-

   Module OM
      Use ISO_C_BINDING
      Implicit None
   Contains
      Subroutine ForMethod() Bind(C,Name='ForMethod')
         Integer :: nArg, mArg
         nArg = COMMAND_ARGUMENT_COUNT()
         Write(*,*) nArg
         mArg = NARGS()
         Write(*,*) mArg
      End Subroutine ForMethod
   End Module OM

 

The compile and link steps are:-

icc -c main.c

ifort -c ForSub.f90

ifort -nofor-main -o test main.o ForSub.o

Running /test something will return nArg = -1 and mArg = 0

On Windows (and without using -nofor-main), the values are 1 and 2.

Abhi

p.s. if I use icc to link then I will get (as expected) undefined symbols for for_write_seq_lis (which is, I believe, due to the write statement.

0 Kudos
6 Replies
mecej4
Honored Contributor III
1,057 Views

If you declare "int main()" instead of "int main(int argc, char *argv[])", even your C main program has no access to argc and argv[]. 

Just as the C start-up code has responsibility to put values into argc and argv and call the user's main(), and do other initial setup tasks, so does the Fortran start-up code. If you use -nofor-main, the Fortran start-up code does not get called, so COMMAND_ARGUMENT_COUNT will be undefined. Similarly for related functions.

0 Kudos
Steve_Lionel
Honored Contributor III
1,052 Views

-nofor-main isn't needed on Windows. You do have to link with the ifort run-time libraries. On Linux that doesn't happen automatically the way it does on Windows.

0 Kudos
abhimodak
New Contributor I
1,049 Views

Hi Steve

Thanks for your response.

>>> nofor-main isn't needed on Windows. You do have to link with the ifort run-time libraries. On Linux that doesn't happen automatically the way it does on Windows.

Agreed (as acknowledged in my post)  on three statements. What I am looking for is "how" do so ? That is how to link with ifort run-time-libraries === what is the command line going to looking.... probably a dumb question. I am wondering if it would be something like:

ifort -nofor-main -lLIBRARYNAME -o test ......

Abhi 

 

0 Kudos
abhimodak
New Contributor I
1,044 Views

In other words, what is the name of the fortran runtime library that should be linked.

Abhi

0 Kudos
abhimodak
New Contributor I
1,034 Views

Hi 

I see that the following (explicit setting of fortran run-time environment) works:-

 

#include <stdio.h>

extern void ForMethod();
extern void for_rtl_init_(int *, char **);
extern int for_rtl_finish_( );

int main(int argc, char ** argv) {
int io_status;
for_rtl_init_ (&argc, argv);

ForMethod();

int for_rtl_finish_( );
io_status = for_rtl_finish_( );

return 0;
}
0 Kudos
Steve_Lionel
Honored Contributor III
1,017 Views

Hmm - the Intel documentation used to tell you how to do this, but I am having difficulty finding it now. Generally the recommendation is to use the ifort command to do the linking.

Yes, calling for_rtl_init_ and for_rtl_finish_ is often needed on Linux when the main program is not Fortran.

0 Kudos
Reply