首页 > 解决方案 > 在带有 LLD 的 Windows 10 上使用 Clang

问题描述

编译一个简单的 hello world 程序会在使用clang. 我知道 usingclang-cl将摆脱警告。

在 Clang 的网站上,它声明:“clang-cl 是 Clang 的替代命令行界面,旨在与 Visual C++ 编译器 cl.exe 兼容。”

我不想使用 Microsoft Visual C++ 的工具链。我想使用 Clang 作为编译器,使用 LLD 作为链接器。


我编译:

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;
}

标签: c++clangllvmclang-cl

解决方案


clang是C编译器,clang++是c++之一。所以要编译为 c++,你需要clang -c main.cpp -o main.o

clang-cl另一端是替代驱动程序。如果您不想使用工具链,请不要理会它。但是,如果您像我一样尝试编译一个当前使用 MSVC 编译的项目,并且还想使用 clang 编译它,那真的很有用。

如果您不使用三元组,主要区别在于它链接到的平台 ABI。

Clang++ 链接到 mingw 标准库,而 clang-cl 使用 Microsoft 运行时。因此,名称 mangling ... 也与 MSVC 兼容。(当然,它有一个许可模式,它允许一些无效代码以实现兼容性)


推荐阅读