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

  • Thread starter Thread starter jf22901
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
The discussion centers on the use of the #ifndef preprocessor directive in Fortran code, which is being processed through the C preprocessor. The #ifndef statement checks if a preprocessor variable, in this case "global," is not defined. If "global" is not defined, the code within the #ifndef block, such as defining an array, will be executed. It is clarified that "global" can be defined earlier in the code or passed to the compiler via a makefile with a flag like -Dglobal. The original poster discovers that their makefile includes this flag, confirming that the "global" variable is indeed defined, which affects the conditional compilation. The conversation emphasizes the importance of understanding the C preprocessor and reviewing makefile documentation for better clarity on this topic.
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:
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

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
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K