Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Detect missing include files with c++ preprocessor?

Credits go to torhu for helping out!
This thread is locked; no one can reply to it. rss feed Print
Detect missing include files with c++ preprocessor?
Mark Oates
Member #1,146
March 2001
avatar

Is is possible to use a header guard or something similar to detect if a header file is present or not?

#ifndef FILESYSTEM_INCLUDE_FILE_WHATEVER
  // add custom implementation
#else
  #include <filesystem>
#endif

torhu
Member #2,727
September 2002
avatar

No, you have to use make or some other external tool to do it.

Mark Oates
Member #1,146
March 2001
avatar

Okie dokie. Thanks.

torhu
Member #2,727
September 2002
avatar

But you can still define macros that you can then check in your headers, like `gcc -DHAVE_THE_FILE=1 game.cpp` etc. Just in case you didn't know ;)

Polybios
Member #12,293
October 2010

You can use __has_include() since C++ 17.

torhu
Member #2,727
September 2002
avatar

Polybios said:

You can use __has_include() since C++ 17.

Cool. I've been following the development of C++, but never noticed that feature.

Mark Oates
Member #1,146
March 2001
avatar

Modern C++ has a lot of cool stuff. <filesystem> I really like. It's got some cool little conveniences like temp_directory_path, last_write_time.

I'm not a fan of the more permissive complex conventions. They seem to me to lead down the bad path of incompressible complexity.

As for __has_include(), that's awesome. I probably won't use that though. I should really figure out why <filesystem> isn't appearing on the other system rather than trying to patch around it.

torhu
Member #2,727
September 2002
avatar

Most likely they have older GCC installations. You could install Boost and use boost::filesystem (or whatever it's called), I think that's pretty much a drop-in replacement.

Polybios
Member #12,293
October 2010

Hmm... Just make C++ 17 (with its standard headers) a requirement? They will come over time anyway.

Go to: