What is the purpose of #ifndef statements in Fortran code?

  • Context: Fortran 
  • Thread starter Thread starter jf22901
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around the purpose and functionality of #ifndef statements in Fortran code, particularly in the context of code that is processed by the C preprocessor. Participants explore the implications of these statements and their conditions for defining variables.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant seeks clarification on the purpose of #ifndef statements, suggesting that it defines an array conditionally.
  • Another participant proposes that #ifndef stands for "if not defined," drawing a parallel to Python.
  • A participant explains that #ifdef and #endif are C preprocessor statements, indicating that the Fortran code is processed by the C preprocessor before compilation.
  • It is noted that the condition for defining the array is that a preprocessor variable called "global" is not defined, which is typically not defined by default.
  • Further clarification is provided that "global" could be defined earlier in the source code or passed to the compiler via a makefile option.
  • The original poster expresses gratitude for the assistance and confirms that the makefile contains a compiler flag with '-Dglobal'.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the #ifndef statement and its relation to the C preprocessor, but there is no explicit consensus on all details of its usage or implications.

Contextual Notes

The discussion does not resolve the broader implications of using preprocessor directives in Fortran or the specific scenarios in which they might be applied.

jf22901
Messages
55
Reaction score
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:
#ifndef global
      REAL array(2500)
#endif

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.
 
Technology news on Phys.org
kinda a shot in the dark,
but maybe ifndef = if not defined?
like python
 
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.
 
jf22901 said:
I'm assuming it only defines the array if some condition is met, but what condition?

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
-Dglobal
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.
 
AlephZero said:
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
-Dglobal
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.

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:
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K