我开始在Linux上尝试VS Code,并尝试构建一个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 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)
parsed_json := $(call glob-match,$(c_cpp_properties.json),*"defines": [[]*[]]*)
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§and§quotes"
-------------
define-name:FOO define-content:
define-name:BAR define-content: 100
define-name:BAZ define-content: "some string with spaces and quotes"