Write my main program and module in different files

When I write my main program and module in different files, it can’t work well.

I give an example.
Since I’m a newcomer, I can only put up one picture and I’ll give the details in words. I have two files, one is bin.f90, the other is bin_module.f90. These two files are in the same folder, in which bin is my main program, it uses bin_module.f90, but the compilation has not been able to pass!

program bin
use bin_module

implicit none
i = 1
write (*, *) i
end program bin


module bin_module
implicit none

! Trival variables
integer :: i


end module bin_module

Here’s the result.

It confuses me for many days.

Welcome to the forum.

The file bin_module.f90 need not contain a contains line when there are no subroutines or functions that are to be contained.

Your build system may have to be coaxed into ensure that it has compiled bin_module.f90 without errors before attempting to compile bin.f90. It may also need encouragement to get it to display build errors to the user.

Although the contains statement is unnecessary, as pointed out, using either PowerShell (which the OP is using, based on the error message) or Windows CMD, the command gfortran bin_module.f90 bin.f90 creates an executable.

I tried your suggestion, unfortunately the compilation still doesn’t pass.
bin_module.f90 just has few lines.I think no error in bin_module.f90.

Your build system is not issuing the correct gfortran command to compile and link the two source files. Note that the first command in your screenshot should have contained bin_module.f90 (or bin_module.o, if that file has already been compiled).

This is my folder.If I have a .mod file, does that mean bin_module_f90 has been compiled?

If not, how I can compile it?

When bin_module.f90 is compiled, two files are generated – bin_module.mod and bin_module.o . Your IDE is showing the .mod file. Presumably, the same subdirectory also contains the object file bin_module.o, which should be linked with bin.o .

This post requires an understanding of the need for .mod files to be present for subsequent .f90 files that reference the .mod files via “use bin_module”. Order is important, which is not guaranteed by “gfortran -c *.f90”

I am also confused as to when bin_module.o is generated or required for linking. Certainly an empty contains section in the module definition does not help.

In the example above, only bin_module.mod is required to be present for bin.f90 to be compiled.

A set of interactive commands for cmd.exe that should work are:
gfortran -c bin_module.f90
grortran -c bin.f90
gfortran bin.o -o bin.exe
gfortran bin.o bin_module.o -o bin.exe
(or possibly)
gfortran *.o -o bin.exe

If this does not work, then there is a problem with the IDE being used ?

I tried what you say, but it still not work. :smiling_face_with_tear:
I guess there is something wrong with my launch.json and task.json, because I found them myself from the web, after all, I’m not very familiar with this area.

I use JohnCampbell’s suggestion.

It has bin_module.o and bin.o.

The correct sequence of commands to perform is:

gfortran -c bin_module.f90
gfortran -c bin.f90
gfortran bin_module.o bin.o -o bin.exe

All .o files must be provided in the last command (link).