Is there a way to concatenate strings in cmake?
I have a folder that only contains .cpp files with main methods. I thought this would be easy by just using a foreach through all src files. This is what I've got this far:
project(opengl-tutorial)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_definitions(
--std=c++11
)
foreach (src ${SRC_LIST})
# name = ${src} + ".out"
add_executable(${name} ${src})
target_link_libraries(${name} GL GLU GLEW glfw)
endforeach(src ${SRC_LIST})
How can I do what's described in the comment?
if you just want to deal with a string value see @nonexplosive's answer.
However if you wish to have a Cmake variable in your
CMakeLists.txt
and set that variable to some value do use either: [string()
] for Cmake 3.0+ (https://cmake.org/cmake/help/v3.0/command/string.html) orset()
for Cmake 2.0+.The reason you have two options is because older cmake doesn't support the
CONCAT
feature.Example CMakeLists.txt:
Full stdout:
Three typical CMake string concatenation methods
While the answer to this particular question will be best handled via
set
orstring
, there is a third possibility which islist
if you want to join strings with an arbitrary character.set()
Just combine strings like in bash
string(CONCAT)
Concatenate all the input arguments together and store the result in the named output variable.
string(CONCAT [...])
list(APPEND)
Appends elements to the list.
list(APPEND [ ...])
When it comes to things like compiler flags, this is the tool of choice. Lists in CMake are just semicolon separated strings and when you quote them, you get the list joined with semicolons. Then you can just string replace the semicolon.
"${src}.out"
should work fine, so you can writeset(NAME "${src}.out")
and use${NAME}
wherever you need to.This is only sort of related, but I found this answer when looking for how do you concatenate paths so that you don't get double or duplicate slashes if one ends in a slash and the other begins with one?
if
SOME_VAR="/foo/"
andSOME_OTHER_VAR="/bar/"
will give you
/foo/bar/
if you want it to give you native path delimiters (i.e. backslashes in windows), then use
TO_NATIVE_PATH
instead ofTO_CMAKE_PATH