c++ - Clang 不能编译基本的头文件(比如) 在 macOS 中
问题描述
我的 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++
,但也有相同的错误。
解决方案
尝试在 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
推荐阅读
- c# - 比较两个字段时,来自 OrganizationServiceProxy 的 Linq Queryable 抛出异常
- logstash - Logstash 文件输入不解析文件
- flutter - 整数列表的基本操作
- twilio - 特维利奥
连接器选项? - android - 如何从 Android 项目的 build.gradle 运行 Kotlin 任务?
- python - 将字符串与“{”符号进行比较的问题
- r - R: Formattable -Column 不会根据条件更新
- angular - 什么时候在 Angular 中使用路由?
- python-3.x - 在连接期间停止 python 请求
- javascript - 为什么 Number 在我的 Teaspoon/Jasmine 规格测试中不是构造函数?