c++ - 有没有办法从 Makefile 访问 VS Code c_cpp_properties.json 文件中的定义?
问题描述
我开始在 Linux 上试验 VS Code 并尝试构建一个 C++ 项目。该项目包含一堆源文件和一个 Makefile。要为 IntelliSense 定义 C/C++ 宏,VS Code 支持"defines"
文件中名为 c_cpp_properties.json 的属性。
不幸的是,经过大量搜索后,我找不到如何"defines"
从 Makefile 访问这些宏,以便在编辑代码和构建代码时使用相同的宏集。我试图避免在需要调整宏时分别手动编辑 Makefile 和 JSON 文件。
解决方案
gmtt是一个 GNUmake 库,它允许足够的程序化 make 来解决此类问题:
如果 json 文件如下所示:
{
"env": {
"myDefaultIncludePath": ["${workspaceFolder}", "${workspaceFolder}/include"],
"myCompilerPath": "/usr/local/bin/gcc-7"
},
"configurations": [
{
"name": "Mac",
"intelliSenseMode": "clang-x64",
"includePath": ["${myDefaultIncludePath}", "/another/path"],
"macFrameworkPath": ["/System/Library/Frameworks"],
"defines": ["FOO", "BAR=100", "BAZ = \"some string with spaces [brackets] and quotes\""],
"forcedInclude": ["${workspaceFolder}/include/config.h"],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"compileCommands": "/path/to/compile_commands.json",
"browse": {
"path": ["${workspaceFolder}"],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
然后以下 make 文件将隔离定义及其值:
include gmtt/gmtt.mk
c_cpp_properties.json := $(file <c_cpp_properties.json)
# the glob pattern uses [[] and []] to catch the square brackets
parsed_json := $(call glob-match,$(c_cpp_properties.json),*"defines": [[]*[]]$(comma)$(newline)*)
defines := $(word 4,$(parsed_json))
# first save all escaped quotes by converting them to the -never-matching character:
defines := $(subst \",$(-never-matching),$(defines))
# all quotes which are left are separators now, replace with space:
defines := $(subst ",$(space),$(defines))
# get rid of spaces and commas between defines: remove any number of leading or trailing space-replacements
# and finally convert back the escaped quotes to their now non-escaped ("\" removed) form:
$(call while,$$(or $$(findstring $$(space)$$(-spacereplace),$$(defines)),\
$$(findstring $$(-spacereplace)$$(space),$$(defines))),\
defines := $$(subst $$(space)$$(-spacereplace),$$(space),$$(subst $$(-spacereplace)$$(space),$$(space),$$(defines))),\
defines := $$(subst $$(-never-matching),",$$(subst $$(space)$$(comma)$$(space),$$(space),$$(defines))))
# Print what we have so far:
$(info --------------$(newline)$(defines)$(newline)-------------)
# for each item in $(defines): split at ":", then reverse the space-replacement and print them
$(foreach d,$(defines),$(info define-name:$(call spc-unmask,$(firstword $(subst =, ,$(d)))) define-content: $(call spc-unmask,$(word 2,$(subst =, ,$(d))))$(newline)))
输出:
$ make
--------------
FOO BAR=100 BAZ§=§"some§string§with§spaces,§[brackets]§and§quotes"
-------------
define-name:FOO define-content:
define-name:BAR define-content: 100
define-name:BAZ define-content: "some string with spaces, [brackets] and quotes"
需要注意的是,剖析文件的 glob 模式对于 json 文件的良好格式至关重要 -"defines"
行尾必须以 结尾],\n
,尽管您可以使用可选字符?
,但这有一个自然的 glob 模式结束没有RE那么强大。
推荐阅读
- android - 如何从 Android.mk 构建错误中获取 LOCAL_STATIC_ANDROID_LIBRARIES 名称?
- visual-studio-code - 无法在 WSL 上启动 VScode Cloud Run 插件
- android - “Null”类型不是“Widget”类型的子类型
- javascript - jQuery Ajax 从控制器操作方法返回未定义
- ubuntu - Kubernetes pods can't access the internet while host can
- swift - 如何从我的 macOS 应用程序的用户那里收集崩溃报告?
- performance - 页面处于IDLE状态超过20s如何提高性能?
- discord.js - 如何将普通消息转换为嵌入消息?不和谐.js
- html - 为什么我的时间数据在抓取后没有显示?
- html - CSS - 在动态大小的盒子之间画线