CMAKE:在函数内创建和构建列表——具有目录或全局范围
我并不完全熟悉 cmake 的作用域规则。我需要在为 IDL 进行 RPC 代码生成时建立各种文件的列表。
function(generate_rpc file_name)
set(PROTO_FILES ${PROTO_FILES} ${file_name})
endfunction(generate_rpc)
generate_rpc(BasicProtocol.proto)
generate_rpc(dummy.proto)
message(STATUS "PROTO FILES: ${PROTO_FILES}")
每次列表都是空的。我需要可以从函数内构建的可附加列表。
I am not entirely familiar with the scoping rules of cmake. I need to buildup a list of various files whilst doing RPC code-generation for an IDL.
function(generate_rpc file_name)
set(PROTO_FILES ${PROTO_FILES} ${file_name})
endfunction(generate_rpc)
generate_rpc(BasicProtocol.proto)
generate_rpc(dummy.proto)
message(STATUS "PROTO FILES: ${PROTO_FILES}")
The list is empty each time. I need append-able list that can be built from within a function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽管宏的定义和调用方式与函数相同,但它们之间存在一些差异,例如范围和执行时间。
范围:
执行:它的工作方式类似于C++或C
宏:在配置之前将变量名称替换为字符串。
功能:执行时替换变量名。
总之,在 set 命令中添加 PARENT_SCOPE 标志
set(PROTO_FILES ${PROTO_FILES} ${file_name} PARENT_SCOPE)
Although Macros are defined and called in the same manner as functions, there are some differences between them, for example in SCOPE and when its execution.
SCOPE:
EXECUTION: it works like C++ or C
Macro: the variable names are replaced to strings before configuring.
Function: the variable names are replaced during execution.
In conclusion, add the PARENT_SCOPE flag in set command
set(PROTO_FILES ${PROTO_FILES} ${file_name} PARENT_SCOPE)
使用宏而不是函数似乎可以做到这一点:
编辑:
根据 http://www.cmake.org/cmake/help/syntax.html (应该是在手册页中,IMO):
CMake 函数为变量创建本地范围,宏使用全局范围。
Using a macro instead of a function seems to do it:
EDIT:
According to http://www.cmake.org/cmake/help/syntax.html (should be in the man page, IMO):
CMake functions create a local scope for variables, and macros use the global scope.