有没有办法从Makefile访问VS Code c_cpp_properties.json文件中的定义

Is there a way to access the defines in a VS Code c_cpp_properties.json file from a Makefile?

本文关键字:properties cpp json 定义 文件 Makefile 访问 Code VS 有没有      更新时间:2023-10-16

我开始在Linux上试用VS代码,并试图构建一个C++项目。该项目包含一堆源文件和一个Makefile。要为IntelliSense定义C/C++宏,VS Code支持名为C_cpp_properties.json的文件中的属性"defines"

不幸的是,经过大量搜索,我找不到如何从Makefile访问这些"defines"宏,以便在编辑代码和构建代码时使用同一组宏。我正在努力避免在需要调整宏时分别手动编辑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强大。