Can swift's clang.exe be used as the generalized clang compiler?


I'm using Windows, the swift installer will install clang.exe at C:\Library\Developer\Toolchains\unknown-Asserts-development.xctoolchain\usr\bin and I think I need to add the path to my system variable of 'path'.

In that case, the clang.exe installed from swift will be available at system level, then can it be used as a generalized clang.exe, for example, to compile other c/c++ source code?

The reason why I ask is that I heard that lldb from swift is not same as the original one.

Just like the lldb in the installer, it does have patches that aren't in upstream LLVM, but that doesn't mean it isn't fully functional.


@felixfxu, yes, as @John_McCall mentioned, the compiler does have some additional patches for Swift, but, overall, it should be usable as a general C/C++ compiler as well (and in fact, in some cases is required). The path that you are referring to should already be added to Path when you install Swift on Windows (the installer will configure this). Of course, the version of clang that is being shipped is not from main, so there may be occasional issues that need to be fixed, but we should be able to backport self-contained changes if necessary.

1 Like

In fact, you don't even have to invoke clang directly. You can just run swiftc with a .c file and it does the right thing (at least here on macOS):

$ cat x.c
#include <stdio.h>

int main() {
  printf("Hello world.\n");
$ xcrun swiftc x.c
$ ./x
Hello world.

I don't think that this works as well on Windows due to the need to inject the swift registrar as swiftc assumes that the target is a Swift target and not only a C/C++ target. If you add the explicit link to the swiftCore library, it should work on Windows as well! (This is something that we end up seeing with swift-driver).

Doesn't seem to work on Linux either, but I could be doing it wrong:

$ swiftc hello.c
error: autolink-extract command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: error opening input file 'hello.c' (The file was not recognized as a valid object file
error: fatalError

Using swift rather than swiftc spews a dozen errors as it tries to parse the C file as Swift code.