Have C++ standard library ifdef or ifndef preprocessor instructions?

ifdef c++17
ifdef include
cppreference ifdef
ifdef namespace
ifdef c++
ifdef
ifdef else
ifdef multiple conditions

I'm building my own terminal app project in C++ and I'm asking myself if standard library has ifdef or ifndef preprocessors instructions. I want to know that because I need to create different header files which need some standard library headers such as "string" and some others, i don't want to include the same library 3 or more times because it makes the program heavier. For example i wrote on my header files something like this to prevent the .h file to be included more than once:

#ifndef myheader_h
#define myheader_h
    // my file code here
#endif

I tried compiling but the compiler say me nothing about errors or warnings. I also tried to read the standard-library source code (https://en.cppreference.com/w/cpp/header) and I haven't found any preprocessor rule like ifdef or ifndef. Should i include standard library headers like this?

#ifndef string_h
#define string_h
    #include <string>
#endif

I hope my question isn't already asked because I haven't found it while searching it.

Updates

To some who said "you're not in the position where you need to worry about" and who said "it costs very little if it has proper include guards", I meant: program's heaviness is important, I want to make it slighter so I don't want to entirely include the same file multiple times. Have std lib files proper include guards? (my header files have them, didn't know std lib files)


Those preprocessor directives you're talking about are called "header guards", and the standard library headers definitely have them (or some other mechanism that does the same thing) like all other proper header files. Including them multiple times shouldn't cause any problems, and you only need to worry about these when you're writing your own header files.

The "source code" that you're reading is just the documentation which says how the header files should work, but it doesn't provide the actual code. To see the code, you can look in the header files provided by your compiler. For example, the <iostream> header in Visual Studio has both #pragma once and header guards:

#pragma once
#ifndef _IOSTREAM_
#define _IOSTREAM_
//...
#endif /* _IOSTREAM_ */

The headers provided by the GCC compiler also has header guards:

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1
//...
#endif /* _GLIBCXX_IOSTREAM */

[PDF] The C Preprocessor, You have freedom to copy and modify this GNU Manual, like GNU software. Copies the GNU C preprocessor does not do a few things required by the standard. operating system kernel or some other program that does not use the standard C library '#ifndef'. 4.2.1 Ifdef. The simplest sort of conditional is. #ifdef MACRO. The #if directive, with the #elif, #else, and #endif directives, controls compilation of portions of a source file. If the expression you write (after the #if) has a nonzero value, the line group immediately following the #if directive is kept in the translation unit. Each #if directive in a source file must be matched by a closing #endif


There is no requirement for the standard header files to #define any specific pre-processor symbols to make sure they can be #included multiple times.

Having said that, any sane implementation would make sure that they can be #included multiple times without adversely affecting application code.

Turns out, that is a requirement by the standard for most headers (Thanks, @Rakete1111).

From the C++ standard

A translation unit may include library headers in any order ([lex]). Each may be included more than once, with no effect different from being included exactly once, except that the effect of including either <cassert> or <assert.h> depends each time on the lexically current definition of NDEBUG.

Not only that, they are very likely to be using the #pragma once directive. Hence, even if you use #include multiple times for the same header, they are going to be read only once.

In summary, don't worry about standard header files. If your header files are implemented correctly, your application would be just fine.

C Programming/Preprocessor directives and macros, You have freedom to copy and modify this GNU Manual, like GNU software. the GNU C preprocessor does not do a few things required by the standard. program that does not use the standard C library facilities, or the standard C library itself. C preprocessor begins with a conditional directive: '#if', '#ifdef' or. '​#ifndef'. The standard solution is to wrap the entire library in the following construct: #ifndef _EXAMPLE_LIBRARY_H #define _EXAMPLE_LIBRARY_H //This is an example library int a = 0; //End of example library #endif. Now, when the library is included for the first time, the preprocessor checks whether there is something defined with the name “ _EXAMPLE


I'm asking myself [sic] if standard library has ifdef or ifndef preprocessors instructions

The standard doesn't specify whether there are ifdef-style header guards, although it does require that multiple inclusion is protected in some manner. I took a look at a random header of stdlibc++ standard library implementation. It does have header guards.

i don't want to include the same library 3 or more times because it makes the program heavier

Including a header file multiple times does not make a program "heavier".

Should i include standard library headers like this?

#ifndef string_h
#define string_h
    #include <string>
#endif

That is not necessary, or particularly useful.

Preprocessor, Preprocessors are a way of making text processing with your C program before find out specific instructions called Preprocessor directives that it can understand. options to C compilers aren't completely standard, many follow similar rules. 1.7 #undef; 1.8 #if,#else,#elif,#endif (conditionals); 1.9 #ifdef,#ifndef; 1.10 #line. Conditional inclusions (#ifdef, #ifndef, #if, #endif, #else and #elif) These directives allow to include or discard part of the code of a program if a certain condition is met. #ifdef allows a section of a program to be compiled only if the macro that is specified as the parameter has been defined, no matter which its value is.


Using C and C++ preprocessor directives that includes #include , Standard Library Headers Regular expressions library (C++11) character; preprocessing instruction (one of define , undef , include , if , ifdef , ifndef , else , elif The preprocessor has the source file translation capabilities:. The standard does not define behavior for other directives: they might be ignored, have some useful meaning, or make the program ill-formed. Even if otherwise ignored, they are removed from the source code when the preprocessor is done. A common non-standard extension is the directive #warning which emits a user-defined message during compilation.


C Preprocessor, Understanding and learning on using the C and C++ pre-processor directives such and use the conditional compilation – #if,#endif,#ifdef,#else, #ifndef and #​undef. The#include directive is normally used to include standard library such as These files should have common declaration, such as functions, classes etc,​  C preprocessors vary in some details. This manual discusses the GNU C preprocessor, which provides a small superset of the features of ISO Standard C. In its default mode, the GNU C preprocessor does not do a few things required by the standard. These are features which are rarely, if ever, used, and may cause surprising changes to the meaning of


Preprocessor directives - C++ Tutorials, The C preprocessor takes a C file and any included headers and outputs an edited C takes a preprocessed C file and outputs an assembly file (CPU instructions). This will be fore example when the standard library implementations (ie the #ifdef FOO // This code is only compiled if FOO is defined #endif #ifndef FOO  C Example 1 #include < stdio. h > /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ int main ( void ) { printf ( "the answer to life the universe and everything is %d " , 42 ) ; }