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

  • Fortran
  • Thread starter jf22901
  • Start date
  • Tags
    Fortran
In summary, the code might only define the array if a preprocessor variable called "global" is not defined.
  • #1
jf22901
55
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
  • #2
kinda a shot in the dark,
but maybe ifndef = if not defined?
like python
 
  • #3
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.
 
  • #4
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.
 
  • #5
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:
 

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

1. What is the purpose of the #ifndef statement in Fortran?

The #ifndef statement in Fortran is used for conditional compilation. It allows a section of code to be compiled only if a certain condition is met. This is useful for creating different versions of a program for different operating systems or hardware configurations.

2. How do you use the #ifndef statement in Fortran?

The #ifndef statement is used in combination with the #define statement. First, you define a macro using #define, then you use #ifndef to check if that macro has been defined. If it has not been defined, the code following the #ifndef statement will be compiled.

3. Can you provide an example of using the #ifndef statement in Fortran?

Sure, here is a simple example:

#define DEBUGprogram example#ifndef DEBUG    print *, "Debugging is turned off."#endif    print *, "This line will always be compiled."end program example

In this example, the code within the #ifndef statement will only be compiled if the DEBUG macro has not been defined. If it has been defined, that code will be ignored.

4. What happens if the condition in the #ifndef statement is not met?

If the condition in the #ifndef statement is not met, the code following the statement will be ignored and not compiled.

5. Are there any other similar statements in Fortran?

Yes, there are two other similar statements in Fortran: #ifdef and #endif. #ifdef is used to check if a macro has been defined, while #endif marks the end of a conditional compilation block.

Similar threads

  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
Replies
4
Views
691
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
5
Views
858
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
481
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top