Switch from "Apple clang" to "Homebrew clang" in VS code configuration

Hello.
I am new to LLVM and need a bit of help to setup to start building.

Currently I have 2 versions of clang in my system.

  1. Installed using brew and
  2. Installed by XCode (which is set by default and referred as “Apple clang”).

I researched how to configure VisualStudio Code and get answers, here or guides [1], [2], [3] but weren’t useful for me.

(BTW brew noted clang/llvm as a “keg-only” package. I’ve uninstalled xcode-tools, but later, when I used otool, a xcode window popped requiring to install xcode-tools…)

Anyway, I have this setup right now

/usr/bin/clang <--- This one installed by XCode tools
/usr/local/Cellar/llvm/<version>/bin/clang <---- This one installed by homebrew

Now I am in a situation where I want to compile with the clang version installed by homebrew using VS code, but I didn’t found any config files to switch from xcode clang to “homebrew” clang.

These are my config files so far:

tasks.json (manually changed command entry)

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: clang build active file",
			"command": "/usr/local/Cellar/llvm/13.0.1_1/bin/clang",
			"args": [
				"-fdiagnostics-color=always",
				"-g",
				"${file}",
				"-o",
				"${fileDirname}/${fileBasenameNoExtension}"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/local/Cellar/llvm/13.0.1_1/bin/clang"
		}
	]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/Cellar/llvm/13.0.1_1/include/**",
            ],
            "defines": [],
            "macFrameworkPath": [],
            "compilerPath": "/usr/local/Cellar/llvm/13.0.1_1/bin/clang",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

A sample version.c file

#include <clang-c/Index.h>
#include <stdio.h>

void show_clang_version(void) {
        CXString version = clang_getClangVersion();
        printf("%s\n", clang_getCString(version));
        clang_disposeString(version);
}

int main( int argc, const char *const * argv )
{
        show_clang_version();
        return 0;
}

This is the error I get

Starting build...
/usr/local/Cellar/llvm/13.0.1_1/bin/clang -fdiagnostics-color=always -g /Users/bel/libclang-test/vs-project/version.c -o /Users/bel/libclang-test/vs-project/version
/Users/bel/libclang-test/vs-project/version.c:1:10: fatal error: 'clang-c/Index.h' file not found
#include <clang-c/Index.h>
         ^~~~~~~~~~~~~~~~~
1 error generated.

I know that llvm-config can provide me with the right paths, but I don’t know where to put them in the configuration files. I am not using CMake since I just started with LLVM.

Any ideas?

I have little experience in VSCode but my hand-waving guess is that the includePath in this configuration file is used for code-completion. Thus I guess you need to still need something like -I/usr/local/Cellar/llvm/13.0.1_1/include in the compiler invocation of the first config file

1 Like

I would strongly recommend you use a real build system than the build commands stuff in VSCode - it’s serviceable but cannot replace a real build system like CMake.

With CMake you can pass the paths to the compiler with CMAKE_C/CXX_COMPILER or even better use a CMAKE_TOOLCHAIN_FILE.

2 Likes

Thank you Tobias.
Does that mean I should reinstall llvm clang with vcpkg?

If you can point me to a boilerplate or starter template for vscode/cmake using libclang that would be awesome.