Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang can't compile basic header files (like <iostream>) in macOS

My macOS version is 10.14

Xcode version is 10.2

To write the plugin for clang. I just install the llvm and clang from Github with following commands.

git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" ../llvm

Then with clang --version, it shows:

clang version 10.0.0 (https://github.com/llvm/llvm-project.git 73f702ff192475b27039325a7428ce037771a5de)
Target: x86_64-apple-darwin18.6.0
Thread model: posix
InstalledDir: /Users/kim/GitHub/llvm-project/build/bin

Now, I just try to compile very easy Hello World program:

#include <iostream>

using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}

with command clang++ test.cpp -o test

But unfortunately, it can't compile with error:

In file included from test.cpp:1:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iostream:37:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/ios:214:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iosfwd:95:
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:136:77: error: use of undeclared
      identifier 'wcschr'
wchar_t* __libcpp_wcschr(const wchar_t* __s, wchar_t __c) {return (wchar_t*)wcschr(__s, __c);}
                                                                            ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:143:87: error: use of undeclared
      identifier 'wcspbrk'
wchar_t* __libcpp_wcspbrk(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcspbrk(__s1, __s2);}
                                                                                      ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:150:78: error: use of undeclared
      identifier 'wcsrchr'; did you mean 'wcschr'?
wchar_t* __libcpp_wcsrchr(const wchar_t* __s, wchar_t __c) {return (wchar_t*)wcsrchr(__s, __c);}
                                                                             ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:138:16: note: 'wcschr' declared here
const wchar_t* wcschr(const wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);}
               ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:157:86: error: use of undeclared
      identifier 'wcsstr'; did you mean 'wcschr'?
wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);}
                                                                                     ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:140:16: note: 'wcschr' declared here
      wchar_t* wcschr(      wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);}
               ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:157:86: error: no matching function for
      call to 'wcschr'
wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);}
                                                                                     ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:140:16: note: candidate disabled: <no
      message provided>
      wchar_t* wcschr(      wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);}
               ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:157:93: error: cannot initialize a
      parameter of type 'wchar_t *' with an lvalue of type 'const wchar_t *'
wchar_t* __libcpp_wcsstr(const wchar_t* __s1, const wchar_t* __s2) {return (wchar_t*)wcsstr(__s1, __s2);}
                                                                                            ^~~~
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:140:38: note: passing argument to
      parameter '__s' here
      wchar_t* wcschr(      wchar_t* __s, wchar_t __c) {return __libcpp_wcschr(__s, __c);}
                                     ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:164:60: error: unknown type name
      'size_t'
wchar_t* __libcpp_wmemchr(const wchar_t* __s, wchar_t __c, size_t __n) {return (wchar_t*)wmemchr(__...
                                                           ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:166:57: error: unknown type name
      'size_t'
const wchar_t* wmemchr(const wchar_t* __s, wchar_t __c, size_t __n) {return __libcpp_wmemchr(__s, _...
                                                        ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:168:57: error: unknown type name
      'size_t'
      wchar_t* wmemchr(      wchar_t* __s, wchar_t __c, size_t __n) {return __libcpp_wmemchr(__s, _...
                                                        ^
In file included from test.cpp:1:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iostream:37:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/ios:214:
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iosfwd:189:14: error: use of undeclared
      identifier 'mbstate_t'
typedef fpos<mbstate_t>    streampos;
             ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iosfwd:190:14: error: use of undeclared
      identifier 'mbstate_t'
typedef fpos<mbstate_t>    wstreampos;
             ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iosfwd:195:14: error: use of undeclared
      identifier 'mbstate_t'
typedef fpos<mbstate_t>    u16streampos;
             ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iosfwd:196:14: error: use of undeclared
      identifier 'mbstate_t'
typedef fpos<mbstate_t>    u32streampos;
             ^
In file included from test.cpp:1:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iostream:37:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/ios:215:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/__locale:14:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string:504:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string_view:175:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/__string:56:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/algorithm:641:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/cstring:60:
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:73:64: error: use of undeclared
      identifier 'strchr'
char* __libcpp_strchr(const char* __s, int __c) {return (char*)strchr(__s, __c);}
                                                               ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:80:75: error: use of undeclared
      identifier 'strpbrk'
char* __libcpp_strpbrk(const char* __s1, const char* __s2) {return (char*)strpbrk(__s1, __s2);}
                                                                          ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:87:65: error: use of undeclared
      identifier 'strrchr'; did you mean 'strchr'?
char* __libcpp_strrchr(const char* __s, int __c) {return (char*)strrchr(__s, __c);}
                                                                ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:75:13: note: 'strchr' declared here
const char* strchr(const char* __s, int __c) {return __libcpp_strchr(__s, __c);}
            ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:94:76: error: use of undeclared
      identifier 'memchr'; did you mean 'wmemchr'?
void* __libcpp_memchr(const void* __s, int __c, size_t __n) {return (void*)memchr(__s, __c, __n);}
                                                                           ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/wchar.h:166:16: note: 'wmemchr' declared here
const wchar_t* wmemchr(const wchar_t* __s, wchar_t __c, size_t __n) {return __libcpp_wmemchr(__s, _...
               ^
In file included from test.cpp:1:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/iostream:37:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/ios:215:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/__locale:14:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string:504:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string_view:175:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/__string:56:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/algorithm:641:
In file included from /Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/cstring:60:
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:101:74: error: use of undeclared
      identifier 'strstr'; did you mean 'strchr'?
char* __libcpp_strstr(const char* __s1, const char* __s2) {return (char*)strstr(__s1, __s2);}
                                                                         ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:77:13: note: 'strchr' declared here
      char* strchr(      char* __s, int __c) {return __libcpp_strchr(__s, __c);}
            ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:101:74: error: no matching function for
      call to 'strchr'
char* __libcpp_strstr(const char* __s1, const char* __s2) {return (char*)strstr(__s1, __s2);}
                                                                         ^
/Users/kim/GitHub/llvm-project/build/bin/../include/c++/v1/string.h:77:13: note: candidate disabled: <no
      message provided>
      char* strchr(      char* __s, int __c) {return __libcpp_strchr(__s, __c);}
            ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

I have tried adding -std=c++11, -stdlib=libc++, but there are same errors.

like image 298
kiimmm Avatar asked Aug 14 '19 12:08

kiimmm


1 Answers

I was getting this error while attempting to build and debug C++ code in VSCode.

For me the answer was found here: https://github.com/rstudio/rstudio/issues/7824#issuecomment-694481241

TLDR;

xcode was configured to use the developer tools

▶ xcode-select -p
/Applications/Xcode.app/Contents/Developer

Switching to the command line tools resolved the problem.

sudo xcode-select -s /Library/Developer/CommandLineTools
like image 117
Rowan Baker-French Avatar answered Oct 05 '22 07:10

Rowan Baker-French