2

(During writing this question I found a solution, so this just documents it, because I would really have needed it beforehand!)

Im writing a c++ project and use cmake for it.

In my cmake file I have:

set(MY_DEBUG_WARNINGS "-Og -Wall"
)

# Add warnings:
add_compile_options("$<$<CONFIG:DEBUG>:${MY_DEBUG_WARNINGS}>"
"$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>"
"$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color>"
)

which doesn't work, because the compile command picks up extra escaped quotes:

  "/usr/bin/clang++ -I../src/Exception/include -g \"-Og -Wall\" -fcolor-diagnostics - 
  std=c++17 -o src/Exception/CMakeFiles/exception.dir/Exception.cpp.o -c 
  /home/bob/app/src/Exception/Exception.cpp",
  "file": "/home/bob/app/src/Exception/Exception.cpp"  

The problem seems to be the space in the warnings.

This question "solves" the issue by wrapping each value in its own expression, which seems weird and awfully impractical. Is there any easier way to pass a string with spaces in it in a generator expression?

As mentioned I found an acceptable solution myself but will not yet mark it as my accepted answer because I'm not sure, this is a good solution and not a hack that may bite me if I ever need semi-colons in the options themselves.

Eike
  • 359
  • 3
  • 8

2 Answers2

3

"-Og -Wall" is a string, whereas "-Og;-Wall" is a list (as lists are semicolon separated strings).

So set(my_list -Og -Wall) (no double quotes) also creates a list. Read all about it here.

firmament
  • 80
  • 1
  • 7
  • Thank you so much! This finally makes sense! Is it just me or is the cmake documentation not helpful? I almost never understand it, when I try to read it because it almost never contains concrete examples. But maybe its not written for newbies and more experienced cmake users gain more from it. – Eike Nov 28 '20 at 11:27
  • 2
    Do you happen to know, why I cannot insert the list directly into the generator expression? Like `$<$:-Og -Wall>` It doesn't work with quotes around it or without. – Eike Nov 28 '20 at 11:33
  • Also: I guess this is a better answer for the linked question. Do you want to supply your answer there, too? If not, I will. – Eike Nov 28 '20 at 11:41
  • 3
    Agree that the docs could benefit _greatly_ from a few examples :). What I normally do is I search github for things, eg "target_compile_definitions language:cmake" to find something concrete. – firmament Nov 28 '20 at 18:41
  • For some reason when you use a list in a generator expression the syntax is different, then there's a comma between the elements: `$<$:${some_variable}>` – firmament Nov 28 '20 at 18:54
  • Beware that if you are on an older release, I remember getting bitten by this on at least 3.10, you could not add multiple values to genex as above, then you had to repeat the conditon for each value. – firmament Nov 28 '20 at 18:57
  • That sounds like a useful hint, I'll try that (searching for examples on github) next time. For your other comment: I meant, that I cannot replace `${some_variable}` with the list itself. Or rather I can but then have to use semi-colon instead of space. But well, suits me fine, using a variable. – Eike Nov 28 '20 at 20:23
0

The problem is indeed the space and it can be solved like this:

 set(MY_DEBUG_WARNINGS "-Og;-Wall"
 )

replacing all spaces with a semi-colon. With this the compile command becomes:

 "/usr/bin/clang++ -I../src/Exception/include -g -Og -Wall 
 -fcolor-diagnostics -std=c++17 -o 
 src/Exception/CMakeFiles/exception.dir/Exception.cpp.o -c 
 /home/bob/app/src/Exception/Exception.cpp",

which actually works.

I have no idea whether this makes problems elsewhere, so a comment from someone with more knowledge would be much appreciated.

Eike
  • 359
  • 3
  • 8