Regarding swift conditional compilation and preprocessor directives

Preprocessor directive "Instructions that function on source code, which is a set of characters"

Computer pre-processing, pre-processing, that is, pre-processing to meet the conditions of the core program in computer processing)
In the compiler, the source code written by humans is translated into machine language, but before that, it is the role of the preprocessor to process the source code according to certain rules.

wrap up
 #ifndef, #define, #The line labeled endif is a popular way to prevent double header files from being read in C.
If you don't do this, the header file will be read many times, and variables and constants will be defined many times, resulting in a compilation error.
#ifdef
function:Determines if an identifier is defined.
(Identifier name)If is defined, process it. Processing is a command to the preprocessor.
The identifier is#Define a value with define, but if undefined, "0", that is, nothing is returned.
Format: #ifdef (Identifier name)
processing
#define
function:String replacement.
identifier(Character string 1)Value(String 2)Is defined as. This time,#In define, character strings are replaced with each other. Because the preprocessor directive is just a character string described in the source file.=>It's just dealing with source code.

#define __MAX 100
If so, at the compilation stage,"__MAX"Is an integer type(Int)Has been replaced by 100. In the source code"__MAX"But"100"Has been replaced with the string.

A preprocessor instruction can be defined as "an instruction that functions on the source code, which is a set of characters".
#endif
function: #ifdef and#Specify at the end of the processing block by ifndef or the like.
#ifdef and#If the result is "true" for both ifndef(processing)Executes one line.

Reference example

Controlling whether to compile or not according to the following conditions is called "conditional compilation".

By letting the processing inside the abstract identifier be performed each time, it is prevented from being defined every time.

In a program, Sample.Consider the first time h was included.
1 First INCLUDED_Sample_h_Is not defined. Therefore#ifdef is ignored, const float PI= 3.14159;Will be compiled.
And#INCLUDED by define_Sample_h_Is defined.

2 Sample after the second time.When h is included, it is already INCLUDED_Sample_h_Is defined. For this reason,#by ifdef#define and const float PI= 3.14159 is ignored and will not compile.
In other words, Sample in one program.No matter how many times h is included, the definition is once.
And#The processing inside the ifdef identifier is performed each time it is read.
Sample.h file

  #ifndef INCLUDED_Sample_h_
  
  #define INCLUDED_Sample_h_
  

  const float PI = 3.14159;
 
  #endif

Reference URL https://www.grapecity.com/tools/support/powernews/column/clang/014/page02.html

http://ejje.weblio.jp/content/preprocessor

http://e-words.jp/w/%E3%83%97%E3%83%AA%E3%83%97%E3%83%AD%E3%82%BB%E3%83%83%E3%82%B5.html

http://www.kuma-de.com/blog/2013-04-24/5224

Recommended Posts

Regarding swift conditional compilation and preprocessor directives
[Swift] Constants and variables