Fatal Error: Can't open module file 'first_module.mod' for reading at (1): No such file or directory

From the below files, how do I create the first_module.mod file requested by the compiler?

File first_module.f:

MODULE FIRST_MODULE
   implicit none
   
   contains
   LOGICAL FUNCTION RUZE(CARD1,CARD2,LENGTH)
   IMPLICIT NONE
   INTEGER LENGTH
   CHARACTER CARD1*(*)
   CHARACTER, dimension (LENGTH:LENGTH) :: CARD2
  
   RUZE = .FALSE.
  
   IF (CARD1(1:1).EQ.'$')THEN
     IF (CARD2(LENGTH) == ' ')THEN
       WRITE (*,*) 'STEP2 SUCCESSFUL' 
       GOTO 10
     ELSE
       WRITE (*,*) 'STEP2 NOT SUCCESSFUL'  
       GOTO 20
     ENDIF 
   ELSE
      WRITE (*,*) 'DID NOT PERFORM STEP2'  
   ENDIF

10 RUZE = .TRUE.
20 RETURN 
   END
  
  SUBROUTINE MAKE_MOVE(STD)
    IMPLICIT NONE
    CHARACTER(*) :: STD(:) 
    CHARACTER REPRESENT*10
    
    IF(RUZE(REPRESENT(1:7),STD(1:7),7))THEN
     WRITE (*,*) 'STEP1 SUCCESSFUL'
    ELSE
      WRITE (*,*) 'STEP1 NOT SUCCESSFUL'   
    ENDIF
        
    RETURN
  END
END MODULE FIRST_MODULE

File test.f:

Program PLAYING
  USE FIRST_MODULE
  IMPLICIT NONE
  INTEGER MOVES
  PARAMETER (MOVES=100)
  CHARACTER*15 BIGMOVE(MOVES)
  CALL MAKE_MOVE(BIGMOVE)
End Program PLAYING
gfortran -u -g -c test.f
test.f:2:9:
     USE FIRST_MODULE
         1
Fatal Error: Can't open module file 'first_module.mod' for reading at (1): No such file or directory

You first compile the source file containing the module first_module. This will produce the .mod file and then the compiler can compile the second file. This type of dependency is quite common.

Try:

gfortran -c first_module.f
gfortran -o mytest test.f first_module.o

(The reason for giving the program a different name than “test” is that under Linux shells “test” may be a builtin command. The default name “a.out” is not very informative in my opinion :slight_smile: )

You can also find useful information, with examples, about the procedures for building programs at fortran-lang.org, Learn section.

first_module.mod doesn’t exist because the compiler just compiled test.f. You don’t mention fist_module.f in your compiling command thus it wasn’t compiled at all. The simplest and fastest solution is what @Arjen recommended. I personally prefer a makefile which makes things clear and it is more or less mandatory for larger projects as well. It is a good idea to get used to them. In your case, consider the following makefile (a little more generic and expandable):

SRC_DIR = .
OBJ_DIR = .
EXE = ../test

FC = gfortran
FFLAGS = -std=f90 -O2 -Wall -Wextra

FINCLUDES = 

LDFLAGS = 

LDLIBS = 

OBJS = \
	$(OBJ_DIR)/first_module.o\
	$(OBJ_DIR)/test.o.o

all: $(OBJS)
	$(FC) $(FFLAGS) $(OBJS) $(LDFLAGS) $(LDLIBS) -o $(EXE)
# ------------------------------------------------------------------------------
$(OBJ_DIR)/first_module.o: $(SRC_DIR)/first_module.f
	$(FC) $(FFLAGS) $(FINCLUDES) -c $(SRC_DIR)/first_module.f -o $@
$(OBJ_DIR)/test.o: $(OBJ_DIR)/first_module.o $(SRC_DIR)/test.f
	$(FC) $(FFLAGS) $(FINCLUDES) -c $(SRC_DIR)/test.f -o $@
# ------------------------------------------------------------------------------

The most important part is the last one where it clearly states that to compile test.f you need first_module.o, meaning you tell the compiler it has to compile first_module.f before attempting to compile test.f. This not only avoids errors like the one you got, but also gives the compiler enough information so that, if you just modify test.f later on, it will only compile that file and keep using the object code for first_module created earlier. Furthermore, it makes parallel compilation in larger projects possible.

Automated tools for Makefiles also exist, and you can use them instead, or you can use cmake, or other tools designed for that purpose, or even an IDE. If you want to get serious about programming, you will need one of those, so it is a good idea to get used to them even for small projects such as the one you posted.