Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is common way to split string into list with CMAKE?

Tags:

cmake

Imagine I have the following string :

set(SEXY_STRING "I love CMake") 

then I want to obtain SEXY_LIST from SEXY_STRING so I can do

list(LENGTH SEXY_LIST len) 

and len is equal 3.

I've found several macros on web, but I really want to know how to do it in "natural" way. This operation seems to be very basic and widely used.

like image 681
Alexander K. Avatar asked Mar 11 '11 12:03

Alexander K.


People also ask

How do I create a list in CMake?

A list in cmake is a ; separated group of strings. To create a list the set command can be used. For example, set(var a b c d e) creates a list with a;b;c;d;e , and set(var "a b c d e") creates a string or a list with one item in it.

What is ${} in CMake?

Local Variables You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.


1 Answers

Replace your separator by a ;. I don't see any other way to do it.

cmake_minimum_required(VERSION 2.8)  set(SEXY_STRING "I love CMake") string(REPLACE " " ";" SEXY_LIST ${SEXY_STRING})  message(STATUS "string = ${SEXY_STRING}") # string = I love CMake  message(STATUS "list = ${SEXY_LIST}") # list = I;love;CMake  list(LENGTH SEXY_LIST len) message(STATUS "len = ${len}") # len = 3 
like image 78
tibur Avatar answered Sep 22 '22 12:09

tibur