ifort multiple definition of `main

ifort multiple definition of `main

I am trying to compile my code with intel fortran and C++ compiler,but there is something wrong with me.

ifort -o aple test_nc.o cam.o-lesmf -nofor_maintest_nc.o In function 'main'test_nc.F90:1: multiple definition of `MAIN__'cam.o:/cam.F90:5: first defined here

I check test_nc.f90, it have a main,but when I check the cam.f90 ,it only have a cam,without main...
according to your suggest ,I add-nofor_main ,but it dosn't work!

can you help me ? Thank you .

11 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
Casey's picture

What is on line 5 of cam.F90? It would be helpful if you could attach some code from the files involved in your error. 

#include <misc.h>
#include <params.h>

#if ( !defined SCAM )
program cam
!-----------------------------------------------------------------------
!
! Purpose: Entry point for NCAR CAM
!
!-----------------------------NOTICE------------------------------------
!
! Community Atmospheric Model, $Name: cam3_1_brnchT_release01 $ (CAM)
!
!
! Method: Call appropriate initialization, time-stepping, and finalization routines.
!
! Author: CAM Core Group
!
!-----------------------------------------------------------------------
use shr_kind_mod, only: r8 => SHR_KIND_R8
use pmgrid
use dycore, only: dycore_is
use history, only: bldfld, intht
use units
use restart, only: read_restart
use time_manager, only: get_nstep
use abortutils, only: endrun
#if ( defined SPMD || defined COUP_CSM )
use mpishorthand, only: mpicom, nsend, nrecv, nwsend, nwrecv
#endif

these is a part of cam.f90,help you can find the error  ! thank you .

mecej4's picture

The name that you place in the PROGRAM <xyzname> statement is immaterial -- it gets replaced by MAIN (or _MAIN or something similar) by the compiler. You are allowed to have only one source code unit with an entry point, and you are attempting to use two.

Casey's picture

Yep, having 2 program statements in the code you are linking will result in 2 MAIN functions. You can make the one that is not your entry point into a function or subroutine. Also I'm not sure you are using -nofor_main correctly. It is my understanding that it is meant to be used when one of your object files is from c/c++ source that contains a main() function and inhibits I fort from linking the FORTRAN startup code. I don't think it is able to be used to pick one of 2 FORTRAN entry points. 

Thanks for your help .You not only let me know my error more extensively, but also let me know the use of -nofor_main.

I am trying to compile and link  23 fortran source files using ifort. After successful compilation, i can't pass the linking phase because of the following error.

xyz.o: In function 'MAIN_'

xyz.f: in(text+0X0):multiple definition of 'MAIN_'

new.o:new.f  in(text+0X0):first defined here

make ***[test] error1

Any help please?

 

 

As per the messages, the source files xyz.f and new.f both contain a main program in terms of the program unit the compiler has seen in those files.

Look at both source files to determine which should serve as the main program. It is possible this occurred because either xyz.f or new.f is missing what one may have intended to be a SUBROUTINE or FUNCTION for the program unit in the file. It is also possible one of those files already contains an explicit PROGRAM statement to identify the main program in which case it should be more obvious which file you need to modify. In the absence of a PROGRAM statement the compiler assumes a program unit seen is a main program and thus applies and implicit PROGRAM MAIN statement.

The thing is new.f is the main program and it has a PROGRAM statement.

All the source files work fine during compilation and linking in windows using visual studio .But, I wanted to compile the sources for Linux and link them to single application using ifort compiler. Then '#' were not recognized and i replaced them by '!dec$ '. and I also convert the sources in to linux file system. I used respective Linux libraries. That was the only change I made on the source code.

I am guessing that '!dec$ ' replacement creats this mess.

 

mecej4's picture

Yes, replacing the '#'s in the first columns of source files by !dec$ can make a mess . Restore the original files with '#'s in them, and use the -fpp compiler option. That will invoke the preprocessor pass and then the compiler will compile the output of the preprocessor.

The Linux version of IFort follows the convention that files ending in .F90 or .F should be preprocessed, so another way of fixing your problem is to name those files that need preprocessing with .F or .F90, leaving the other files with .f or .f90 suffixes.

Thanks! That helped a lot!

Login to leave a comment.