0

I need to include a header in condition if it is present and if not i need to make the necessary steps for my code to work

I need to do something like...

// check if the header is present(may be .h files)
#include"required_header"
{
block-1
}

//if the header is not present 
{
block-2
}

If I use like ....

#ifdef required_header
#include<required_header>
Block-1
#else
Block-2

The above case is redirected to Block-2 always because i have included the header file only inside the condition of ifdef and its never included

Hoping for a method to do it.

Thanks in Advance :}

P.S: I have defined the header but assuming i have not done the work of the required header but i want someone else to write that file and I am not sure if he has written it.

Precisely speaking I want the code replacement way in the place of comments in the first given code.

NSK
  • 180
  • 2
  • 12
  • Did you ever `#define required_header`? Because otherwise `#ifdef required_header` will always be false, like you're seeing. – scohe001 Nov 26 '19 at 21:53
  • yeah i have defined the required_header.The precise question boils down to if there is a file with that name existing or not – NSK Nov 26 '19 at 21:56
  • In general, no. Some compilers (gcc and clang, from memory - there may be others) support a `__has_include("header_name")` but that is non-standard. A common technique is to have header files relied on by your project automatically generated, and the program that generates those headers takes care of finding which headers on the current system/installation actually exist. This is (part of) the reason for configure scripts that accompany a lot of open-source products, where the configuration is a step performed before building. – Peter Nov 26 '19 at 22:03
  • Do you have CMake or QMake or something like that? Each solves your problem in it's own way, generally those build systems allow you to define some symbols in a neater way than `#define`s – MasterAler Nov 26 '19 at 22:04
  • Actually There is a difference. In the above example the work is to include a file conditionally but here the work is to check for the existing file before including it – NSK Nov 26 '19 at 22:04
  • 2
    @Peter It's in the standard now, since C++17. – HolyBlackCat Nov 26 '19 at 22:05

1 Answers1

0

Your strategy won't work. If the pre-processor can't find the header file, it will terminate processing the remainder of the file.

Your best bet is to always have the file but have different content in different places.

#include "required_header"

#if defined(SYMBOL_1)
  // Use code that is appropriate when SYMBOL_1 is defined.
#elif defined(SYMBOL_2)
  // Use code that is appropriate when SYMBOL_2 is defined.
#else
#error "Expected either SYMBOL_1 or SYMBOL_2 to be #define'd"
#endif

Another option will be to use something like:

#ifdef HAS_REQUIRE_HEADER
#include "required_header"
  // Use code that is appropriate when required_header is there
#else
  // Use code that is appropriate when required_header is not there
#endif

And make sure that HAS_REQUIRE_HEADER is defined in the compiler's command line options when "required_header" is available -- either manually or through another tool, such as cmake or qmake.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Wouldn't it be better to have a wrapper header file `check_for_required_header.h` that `#define SYMBOL_1` and `#include "required_header"` or just ` `#define SYMBOL_2`? – Paul Evans Nov 26 '19 at 22:02
  • @PaulEvans, if I understand your comment correctly, that will push the problem one more level down but the same problem will continue to be there. The pre-processor will still fail if it can't find `"required_header"`. – R Sahu Nov 26 '19 at 22:05
  • No, `check_for_required_header.h` won't `#include "required_header"` it'll just `#define SYMBOL_2` if there no `required_header`. – Paul Evans Nov 26 '19 at 22:07
  • @PaulEvans, how does the pro-processor decide whether there is no `required_header`? – R Sahu Nov 26 '19 at 22:08
  • Without something like `cmake` it'll have to be done by hand, – Paul Evans Nov 26 '19 at 22:09
  • @PaulEvans, I see. if a tool like `cmake` is used, that would be definitely easier. – R Sahu Nov 26 '19 at 22:10