在VS代码配置中,将Apple Clang转换为Homebrew Clang

发布于 2025-02-08 22:25:15 字数 3402 浏览 2 评论 0 原文

我的系统中有2个版本的Clang。一个使用啤酒安装,另一种是Xcode(默认设置为“ Apple Clang”)。上一答案,, here 或guides /vscode-cpp-11-Mac/vscode-cpp-11-mac/“ rel =” nofollow noreferrer“> [1] , [2] [3] 对我没有用。

BTW Brew将Clang/LLVM称为“仅小桶”软件包。我已经卸载了Xcode-tools,但是例如,稍后,当我使用Otool时,弹出了一个XCode窗口,需要安装Xcode-tools ...无论如何,我现在就已经设置了:

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

现在我正处于我想与由Homebrew使用VS代码安装的Clang版本,但我没有找到任何配置文件可以从Xcode Clang切换到“ Homebrew” Clang。

这些是我到目前为止的配置文件:

tasks.json(手动更改命令条目)

{
    "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/bin/gcc-11",
            "cStandard": "gnu17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

示例版本.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;
}

这是我得到的错误

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 have 2 versions of clang in my system. One installed using brew and another one it was required by XCode (which is set by default and referred as "Apple clang"). Previous answers, here or guides [1], [2], [3] weren't useful for me.

BTW brew noted clang/llvm as a "keg-only" package. I've uninstalled xcode-tools, but for example 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/bin/gcc-11",
            "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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文