VSCode C++ c_cpp_properties.json“defines”没有定义ubuntu上的符号

w51jfk4q  于 12个月前  发布在  Vscode
关注(0)|答案(1)|浏览(175)

我有一个main.cpp

#include <stdio.h>

int main(){
#if defined(_LINDEBUG)
#if defined(VSCODE)
    printf("VSCODE defined"\n);
#else
    printf("VSCODE not defined\n");
#endif
#endif
}

我的.vscode/文件夹中的c_cpp_properties.json文件如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${default}"
            ],
            "defines": ["VSCODE"],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ]
}

我通过make编译并链接它:

g++ -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp   -c -g -D_LINDEBUG -std=c++14 -MMD -MP -MF "build/Debug/GNU-Linux/_ext/511e4115/main.o.d" -o build/Debug/GNU-Linux/_ext/511e4115/main.o ../src/main.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -fno-common -fPIC -fno-strict-aliasing -fexceptions -fopenmp    -o dist/Debug/GNU-Linux/linux build/Debug/GNU-Linux/_ext/511e4115/main.o  -lm -lpthread -ldl

我希望通过.json文件定义VSCODE_LINDEBUG通过make编译定义为-D_LINDEBUG。净效果是输出为:
VSCODE not defined
有没有办法通过c_cpp_properties.json文件而不是通过make参数来定义一些宏?
tasks.json是:

{
            "label": "lindbgbuild",
            "type": "shell",
            "command": "make",
            "args": [
                "CONF=Debug",
                "-C",
                "./.vscode"
            ],
            "group": "build",
            "problemMatcher": []
}

Makefile存在于.vscode\文件夹中,该文件夹调用包含实际g++ ...命令的Makefile-Debug.mk

lymnna71

lymnna711#

根据文档link(https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference):
defines解析文件时要使用的IntelliSense引擎的预处理器定义列表。也可以使用=设置值,例如VERSION=1。
所以,看起来你只是为了帮助智能感知引擎而在那里添加一些定义。一些快速测试无法从c_cpp_properties. json文件中获取要嵌入到代码中的定义。我的猜测是,当您的机器上只有项目的一部分时,该选项的存在有助于编写代码。
无论如何,你没有通过make定义_LINDEBUG。这是一个编译器标志[链接]。更具体地说,它将任何名称定义为宏,并将其值设置为1。文件没有说明这些定义放在何处。
如果你粘贴的是makefile的内容,你最好学习创建一个合适的makefile,或者把它提升一个层次,学习使用像cmake这样的构建工具。
编辑:根据你的评论,如果你想让VS Code定义它们,那就只需要将参数添加到你的构建任务中。

相关问题