Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

#ifndef statement in Fortran

  1. Oct 17, 2011 #1
    Hi all

    I'm using Fortran code written by someone else, and a lot of the files have #ifndef statements in them. For example:

    Code (Text):

    #ifndef global
          REAL array(2500)
    I've tried searching online, but can't find anything useful that actually explains what this bit of code does. Can anyone on here offer any help? I'm assuming it only defines the array if some condition is met, but what condition?

    Many thanks.
  2. jcsd
  3. Oct 17, 2011 #2
    kinda a shot in the dark,
    but maybe ifndef = if not defined?
    like python
  4. Oct 17, 2011 #3

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The #ifdef and `#endif are C preprocessor statements. Your Fortran code apparently is being passed through the C preprocessor before being compiled as Fortran. Wikipedia article on the C preprocessor: http://en.wikipedia.org/wiki/C_preprocessor.
  5. Oct 17, 2011 #4


    User Avatar
    Science Advisor
    Homework Helper

    The condition is that a preprocessor variable called "global" is not defined (and it's very unlikely that is defined by default).

    It could be defined earlier in the source code with a statement like
    #define global
    or more likely it would be passed to the compiler from the makefile with an option like
    either in the makefile itself, or on the command line when you run make.

    As DH said, see the documentation for the C preprocessor, and also for your version of make.
  6. Oct 17, 2011 #5
    Thanks for the help everyone. I've just checked the makefile and it does indeed have a compiler flag with '-Dglobal'. I'd never noticed that before!

    I'll go and check out all this preprocessor stuff in more detail. I don't know what I'd do without the helpful folks here at PF! :smile:
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook