Andrzej
Andrzej

Reputation: 5127

How to detect the libstdc++ version in Clang?

I would like to write a "portable" C++ library in Clang. "Portable" means that I detect (in C preprocessor) what C++ features are available in the compilation environment and use these features or provide my workarounds. This is similar to what Boost libraries are doing.

However, the presence of some features depends not on the language, but on the Standard Library implementation. In particular I am interested in:

I find this problematic because Clang by default does not use its own Standard Library implementation: it uses libstdc++. While Clang has predefined preprocessor macros __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, they are hardcoded to values 4, 2, 1 respectively, and they tell me little about the available libstdc++ features.

How can I check in Clang preprocessor what version of libstdc++ it is using?

Upvotes: 9

Views: 10265

Answers (2)

user1766438
user1766438

Reputation: 582

This is what I think would help. It prints the value of the _LIBCPP_VERSION macro:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[])
{
    cout<<"Value = "<<_LIBCPP_VERSION<<endl;
    return 0;
}

Compile it again the version of clang you want the info for.

Upvotes: 2

rubenvb
rubenvb

Reputation: 76721

Clang does come with its own standard library implementation, it's called libc++. You can use it by adding -stdlib=libc++ to your compile command.

That being said, there are various ways to check Clang/libstdc++ C++ support:

  1. Clang has the __has_feature macro (and friends) that can be used to detect language features and language extenstions.
  2. Libstdc++ has its own version macros, see the documentation. You'll need to include a libstdc++ header to get these defined though.
  3. GCC has its version macros which you already discovered, but those would need to be manually compared to the documentation.

And also, this took me 2 minutes of googling.

Upvotes: 5

Related Questions