clang“你好,世界!” Windows 中的链接错误
我刚刚下载了 CLang 源代码,使用 CMake 创建了 Visual C++ 10 IDE 工作区,并从 Visual C++ 10.0 (express) 构建了所有内容。
现在我在 hello world 上收到一堆链接器错误:
d:\dev\test> type con >foo.cpp #include <iostream> using namespace std; int main() { cout << "Hello, cling-clong world!" << endl; } ^Z d:\dev\test> clang++ foo.cpp foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry D2Ev foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol ___cxa_allocate_exception referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol ___cxa_throw referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol __ZSt17iostream_categoryv referenced in function __ZSt15make_er
那么我该怎么办呢?
I just downloaded the CLang sources, made a Visual C++ 10 IDE workspace by using CMake, and built everything from Visual C++ 10.0 (express).
Now I get a bunch of linker errors on hello world:
d:\dev\test> type con >foo.cpp #include <iostream> using namespace std; int main() { cout << "Hello, cling-clong world!" << endl; } ^Z d:\dev\test> clang++ foo.cpp foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry D2Ev foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol ___cxa_allocate_exception referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol ___cxa_throw referenced in function __ZNSt8ios_base5clearEib foo-839435.o : error LNK2019: unresolved external symbol __ZSt17iostream_categoryv referenced in function __ZSt15make_er
So what can I do about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想在 Windows 上试验 Clang,我建议使用 MinGW 构建版本,就像我在这里提供的版本(或者使用您最喜欢的 MinGW 工具链自行构建):
32 位
64 位< /p>
您将需要 gcc 和 clang 软件包(没有 linux/mac/cygwin 后缀的软件包)并将它们解压到同一目录。 Clang使用gcc来链接,并且可以链接到GCC的libstdc++,几乎可以让你访问C++标准库。例外情况适用于 32 位版本。不过,我对调试信息没有任何运气。
我们正在努力为 Clang 提供更好的 MS 支持,但这是一项进展缓慢的任务。
If you want to experiment with Clang on Windows, I suggest using a MinGW built version, like the one I provide here (or build it yourself using your favourite MinGW toolchain):
32-bit
64-bit
You will need both the
gcc
andclang
packages (those without linux/mac/cygwin suffixes) and extract them to the same directory. Clang uses gcc to link, and can link to GCC's libstdc++, pretty much giving you access to the C++ standard library. Exceptions work for the 32-bit version. I haven't had any luck with debug info though.Work is being done to bring better MS support to Clang, but it's a slowly progressing task.
我认为这里有一个误解。
Clang(慢慢地)被教导如何解析 MFC 标头。据我所知,François Pichet 几乎是独自完成这个项目,但整个头文件中只有几个错误(!),而且显然 VC++ 解析附带的标准库很久以前就已经完成了。
然而,这是关于AST生成,而不是代码生成。 Clang 目前无法正确生成与 VC++ 库交互的代码。不仅名称修改不完整,而且 ABI 仍在塑造中(过去 2 个月内有许多补丁以获得正确的填充/对齐),并且存在一个长期存在的问题(例外)。
如果你想在 Windows 上使用 Clang,你应该使用 MinGW 或 MinGW64,请参阅 ruben 的回答。
I think there is a misunderstanding here.
Clang is (slowly) being taught how to parse MFC headers. As far as I know, François Pichet is about alone on this project but there are only a few errors in the whole headers lot (!), and obviously the standard library shipped with VC++ parsing has been completed a long time ago.
However, this is about AST generation, not Code Generation. Clang is currently unable to properly generate code to interact with VC++ libraries. Not only is the name mangling incomplete, but the ABI is still being shaped (there have been numerous patches in the last 2 months to get the right padding/alignment) and there is a long standing issue with exceptions.
If you wish to use Clang on Windows, you should use MinGW or MinGW64, see ruben's answer.
与 MinGW gcc 不同,clang 不包含 glibc 库,因此默认情况下,它没有标准库或任何其他库。我不知道如何使用 Visual C++ 标准库,但从您发布的内容中可以清楚地看出,默认情况下它会生成像 gcc 这样的符号,因此您需要将它与 glibc 或 newlib 一起使用...
或者您使用
-Ldirectory
指定 .lib 文件的库路径(或 glibc 的 .a),或者将它们放入默认的 clang 搜索路径之一(运行:clang -v -x c++ -fsyntax-仅某些文件
才能看到它们。)Unlike MinGW gcc, clang does not include the glibc library, so by default, it has no standard library or whatsoever. I don't know how to use the Visual C++ standard libary with it, but its pretty clear from what you post, that by default it produces symbols like gcc, so you need to use it with glibc or newlib...
And either you specify the the libary path with
-Ldirectory
to the .lib files (or .a for glibc), or put them into one of default clang search pathes (run:clang -v -x c++ -fsyntax-only some file
to see them.)仅使用 clang++ 编译 .cpp,然后尝试 ld 链接器而不是 MS 链接。对我有用。
Use clang++ only to compile .cpp and then instead of MS link try ld linker. Works for me.
Clang/LLVM 对 Windows 的支持仍然不完善。尝试包含此选项:
-Xclang -cxx-abi -Xclang microsoft
。但是,我不确定它是否可以正常工作。Clang/LLVM's supports for Windows are still not perfect. Try to include this option:
-Xclang -cxx-abi -Xclang microsoft
. But, I'm not sure whether it makes work correctly.