Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How deep copy a list of strings

Tags:

cmake

When I use set to copy a list of strings I get a reference instead of a deep copy, because when I remove some items from my new list variable, elements are removed from the original list too.

My code looks like the following snippet:

set(NEW_LIST ${TARGET_NAME}_SRC_FILES)
message("new list content1    : ${${NEW_LIST}}")
list(REMOVE_ITEM ${NEW_LIST} ${${TARGET_NAME}_LIN64_EXCLUDED_SRC_FILES})
message("new list content2    : ${${NEW_LIST}}")
message("original list content: ${${TARGET_NAME}_SRC_FILES}")

First print give me the full list before the removal and both following are shorter and identical.

I am looking for something that will not alter the original list.

Edit: I updated the upper code snippet and the lists are filled like this:

set( ${TARGET_NAME}_SRC_FILES
     foo.cpp
     foo2.cpp)
like image 797
Xavier Bigand Avatar asked Oct 20 '25 14:10

Xavier Bigand


1 Answers

Operation

set(NEW_LIST ${TARGET_NAME}_SRC_FILES)

assigns NEW_LIST variable to the name of a variable ${TARGET_NAME}_SRC_FILES.

So double dereference of NEW_LIST returns a value of ${TARGET_NAME}_SRC_FILES variable.

# Prints value of '${TARGET_NAME}_SRC_FILES' variable.
message("new list content1: ${${NEW_LIST}}")

For assign value of one variable to another one, you need to dereference the variable:

 set(NEW_LIST ${${TARGET_NAME}_SRC_FILES})
 # Now NEW_LIST variable contains current value of '${TARGET_NAME}_SRC_FILES' one.
 # Possibly modify '${TARGET_NAME}_SRC_FILES' variable...
 # ... but content of NEW_LIST variable remains the same
 message("new list content1: ${NEW_LIST}")
like image 64
Tsyvarev Avatar answered Oct 22 '25 10:10

Tsyvarev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!