# target link quoted options with variable

**URL:** <https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930>\
**Category:** Code\
**Tags:** os:windows, gen:ninja\
**Created:** [March 10, 2021, 2:06pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930 "2021-03-10T14:06:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 2:06pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/1 "2021-03-10T14:06:45Z")

</div>

Hi,  
I need to pass some unorthodox path arguments to the linker, like:

```auto
-j"C:\some path\to directory\parser";"C:\some path\to directory\block"

```

Now my CMakeLists.txt looks like:

```auto
...
set(PATH_1 ${PROJECT_SOURCE_DIR}/parser)
set(PATH_2 ${PROJECT_SOURCE_DIR}/block)
string(REPLACE "/" "\\" PATH_1 ${PATH_1})
string(REPLACE "/" "\\" PATH_2 ${PATH_2})
set(paths
    \"${PATH_1}\"
    \"${PATH_2}\")

string(REPLACE ";" "\\;" paths "${paths}")
message("-j${paths}")
target_link_options(${target} PRIVATE
    "-j${paths}")
...

```

and generated build.ninja contain next line:

```auto
LINK_FLAGS = ... -j\"C:\some path\to directory\parser\";\"C:\some path\to directory\block\"

```

but I would like to see the following result:

```auto
LINK_FLAGS = ... -j"C:\some path\to directory\parser";"C:\some path\to directory\block"

```

Why `"\;"` works, but `"\""` not works?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 10, 2021, 2:27pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/2 "2021-03-10T14:27:18Z")

</div>

`\;` is an oddball artifact of the way CMake’s list representation was “designed”. It passes through rather than having a `\` stripped off. Usually I just play around with escaping `;` more and more until I get the result I want.

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 2:40pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/3 "2021-03-10T14:40:17Z")

</div>

I’ve been playing with escape sequences for about 3 days now and went through a bunch of options. As a result, I cannot get the desired result. Also found these sources in which a similar problem was never properly resolved: [1](http://cmake.3232098.n2.nabble.com/Double-quotes-being-removed-Windows-removing-compiler-flags-td7580824.html), [2](https://stackoverflow.com/questions/63578867/why-does-the-cmake-build-not-set-the-path-in-quotes)

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 2:40pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/4 "2021-03-10T14:40:58Z")

</div>

Also found a similar problem, but the solution doesn’t work for me: [3](https://discourse.cmake.org/t/string-escaping-with-target-compile-options/2117)

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 10, 2021, 2:42pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/5 "2021-03-10T14:42:25Z")

</div>

Hmm. Does using `LINKER:` or `SHELL:` help at all? See the bottom of [the `target_link_options` docs](https://cmake.org/cmake/help/latest/command/target_link_options.html).

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 2:43pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/6 "2021-03-10T14:43:36Z")

</div>

Didn’t help, already tried

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 4:56pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/7 "2021-03-10T16:56:25Z")

</div>

I tried:  
makerules.cmake

```auto
set(CMAKE_CXX_LINKER_WRAPPER_FLAG "-j")
set(CMAKE_CXX_LINKER_WRAPPER_FLAG_SEP ";")

```

CMakeLists.txt

```auto
set(PATH_1 ${PROJECT_SOURCE_DIR}/parser)
set(PATH_2 ${PROJECT_SOURCE_DIR}/block)
string(REPLACE "/" "\\" PATH_1 ${PATH_1})
string(REPLACE "/" "\\" PATH_2 ${PATH_2})
set(paths
    \"${PATH_1}\"
    \"${PATH_2}\")
target_link_options(${target} PRIVATE
    "LINKER:${paths}")

```

output in build.ninja:

```auto
LINK_FLAGS = -j\"C:\some path\to directory\parser\" \"C:\some path\to directory\block\"

```

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 10, 2021, 5:00pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/8 "2021-03-10T17:00:05Z")

</div>

> [@kmosk](#):
>
> `set(CMAKE_CXX_LINKER_WRAPPER_FLAG "-j") # not work?`

This variable is a toolchain-level thing and applies to the entire C++ toolchain. As such, it is only used from the top-level scope and it applies to all such linker flags. The separator variable is the same way.

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 5:05pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/10 "2021-03-10T17:05:25Z")

</div>

sorry, update previous message\*

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 5:10pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/11 "2021-03-10T17:10:26Z")

</div>

last line in toolchain file:

```auto
...
set(CMAKE_USER_MAKE_RULES_OVERRIDE ${CMAKE_CURRENT_LIST_DIR}/makerules.cmake)

```

---

<div class="post-metadata">

**Author:** ![kmosk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kmosk/32/1279_2.png) [@kmosk](https://discourse.cmake.org/u/kmosk)\
**Post date:** [March 10, 2021, 8:23pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/12 "2021-03-10T20:23:24Z")

</div>

I tried to run a build from Linux using Unix Makefiles:  
link.txt:

```auto
... "-j\"C:\\some path\\to directory\\parser\";\"C:\\some path\\to directory\\block\"" ...

```

I noticed that when using Ninja, the handling of the `"\\"` escape sequence was.  
Probably the way to solve my problem lies at the stage of passing the string containing the linker flags to the Ninja generator.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [March 10, 2021, 10:02pm UTC](https://discourse.cmake.org/t/target-link-quoted-options-with-variable/2930/13 "2021-03-10T22:02:17Z")

</div>

Hmm. Please file [an issue](https://gitlab.kitware.com/cmake/cmake/-/issues) with the expected command line as you would run it in a shell and how you’ve tried specifying it in CMake code to pass through. The results of each (especially those that are close) would be helpful. If it is per-generator behavior, differences there would also be helpful.

Sorry, quoting in CMake is problematic due to the stringly-typed language, but we try to work around the problem in various ways (such as `LINKER:` or `SHELL:`). A `VERBATIM:` might be warranted.
