用CMake检查列表是否包含特定条目的最佳方法 [英] Best way to check with CMake whether list containts a specific entry

查看:107
本文介绍了用CMake检查列表是否包含特定条目的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查列表是否包含特定条目,如以下代码片段所示:

I want to check whether a lists contains a specific entry like in the following code snipplet:

macro(foo)
if ($(ARGN} contains "bar")
  ...
endif
endmacro()

CMake不提供包含。什么是获得所需结果的最佳/最简单方法?

CMake does not offer a contains. What is best / easiest way to get the desired result?

在CMake的 Wiki 中,我找到了LIST_CONTAINS宏,但是Wiki页面已过时

In CMake's wiki I found a LIST_CONTAINS macro, but the wiki page is outdated. Is this still the best way to go or has CMake gained new capabilities?

推荐答案

对于CMake 3.3或更高版本, if 命令支持 IN_LIST 运算符,例如:

With CMake 3.3 or later, the if command supports an IN_LIST operator, e.g.:

if ("bar" IN_LIST _list)
 ...
endif()

对于较旧版本的CMake,您可以使用内置的 list(FIND)函数:

For older versions of CMake, you can use the built-in list(FIND) function:

list (FIND _list "bar" _index)
if (${_index} GREATER -1)
  ...
endif()

这篇关于用CMake检查列表是否包含特定条目的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆