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

Posted

技术标签:

【中文标题】有没有办法从 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? 【发布时间】:2019-12-04 01:45:56 【问题描述】:

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

不幸的是,经过大量搜索,我找不到如何从 Makefile 访问那些 "defines" 宏,以便在编辑代码和构建代码时使用相同的宏集。我试图避免在需要调整宏时分别手动编辑 Makefile 和 JSON 文件。

【问题讨论】:

【参考方案1】:

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 强大。

【讨论】:

以上是关于有没有办法从 Makefile 访问 VS Code c_cpp_properties.json 文件中的定义?的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法定义全局 Makefile(隐式)规则?

有没有办法从 VS 中的调试器中转储流

有没有办法获取定义了make变量的makefile的名称?

VIM:有没有一种简单的方法可以从 Vim 管理 Visual Studio 解决方案/makefile 项目? [关闭]

nmake.exe:有没有办法从宏中指定的一组文件中排除一个文件?

从 Visual Studio 2008 解决方案创建 Unix makefile