Visual studio code 如何使用VSC导航Linux内核源代码

Visual studio code 如何使用VSC导航Linux内核源代码,visual-studio-code,preprocessor,Visual Studio Code,Preprocessor,我正在从EclipseCDT转换。在使用预处理器定义(无论是在Makefile中显式指定还是通过外部自动生成的头文件包含)浏览C/C++代码时,必须使用预处理器宏功能。没有这一点,就不可能导航Linux源代码,如上所述。我正在为VSC寻找一个等效的功能。如果能给我一个指针就好了 w/问候 安装ms-vscode.cpptools扩展 在VSCode中打开内核源代码文件夹 按照,添加“${workspaceFolder}/include”和“${workspaceFolder}/arch/{you

我正在从EclipseCDT转换。在使用预处理器定义(无论是在Makefile中显式指定还是通过外部自动生成的头文件包含)浏览C/C++代码时,必须使用预处理器宏功能。没有这一点,就不可能导航Linux源代码,如上所述。我正在为VSC寻找一个等效的功能。如果能给我一个指针就好了

w/问候

  • 安装ms-vscode.cpptools扩展
  • 在VSCode中打开内核源代码文件夹
  • 按照,添加“${workspaceFolder}/include”和“${workspaceFolder}/arch/{your arch}/include”到includePath,“your arch”是x86/arm等
  • 等待智能索引

  • 我不需要添加arch文件夹(对于x86_64,它实际上是空的),而是添加一些基本定义,以使特定于编译器的类型和宏对intellisense可见。我的最小配置如下所示。为了获得完美的结果,您必须添加kernel.config文件(例如config_MMU)中配置的所有定义。其中有很多,所以通常你只关注你真正关心的少数人

    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**",
                    "${workspaceFolder}/include"
                ],
                "defines": [
                    "__GNUC__",
                    "__KERNEL__"
                ],
                "compilerPath": "/usr/bin/gcc",
                "cStandard": "c11",
                "cppStandard": "c++17",
                "intelliSenseMode": "clang-x64"
            }
        ],
        "version": 4
    }
    

    到今天为止,我发现该项目的使用优于上面列出的两个答案,因为代码导航和Intellisense工作得非常好


    该项目已发布到公共领域。

    这是对我最有效的方法(修复了中代码片段的一些问题):

    {
    “配置”:[
    {
    “名称”:“Linux”,
    “包含路径”:[
    “${workspaceFolder}/**”,
    “${workspaceFolder}/include”,
    “${workspaceFolder}/arch//include”#替换为要构建的实际体系结构
    ],
    “强制包括”:[
    “${workspaceFolder}/BUILD/include/generated/autoconf.h”
    ],
    “定义”:[
    “内核”
    ],
    “compilerPath”:“/usr/bin/gg”,用您的编译器(也叫gcc交叉编译器)替换它
    “cStandard”:“c11”,
    “intelliSenseMode”:“gcc-x64”
    }
    ],
    “版本”:4
    }
    
    您必须添加内核中配置的所有定义。配置文件中的所有定义都已存在于
    include/generated/autoconf.h
    (在
    /usr/src/linux headers-$(uname-r)/
    下),应该足够
    forceInclude
    c_cpp_properties.json
    中使用它了。我正在使用内核,这个答案非常有用。我注意到,如果您使用的是模块开发,那么也可以使用机器的linux头文件。在我的例子中,添加到
    settings.json
    :`C_Cpp.default.includePath:“[/usr/src/linux-headers-5.0.0-custom-buster/arch/x86/include/”,“/usr/src/linux-headers-5.0.0-custom-buster/include/”]`
    {
        "configurations": [
            {
                "name": "Linux",
                "includePath": [
                    "${workspaceFolder}/**",
                    "${workspaceFolder}/include",
                    "${workspaceFolder}/arch/<arch>/include" # replace <arch> with actual architecture to build
                ],
                "forcedInclude": [
                    "${workspaceFolder}/BUILD/include/generated/autoconf.h"
                ],
                "defines": [
                    "__KERNEL__"
                ],
                "compilerPath": "/usr/bin/gg", # replace this with your compiler (also gcc cross-compiler)
                "cStandard": "c11",
                "intelliSenseMode": "gcc-x64"
            }
        ],
        "version": 4
    }