【问题标题】:Using Clang on Windows 10 with LLD在带有 LLD 的 Windows 10 上使用 Clang
【发布时间】:2019-09-22 06:51:14
【问题描述】:

编译一个简单的 hello world 程序会在使用 clang 编译时产生警告。我知道使用clang-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
  • 这个库是什么?它从哪里来的?为什么找不到?

main.cpp

#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++ clang llvm clang-cl


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢。我用clang 尝试了一些东西,但它从未编译过。它只是根据我的尝试不断生成新的或不同的警告,尽管他们的网站说明说clang 可以强制编译 C++。 clang++ 工作。
    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多