c++ - 在带有 LLD 的 Windows 10 上使用 Clang
问题描述
编译一个简单的 hello world 程序会在使用clang
. 我知道 usingclang-cl
将摆脱警告。
在 Clang 的网站上,它声明:“clang-cl 是 Clang 的替代命令行界面,旨在与 Visual C++ 编译器 cl.exe 兼容。”
我不想使用 Microsoft Visual C++ 的工具链。我想使用 Clang 作为编译器,使用 LLD 作为链接器。
- “与 Visual C++ 编译器的兼容性”是什么意思?
- 我如何知道默认使用哪个链接器?Clang 的文档说默认使用 LLD,但是,如果是这样,那为什么会有警告呢?为什么
clang-cl
这个警告的推荐解决方案是?
铛
我编译:
clang main.cpp
并收到警告:
main-a354e7.o : warning LNK4217: locally defined symbol _CxxThrowException imported in function "class std::num_put<char,class std::ostreambuf_iterator<char,struct std::char_traits<char> > > const & __cdecl std::use_facet<class std::num_put<char,class std::ostreambuf_iterator<char,struct std::char_traits<char> > > >(class std::locale const &)" (??$use_facet@V?$num_put@DV?$ostreambuf_iterator@DU?$char_traits@D@std@@@std@@@std@@@std@@YAAEBV?$num_put@DV?$ostreambuf_iterator@DU?$char_traits@D@std@@@std@@@0@AEBVlocale@0@@Z)
main-a354e7.o : warning LNK4217: locally defined symbol __std_terminate imported in function "int `public: __cdecl std::locale::~locale(void)'::`1'::dtor$6" (?dtor$6@?0???1locale@std@@QEAA@XZ@4HA)
它仍然生成了一个a.exe
文件。但是,同样的命令在 Debian 终端(Linux 的 WSL Windows 子系统)中运行时不会生成文件,并且会出现错误。
clang && lld [lld-link]
我尝试编译为目标代码,然后将其传递给 LLD。它导致了一个错误:
clang -c main.cpp -o main.o
lld-link main.o
lld-link: error: could not open libcpmt.lib: no such file or directory
- 这是什么图书馆?它从哪里来的?为什么找不到?
主文件
#include <iostream>
using namespace std;
int add1(int x);
int main()
{
int a;
a = 1;
cout << a << endl; //prints a and goes to new line
a = add1(a);
return 0; //must return 0 to tell the OS the
//program executed successfully
}
int add1( int x )
{
x = x + 1;
return x;
}
解决方案
clang
是C编译器,clang++
是c++之一。所以要编译为 c++,你需要clang -c main.cpp -o main.o
clang-cl
另一端是替代驱动程序。如果您不想使用工具链,请不要理会它。但是,如果您像我一样尝试编译一个当前使用 MSVC 编译的项目,并且还想使用 clang 编译它,那真的很有用。
如果您不使用三元组,主要区别在于它链接到的平台 ABI。
Clang++ 链接到 mingw 标准库,而 clang-cl 使用 Microsoft 运行时。因此,名称 mangling ... 也与 MSVC 兼容。(当然,它有一个许可模式,它允许一些无效代码以实现兼容性)
推荐阅读
- javascript - 如何将日期字符串解析为本地日期?
- node.js - node.js-10:TypeError:无法读取未定义的属性“路径”
- ios - 移动客户端不应该终止与 API 的连接吗?
- php - 我如何能够在我的数据表服务器端脚本中获取主键值数组作为响应
- sql - 我的情况的事务复制或快照复制
- javascript - 根据下拉选择更改隐藏字段值
- javascript - Angular 无法获取文档 ID
- vba - 有没有办法仅在启用宏的情况下显示 word 文件的内容?
- wordpress - Wordpress – 为不同的页面加载不同的 CSS 不起作用
- ios - 如何在另一个 pod 的 podspec 中使用私有 pod 作为依赖项?