Clang无法在macOS中编译基本头文件(例如<iostream>)

时间:2019-08-14 12:22:19

标签: c++ macos header-files llvm-clang libc++

我的macOS版本是10.14

Xcode版本为10.2

为clang编写插件。我只是使用以下命令从Github安装llvm和clang。

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

然后显示clang --version,它显示:

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

现在,我只是尝试编译非常简单的 Hello World 程序:

#include <iostream>

using namespace std;

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

使用命令clang++ test.cpp -o test

但是不幸的是,它无法编译错误:

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.

我尝试添加-std=c++11-stdlib=libc++,但是存在相同的错误。

2 个答案:

答案 0 :(得分:1)

我在 VSCode 中尝试构建和调试 C++ 代码时遇到此错误。

对我来说,答案是在这里找到的: https://github.com/rstudio/rstudio/issues/7824#issuecomment-694481241

TLDR;

xcode 被配置为使用开发者工具

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

切换到命令行工具解决了问题。

sudo xcode-select -s /Library/Developer/CommandLineTools

答案 1 :(得分:0)

它可以通过编译

clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I/.../llvm-project/libcxx/include -L/.../llvm-project/libcxx/lib -Wl,-rpath,/.../llvm-project/libcxx/lib test.cpp -o test