12

I want to optionally add some library paths in my CMakeLists file and the way to do it to is to have a variable set as:

set(MYLIBDIR "DEFAULT")

If the user wants to specify a custom directory he will change it to:

set(MYLIBDIR /path/to/dir1
/path/to/dir2)

So in order to check if the user has indeed provided extra directories I check:

if(NOT ${MYLIBDIR} STREQUAL "DEFAULT")
 link_directories(${MYLIBDIR})
endif()

When I try to do this I am getting an error from cmake. Is there a way to concatenate all of the elements of a variable before the string comparison?

user4168715
  • 159
  • 2
  • 3
  • 11
  • 1
    Could you please also give the error message you got? Can you try `if(NOT MYLIBDIR STREQUAL "DEFAULT")`? For more details see [What's the CMake syntax to set and use variables?](http://stackoverflow.com/questions/31037882/whats-the-cmake-syntax-to-set-and-use-variables) and [CMake compare to empty string with STREQUAL failed](http://stackoverflow.com/questions/19982340/cmake-compare-to-empty-string-with-strequal-failed). – Florian Mar 18 '16 at 15:31
  • The error is: `CMake Error at CMakeLists.txt:264 (if): if given arguments: "NOT" " /path/to/dir1" " /path/to/dir2" "STREQUAL" "DEFAULT" Unknown arguments specified ` – user4168715 Mar 18 '16 at 15:35
  • Oh thank you very much. I found the answer in the first link that you referenced. I had to write `if(NOT "${MYLIBDIR}" STREQUAL "DEFAULT")` – user4168715 Mar 18 '16 at 15:46
  • You're welcome. Then this was a quoting issue. I've put some details in my answer found below. And I added a link to [cmake: when to quote variables?](http://stackoverflow.com/questions/35847655/cmake-when-to-quote-variables) which in this case probably explains the inner working better then the previous two links. – Florian Mar 18 '16 at 15:59

1 Answers1

34

Turning my comment into an answer

Concatenating a list would simply be achieved by putting quotes around the variable reference:

if(NOT "${MYLIBDIR}" STREQUAL "DEFAULT")

would be the same as

if(NOT "/path/to/dir1;/path/to/dir2" STREQUAL "DEFAULT")

But I would recommend

if(NOT MYLIBDIR STREQUAL "DEFAULT")

For more details see

Community
  • 1
  • 1
Florian
  • 39,996
  • 9
  • 133
  • 149