C++11 standard libraries in libstdc++ on Mac

2019-06-06 17:27发布

问题:

In a Xcode project which I currently work on, I am using C++11 with Apple LLVM 4.2 (clang) compiler, and using libstdc++ as my standard library, because I am using a library (NTL) which was not compiled with libc++ and so I must use libstdc++.

When writing the following code:

#include <regex>
int main()
{
     return 0;
}

It doesn't compile, by saying:

'regex' file not found

And so I can't use any C++11 library with my libstdc++ (Tried <mutex>, <thread> aswell).

Another I tried to take is to recompile NTL with cc and libc++, but that doesn't seem much to work. The following is some of the errors that were produced:

../include/NTL/config.h:57:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:88:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:95:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:112:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:120:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:143:7: error: expected value in expression
#elif 
      ^
../include/NTL/config.h:168:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:189:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:208:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:226:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:248:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:260:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:273:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:289:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:309:5: error: expected value in expression
#if 
    ^
../include/NTL/config.h:326:7: error: expected value in expression
#elif 
      ^
In file included from FFT.c:3:
In file included from ../include/NTL/FFT.h:6:
In file included from ../include/NTL/ZZ.h:19:
../include/NTL/tools.h:21:10: fatal error: 'iostream.h' file not found`

It seems that the configuration header was somehow "damaged" and that libc++ doesn't have <iostream.h> and old c++ headers. And so, recompiling NTL was a bit of trouble for me.

And so, how can I fix it? How can I still use libstdc++ on my project and have C++11 Libraries? If that helps, I have g++-4.8 installed with brew. Is there a way to map libstdc++ that clang uses to a new one?

回答1:

You'll need to tell your copy of clang how to find the libstdc++ headers that you want to use. Clearly it's not finding them. I think that the flags you want are "-nostdinc -I /path/to/better/headers".

The first part tells clang not to use it's standard headers, and the second one tells it to look for headers "over there".