【问题标题】:VSCode C++ c_cpp_properties.json "defines" does not define the symbol on ubuntuVSCode C++ c_cpp_properties.json“定义”没有定义ubuntu上的符号
【发布时间】:2021-02-04 14:03:59
【问题描述】:

我有一个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

【问题讨论】:

    标签: c++ ubuntu visual-studio-code


    【解决方案1】:

    根据文档[link]

    defines IntelliSense 引擎的预处理器定义列表 在解析文件时使用。 (可选)使用 = 设置一个值,例如 例如 VERSION=1。

    因此,您似乎只会在此处添加一些定义以帮助 Intellisense 引擎。一些快速测试无法从 c_cpp_properties.json 文件中获取要嵌入到代码中的定义。我的猜测是,当您的机器上只有项目的一部分时,存在帮助编写代码的选项。

    对于它的价值,您不是通过 make 定义 _LINDEBUG。这是一个编译器标志[link]。更具体地说,它将任何名称定义为宏并赋予其值 1。文档没有说明这些定义的放置位置。

    如果你粘贴的是你的makefile的内容,你最好学习创建一个合适的makefile,或者提高一个水平,学习使用像cmake这样的构建工具。

    编辑:根据您的评论,如果您希望 VS Code 定义参数,只需将参数添加到构建任务即可。

    【讨论】:

    • you are not defining _LINDEBUG via make 。如果有-D_XYZ 作为编译器参数,那么我的代码中的#ifdef _XYZ#if defined(_XYZ) 将评估为true。我的措辞可能很草率,但确实_XYZ 是在我的makefile 中定义的。我不手动编写我的makefile。我在 ubuntu 上使用 Netbeans IDE 来完成这项工作,并将它们用于 VSCode。
    猜你喜欢
    • 2020-09-14
    • 2021-08-08
    • 2012-01-02
    • 1970-01-01
    • 2016-04-30
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多