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

How to split a fortran file into multiple files

hamid3252
Beginner
2,529 Views
Hi every body,
I am a C++ programmer and new to fortran. Could you please let me know how I can slip a long Fortran code in to multiple files, like what we normally in C++ by include?
Thank you,
0 Kudos
6 Replies
mecej4
Honored Contributor III
2,529 Views
Fortran provides the following statement (surprise!)

include character-literal-constant

However, splitting a big source code into chunks and 'include' segments is probably not a good style of programming, and may create many headaches. Consider using modules instead.

Some Fortran compilers, including Intel Fortran also provide C-style preprocessors.
0 Kudos
TimP
Honored Contributor III
2,529 Views
Fortran supports MODULE syntax, "to group related procedures and data together," along with both its own form of INCLUDE and, for most compilers, a C-compatible form.
0 Kudos
hamid3252
Beginner
2,529 Views
Thank you! Since I am using this fortran program for ABAQUS (a modeling software), it is necessary the output to be just one obj file. So if I use module, it will be split into multiple obj files. I am not sure if include can help me or not in this regard which is aridiculousness of that modeling software. Shame on its developers! Do you have any other idea?
Thanx!
0 Kudos
Steven_L_Intel1
Employee
2,529 Views
Just do it with INCLUDE as you do in C++.

include 'file1.f90'
include 'file2.f90'
include 'file3.f90'

etc.
0 Kudos
hamid3252
Beginner
2,529 Views
Thanx! But it gives me an error as I have introduced a subroutine in the first line of the included file. Anyway, I could solve the problem by changing the project type from a stand-alone to static link library, so all objs are now included in a lib / obj file, which is usable for the modeler software.
0 Kudos
Steven_L_Intel1
Employee
2,529 Views
Odd - I can't think of a reason why you should get an error assuming each file was compilable on its own. Can you attach a ZIP of an example?
0 Kudos
Reply