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

  • Thread starter Thread starter jf22901
  • Start date Start date
  • Tags Tags
    Fortran
AI Thread 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:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
20
Views
3K
Replies
14
Views
5K
Replies
5
Views
5K
Replies
4
Views
2K
Replies
16
Views
2K
Replies
8
Views
2K
Replies
4
Views
2K
Back
Top