How to check if title has already been included before

I am doing a project and I am stunned by the problem.

I have 3 library.h, which includes another special library, .h definitions, but in my main module I want to include all libraries only once, I mean, I want to check if there is a definitions.h library already included and include it or not depending on the result.

Sort of

 If !(#include"definitions.h") (#include"definitions.h") 
+4
source share
3 answers

You are looking to include guards.

Example

 #ifndef DEFINITIONS_H #define DEFINITIONS_H ... ... #endif 
+9
source
 #ifndef DEFINITIONS_H #define DEFINITIONS_H //lots of code // // // // #endif 

There is also a non-standard #pragma once , see. Is #pragma after secure enable protection?

+6
source

If your header syntax is correct, this should not be a problem. In fact, it is for this reason that you write

 #ifndef _DEFINITIONS_H #define _DEFINITIONS_H [header content] #endif 

So, if your title complies with C conventions, you should be fine.

+2
source

Source: https://habr.com/ru/post/1485185/


All Articles