有没有一种方法可以从Makefile访问VS Code c_cpp_properties.json文件中的定义?

时间:2019-12-04 01:45:56

标签: c++ linux makefile visual-studio-code

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

不幸的是,经过大量搜索之后,我找不到如何从Makefile中访问那些"defines"宏的方法,以便在编辑代码和构建代码时使用同一组宏。我试图避免每次需要调整宏时都必须分别手动编辑Makefile和JSON文件。

1 个答案:

答案 0 :(得分:0)

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"行的必须结尾以{{1} },尽管您可以使用可选字符],\n来玩耍,但这自然而然地以glob模式结尾,其功能不如RE强大。