Search code examples
fortranintel-fortranpreprocessor-directive

How to set and access a custom pre-processor variable?


I'm working on making a Fortran application run on both Windows and Linux. Up till now, it's been Windows only. For now, the Linux version is entirely terminal-based while the Windows version has a GUI. We will have a GUI for the Linux version eventually.

To handle differences between Windows and Linux, I've been using pre-processor directives like

!DEC$ IF DEFINED(_WIN32)
...
!DEC$ ELSEIF DEFINED(__linux)
...
!DEC$ ENDIF

This works well for what I need right now, since I know the Windows version always has a GUI and the Linux version never has a GUI. However, I'd like to split that out so that eventually I can have both GUI and GUI-less builds on both OSs.

Is there a way I can set up my own pre-processor variable so that I can have statements like

!DEC$ IF DEFINED(GUI)
...
!DEC$ ELSE
...
!DEC$ ENDIF

So basically, how would I tell the pre-processor I want a variable called GUI and how would I set its value? I know I'm probably not using the proper terminology as I'm not terribly familiar with this kind of programming. Feel free to correct my syntax.


Solution

  • This is typically done with a command line option for the build variant. Most compilers use -D (or /D) for this, for example, -DGUI, which defines a symbol GUI you can test for (the value will be 1 unless you give a different one.) Check your compiler's documentation to see what the option is.