Clang-cl.exe compiler flags

I am using Visual Studio 17 2020 version of clang-cl.exe from the command line.

How does one create a DLL corresponding to a C file ?
The Microsoft flag for this is /LD; see

But I get the following message when I try to use that flag:

clang-cl.exe /EHs /EHc /c /LD /TC get_started.c
clang-cl: warning: argument unused during compilation: '/LD' [-Wunused-command-line-argument]

Hi Brad,

It’s because of the /c flag. That means clang-cl will just compile, and doesn’t invoke the linker which is the tool that creates the DLL. If you drop that flag it should work:

C:\src\temp>type a.c
void __declspec(dllexport) f() {}

C:\src\temp>clang-cl /EHs /EHc /LD /TC a.c
   Creating library a.lib and object a.exp

C:\src\temp>dir a.dll
[...]
01/15/2024  04:18 PM            91,136 a.dll

I think that /LD is an option for Microsoft’s compiler (cl) and not it’s linker (link); see

Am I mistaken in this regard ?

I want to compile my routines separately (into separate object files) and then link them into a single DLL. Can I use Microsoft’s linker with clang-cl.exe objects ? or do I need to use clang-cl.exe with certain options to do the linking ?

Yes, /LD is an option to the compiler, but the main effect is that it tells the linker to build a DLL. From the doc (/MD, -MT, -LD (Use Run-Time Library) | Microsoft Learn):

/LD […] Passes the /DLL option to the linker. The linker looks for, but does not require, a DllMain function. If you do not write a DllMain function, the linker inserts a DllMain function that returns TRUE.

But the /c flag makes the compiler not invoke the linker, so clang warns that the flag doesn’t do anything.

Yes, you can use Microsoft’s linker with clang-cl objects.

If you want to compile files separately and invoke the linker yourself, you should pass /dll to the linker. For example:

C:\src\temp>clang-cl /c /TC a.c
C:\src\temp>clang-cl /c /TC b.c
C:\src\temp>link /dll a.obj b.obj

For my application, I am compiling multiple object files and linking them in one DLL. The user can choose to use cl or clang-cl. Removing /LD from my clang-cl and cl compiles solved my problem.

I should have noticed that the documentation for /LD (see link above) states:

/LD Creates a DLL.
Passes the /DLL option to the linker. …

Thanks !!