Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether library is included C++

Tags:

c++

I have a function that requires different libraries depending on the values inserted into it, and to prevent different error messages in the log, i want to check if the library needed is included and if not to print an error message more specific than the standard ones.

Is there anyway of checking whether a library is included or not? Something like:

   if(#include <iostream>)
     {
        //do this
        }

EDIT:

Some more details.... i need to check for the libraries in order to know if i can use certain variable types into my code for example to know if i can use type string or if i should use type char[] instead (if the cstring library is used)

like image 970
SpiderLinked Avatar asked Dec 16 '22 03:12

SpiderLinked


1 Answers

you can check if a specific header or macro definition for a given library is defined.

For example:

#ifdef BOOST_BUILD
  #include "somebooststuff.h"
  using namespace boost;
#else
  #include "somestdstuff.h"
  using namespace std;
#endif

where BOOST_BUILD is boost version defined in makefile.

Now, if you want to check if for example header for std::string is included, you can open this header and determine what definitions it contains. For example on my machine header for

#include <string>

contains:

//
// ISO C++ 14882: 21  Strings library
//

#ifndef _GLIBCXX_STRING
#define _GLIBCXX_STRING 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <bits/stringfwd.h>
#include <bits/char_traits.h>  // NB: In turn includes stl_algobase.h
#include <bits/allocator.h>
//...

so I can check if std::string has been defined using:

#ifdef _GLIBCXX_STRING
  //do this
#else
  // do this
#endif
like image 51
4pie0 Avatar answered Dec 25 '22 14:12

4pie0