c++clangllvmclang-cl

Using Clang on Windows 10 with LLD


Compiling a simple hello world program generates warnings when compiling with clang. I understand that using clang-cl will get rid of the warnings.

On Clang's website, it states: "clang-cl is an alternative command-line interface to Clang, designed for compatibility with the Visual C++ compiler, cl.exe."

I do not want to use Microsoft Visual C++'s tool chain. I want to use Clang as the compiler and LLD as the linker.


clang

I compiled:

clang main.cpp

and got warnings:

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)

It still generated an a.exe file. However, this same command generates no file when run in a Debian terminal (WSL Windows Subsystem for Linux) and has errors.


clang && lld [lld-link]

I tried compiling to object code and then passing this to LLD. It resulted in an error:

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

Solution

  • clang is the C compiler, clang++ is the c++ one. So to compile as c++, you need clang -c main.cpp -o main.o

    clang-cl on the other end is an alternative driver. If you don't want to use the toolchain, don't bother about it. However, if you are like me and try to compile a project that currently compiles with MSVC and want to also compile it with clang, it's really useful.

    The main difference, if you don't play with triples is the platform ABI it links to.

    Clang++ links against the mingw standard library while clang-cl uses the Microsoft runtime. As a consequence, the name mangling ... is also MSVC compatible. (Of, and it has a permissive mode in which it allows some invalid code for compatibility)