5

I am currently learning C, taking a CS50 online class via EDX/Harvard. They have us using Clang inside the CS50 IDE/Sandbox which is cloud-based. They haven't explained how to use Clang outside of that tho. Thus I am wondering; How do I setup clang in windows 10 ? as well as for use with VisualStudio Code?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Josh Gray
  • 69
  • 1
  • 1
  • 5
  • Yes definitely! Installing a compiler will also give you some experience on how to install stuff. Have a look at this: https://clang.llvm.org/get_started.html . It has a section called "Using Visual Studio" , it worked for me :) – Rahul Bharadwaj Sep 16 '20 at 06:21
  • For installing git on windows, use powershell instead of cmd. – Rahul Bharadwaj Sep 16 '20 at 06:23
  • Just use the updated versions of LLVM and MinGW http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt2/ – user10191234 Sep 16 '20 at 09:17

3 Answers3

12

On Windows, Clang is not self-sufficient, and is supposed to be used in combination with an other compiler: either MinGW (GCC) or MSVC. Clang is going to use the standard library (and other libraries/headers) of that compiler, since it doesn't ship with ones of its own.

If you want to use it with MSVC and have it installed, running clang-cl instead of cl should just work.

But since you mentioned VSC, I assume you don't want MSVC. Then...

If you want to use it with MinGW and have it installed, use clang --target=x86_64-w64-windows-gnu instead of gcc, and it should also just work. (That's assuming your MinGW produces 64-bit apps. Replace x86_64 with i686 if it's 32-bit.)

If you don't have MinGW yet, you can get a fresh version from MSYS2. Then you have an option to install their unofficial build of Clang instead of the regular one, which has an advantage of using --target=x86_64-w64-windows-gnu automatically (so you don't have to write it manually), but also takes up considerably more drive space, and used to be a bit unstable for me in the past.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • But Mingw doesn't ship any libraries either, it uses MSVC C run-time. Which in turn isn't quite up to date with the C standard. – Lundin Sep 16 '20 at 12:50
  • (Though from a student's perspective it really shouldn't matter if they use gcc or clang, except maybe that the latter is arguably supposed to have better compiler messages) – Lundin Sep 16 '20 at 12:51
  • 1
    @Lundin Yes, programs compiled with MinGW normally use `msvcrt.dll`, which is shipped with Windows. But you also need the C standard library headers, which are shipped with MinGW, but not with Clang. If OP wants to use C++, they also need libstdc++, which is too shipped with MinGW but not Clang. MinGW distributions include some other libraries too, varying by distribution. Clang seems to ship with *some* headers, but it's not self-sufficient, and by itself can't compile even a hello world. – HolyBlackCat Sep 16 '20 at 16:56
  • I didn't have any conformance problems so far. `msvcrt.dll` has non-conformant `printf`/`scanf`, but you don't have to use them. There's a flag that makes MinGW use its own, proper implementations of those functions (same thing works with Clang, if it uses MinGW headers). – HolyBlackCat Sep 16 '20 at 16:58
6

Expanding on HolyBlackCat's answer. The simplest way to get up and running using clang is to download Visual Studio (not code) and choose the following toolsets during its installation-

  • Select "Desktop development with C++"
  • Under "Desktop development with C++" also select "C++ clang tools for windows"

Click install and clang will be usable to you through the commandline, just like the CS50 terminal. You usually won't have to worry too much about playing with extra cmdline options other than the ones cs50 has taught you.

To create a C project in VS with clang-

  • Create an empty C++ project (don't worry it's just named C++ but will work just fine with C - with the C compiler - not C++ compiler)

empty project dialog

  • Go to project properties by right clicking on the project name and clicking properties

project context menu

  • Set the platform toolset to LLVM - Clang in the project general config

platform toolset

Of course, this is not a silver bullet and will not guarantee an identical experience to development on linux. But if you're a beginner, you most likely will not notice any differences and this is a quick and easy way to get started with C dev on windows.

Also remember, once you have VS + Clang installed by following the above steps - you can also write code in VSCode (though it might need some configuration - specifically, you've to point it to the directory where the header files are) and use the terminal with clang to compile.

Chase
  • 5,315
  • 2
  • 15
  • 41
1
  1. Install Clang using Visual Studio Installer.
    (1) Open 'Visual Studio Installer'.
    (2) Visual Studio Community 2022 -> click 'Modify'
    (3) In the [Installation details] checkbox list, check 'C++ Clang tools for Windows'
    (4) Install.

  2. Add clang.exe (created by the above procedure) to the Path environment variable.

  3. Now try clang --version in the terminal.

Great, you're good to go :)

starriet
  • 2,565
  • 22
  • 23