本文介绍了替换CMake列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换CMake列表中的值,但似乎没有任何支持此列表操作。

I needed to replace a value in a CMake list, however there does not seem to be any support for this list operation.

我想出了这个代码:

macro (LIST_REPLACE LIST INDEX NEWVALUE)
    list (REMOVE_AT ${LIST} ${INDEX})
    list (LENGTH ${LIST} __length)

    # Cannot insert at the end
    if (${__length} EQUAL ${INDEX})
        list (APPEND ${LIST} ${NEWVALUE})
    else (${__length} EQUAL ${INDEX})
        list (INSERT ${LIST} ${INDEX} ${NEWVALUE})
    endif (${__length} EQUAL ${INDEX})
endmacro (LIST_REPLACE)

# Example
set (fubar A;B;C)
LIST_REPLACE (fubar 2 "X")
message (STATUS ${fubar})

如何实现?

推荐答案

如果检查:

You don't need the if check:

project(test)
cmake_minimum_required(VERSION 2.8)

macro(LIST_REPLACE LIST INDEX NEWVALUE)
    list(INSERT ${LIST} ${INDEX} ${NEWVALUE})
    MATH(EXPR __INDEX "${INDEX} + 1")
    list (REMOVE_AT ${LIST} ${__INDEX})
endmacro(LIST_REPLACE)

set(my_list A B C)
LIST_REPLACE(my_list 0 "FIRST")
LIST_REPLACE(my_list 1 "SECOND")
LIST_REPLACE(my_list 2 "THIRD")
message (STATUS "NEW LIST: ${my_list}")

这篇关于替换CMake列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:54