# With regard to

Asked 2 years ago, Updated 2 years ago, 135 views

You are now trying to deploy the C++ macro on your own.
So I found "#define" and it looked like this.

#ifndef OuHolder_h
#define OuHolder_h

"#define" like this seems to indicate ""OuHolder_h is defined.""" I don't know how to deploy this.
What should I do?

c++ preprocessor

2022-09-30 20:44

4 Answers

I don't know what you're trying to do, but here's what's commonly called an include guard.Although not mentioned in the description, there should also be endif at the end of the file.

#ifndef OuHolder_h
#define OuHolder_h
// omission
#endif

First of all, please note that ifdef and ifndef are different...

Runs from deifndef 『 to 』endif 実行 only if OOuHolder_h 』 is not defined.
Therefore, if OuHolder_h is defined in the second line dedefine 』, it will not run from iifndef 』 to ifendif 』.

This is to prevent reading the same header file over and over again if you are reading the same header file from multiple files.

First of all, it is recommended that you understand the simple things first and gradually try what you want to do.


2022-09-30 20:44

In other words, #ifndef means that the definition has not yet been defined.

Therefore, if not defined, define.


2022-09-30 20:44

If the purpose is not to deploy on your own, but just to see the source after the pre-process is complete, the compiler may have that option, so you can use it.
For example, VC++ (command name: cl), /E, or g++ or clang++ can output pre-process results.


2022-09-30 20:44

If you write the basics,

#ifndef OuHolder_h// If OuHolder_h is defined so far and the source code up to #endif is valid.
Define the macro #define OuHolder_h// OuHolder_h.If OuHolder_h is written after this line, it means this macro.

#endif

It turns out thatSo

#include<iostream>

#define Macro1
# ifndef Macro1
    // Macro1 is #defined in front of #ifndef, so it is not pre-processed or compiled here.
    # define SUM(x,y)(x+y)
    void print_hello() {std::cout<<"Hello, World!"<<std::endl;}
#endif

int main() {
    intx = SUM(1,2); // SUM is not defined.a compilation error
    print_hello(); // print_hello is not defined in the same way.an error
}

Then, as you can see in the comments, there will be a compilation error.

#include<iostream>

# ifndef Macro 2
    // Since #defineMacro2 is not written in front of #ifndef, both pre-process and compilation are performed.
    # define SUM(x,y)(x+y)
    void print_hello() {std::cout<<"Hello, World!"<<std::endl;}
#endif

int main() {
    int x = SUM(1,2); // pass.
    print_hello();// This also passes.Hello, World! appears on the console.
}

This will pass.

By the way, from the definition of #define OuHolder_h, all the parts in which the identifier OuHolder_h is described in the source code are replaced by .Actually, I don't think there's any blank space.


2022-09-30 20:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.