有没有办法从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?
我开始在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强大。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- Linux的Cpp上的计时器
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 命名空间中具有.h和.cpp文件的类
- 内置函数可查看CPP中的成员变量
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- Cpp-Tuple使用带有变量的get
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何通过cpp程序运行shell脚本
- 使用2个键的cpp-stl::优先级队列排序不正确
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp文件之间切换窗口?在Qt中
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 我需要知道编译器如何在cpp中使用析构函数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 无法在UE4中包含BP类到CPP类
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 无法使用Qt Creator在Windows中构建yaml-cpp