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

**URL:** <https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553>\
**Category:** Uncategorized\
**Created:** [May 25, 2022, 2:03pm UTC](https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553 "2022-05-25T14:03:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giraffe](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@giraffe](https://fortran-lang.discourse.group/u/giraffe)\
**Post date:** [May 25, 2022, 2:03pm UTC](https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553/1 "2022-05-25T14:03:13Z")

</div>

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

File first\_module.f:

```auto
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:

```auto
Program PLAYING
  USE FIRST_MODULE
  IMPLICIT NONE
  INTEGER MOVES
  PARAMETER (MOVES=100)
  CHARACTER*15 BIGMOVE(MOVES)
  CALL MAKE_MOVE(BIGMOVE)
End Program PLAYING

```

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [May 25, 2022, 2:25pm UTC](https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553/2 "2022-05-25T14:25:25Z")

</div>

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:

```auto
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 🙂 )

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [May 25, 2022, 2:48pm UTC](https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553/3 "2022-05-25T14:48:14Z")

</div>

> [@giraffe](#):
>
> “[…]how do I create the first\_module.mod file requested by the compiler?[…]”

You can also find useful information, with examples, about the procedures for building programs at [fortran-lang.org](http://fortran-lang.org), [Learn section](https://fortran-lang.org/learn/building_programs/linking_pieces).

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [May 25, 2022, 2:55pm UTC](https://fortran-lang.discourse.group/t/fatal-error-cant-open-module-file-first-module-mod-for-reading-at-1-no-such-file-or-directory/3553/4 "2022-05-25T14:55:25Z")

</div>

`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):

```auto
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.
