0

I'm importing the following module (from an static library) in a file named initprogram.f90 this way:

use device_info

However I only want to include this library when this option in the Makefile is selected:

ifeq ($(strip $(WITH_GPU)),1) 

(When WITH_GPU is equal to 1). If I'm not using a GPU, device_info.mod should not be available, since I don't need it. How can I do that?

Basically I want to get rid of this error:

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

When compiling without the library where device_info.mod is defined.

Caterina
  • 775
  • 9
  • 26

1 Answers1

1

You probably need:

  1. A preprocessor to hide or not the use device_info declaration in your Fortran source file, depending on an option you pass to it. Do you have a preprocessor in your Fortran compilation chain? If yes do you know how to pass it options from the command line and how to use them in your source file to hide or not parts of your code?

  2. Pass the right option to the compiler chain from your Makefile.

Let's assume you do have a preprocessor and it has an #ifdef - #endif macro. Let's also assume your compiler chain takes options -D MACRO=VALUE from the command line. And let's assume the syntax of your compiler command looks like:

<compiler-name> <options> <source-file> -o <binary-output-file>

Just edit your source file and add:

#ifdef WITH_GPU
use device_info
#endif

And then, edit your Makefile:

COMPILER           := <whatever-compiler-you-use>
COMPILER_FLAGS     := <whatever-compiler-options-you-need-by-default>
OTHER_DEPENDENCIES := <whatever-default-dependencies>

ifeq ($(strip $(WITH_GPU)),1) 
COMPILER_FLAGS     += -D WITH_GPU=1
OTHER_DEPENDENCIES += device_info.mod
endif

initprogram.exe: initprogram.f90 $(OTHER_DEPENDENCIES)
    $(COMPILER) $(COMPILER_FLAGS) $< -o $@

(the $< and $@ make automatic variables expand respectively to the first prerequisite (initprogram.f90) and the target (initprogram.exe) of the rule).

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51