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

duplicate symbol '_MAIN__' error

yeleisun
Beginner
1,017 Views

I have two source file with two programs, and one program file try to call a funciton in the other program file:

=====================================

$cat main.f90
program main

end program main

subroutine hello
print *,'Hello world!'
end subroutine hello

======================================

$cat test.f90
program test

call hello
end program test

 

$ifort -c test.f90

$ifort -c main.f90

$ifort test.o main.o -o test
duplicate symbol '_MAIN__' in:
test.o
main.o

 

Does someone know how to get ride of the this _MAIN__ error?

 

Thanks!

 

0 Kudos
3 Replies
Steve_Lionel
Honored Contributor III
1,002 Views

Remove "program main... end program main" from main.f90.  Your test.f90 contains the main program, main.f90 contains only a subroutine (so your files are misnamed.) You are not allowed to have two main programs; it is fine (and typical) to have only a subroutine (or function) in a separate source file.

You can, of course, simply add the subroutine to the end of test.f90. When you start writing more complex programs you can have multiple subroutines and functions in a source file. (The difference is that a function returns a value while a subroutine does not.)

0 Kudos
yeleisun
Beginner
927 Views

Thank you very much Steve for the solution.

I'm actually having several subroutines in main.f90. The main program in main.f90 called all the subroutines there. 

I create another program in test.f90 and tried to call some of the subroutines in main.f90, which do not work and give this _MAIN__error.

Since the programs' name (main and test) are different, I have thought that ifort compiler should recognize there are two different programs the user want to build.

 

Anyhow, as you suggested, I will create  file_1 contains the subroutines; file_2 contains the 1st program and  file_3 contains the 2nd program.

0 Kudos
Steve_Lionel
Honored Contributor III
915 Views

You can build only one program at a time, which means one main program across all the source files.

0 Kudos
Reply