# How not to have dollar sign in target\_link\_options mangled?

**URL:** https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939
**Category:** Code
**Created:** [August 16, 2021, 8:27am UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939 "2021-08-16T08:27:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mmarczell-graphisoft](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmarczell-graphisoft/32/1714_2.png) [@mmarczell-graphisoft](https://discourse.cmake.org/u/mmarczell-graphisoft)
#### Post date: [August 16, 2021, 8:27am UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/1 "2021-08-16T08:27:40Z")

</div>

If I have this CMake script:

```auto
add_link_options(--preload-file=temp$.txt)
add_executable(main main.cpp)

```

or

```auto
add_executable(main main.cpp)
target_link_options(main PUBLIC --preload-file=temp$.txt)

```

and an arbitrary file named `temp$.txt`, it doesn’t find the txt file because the dollar sign gets changed into something like `\$$` in the actual compiler invocation.

(Don’t mind the actual link option, it’s an Emscripten specific one, but the problem I believe is not specific to Emscripten development.)

What should I do to make it find my file which has a dollar sign in its name? This doesn’t help:

```auto
target_link_options(main PUBLIC [=[--preload-file=temp$.txt]=])
```

---

<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: [August 16, 2021, 3:41pm UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/2 "2021-08-16T15:41:10Z")

</div>

Do you really _need_ to have `$` in the name? Is there no other character that can work? CMake doesn’t guarantee that arbitrary text can make it through untouched (e.g., a filename with `;` in it is going to have a horrible time). I think `$` may just be in the same bucket as I believe it is important to all non-Xcode generators in some way.

---

<div class="post-metadata">

### Author: ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)
#### Post date: [August 16, 2021, 8:14pm UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/3 "2021-08-16T20:14:50Z")

</div>

I observe that using `CMAKE_EXE_LINKER_FLAGS` is not affected by the same escaping behavior. Consider:

```cmake
cmake_minimum_required(VERSION 3.21)
project(test)

set(CMAKE_EXE_LINKER_FLAGS "--preload-file=temp$.txt")

add_executable(main main.cpp) # main.cpp is empty for demo

```

Then at the terminal, we can see:

```auto
$ cmake -G Ninja -S . -B build -DCMAKE_BUILD_TYPE=Release
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /path/to/build
$ cmake --build build/ -- -nv
[1/2] /usr/bin/c++ -O3 -DNDEBUG -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test2/main.cpp
[2/2] : && /usr/bin/c++ -O3 -DNDEBUG --preload-file=temp$.txt CMakeFiles/main.dir/main.cpp.o -o main && :

```

This also works with `Unix Makefiles`, though it is obscured in the command output.

> [@ben.boeckel](#):
>
> CMake doesn’t guarantee that arbitrary text can make it through untouched (e.g., a filename with `;` in it is going to have a horrible time).

Escaping with `\$$` looks like a bug, though. Depending on the backend, I would expect to see _either_ `\$` _or_ `$$`, not the two laid on top of each other. Also, shouldn’t `CMAKE_*_LINKER_FLAGS*` escape the same way as `target_link_options`, `add_link_options`, etc.?

---

<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: [August 16, 2021, 8:33pm UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/4 "2021-08-16T20:33:58Z")

</div>

Hmm. Good point. Unfortunately, fixing any of this is going to be a policy. The `CMAKE_<LANG>_LINKER_FLAGS` are “old” and probably don’t have the same codepaths as the usage requirement-using `target_link_*` bits.

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [August 16, 2021, 11:56pm UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/5 "2021-08-16T23:56:00Z")

</div>

A `\$$` looks perfectly fine inside a Makefile or Ninja using a shell. The `\` quotes the dollar sign for the shell command line and the `$$` is a single dollar sign in Make/Ninja syntax.

But the Makefile generated by CMake does not call the linker directly. Instead it generates a `link.txt` file which is given to `cmake -E cmake_link_script`. In that case doing a `$$` quoting is a bug, the working string is `\$`. Most probably introduced with the `cmake_link_script` feature.

And a Ninja file generated by CMake file contains `\$$$$` which is also a bug. The working string is `\$$`.

---

<div class="post-metadata">

### Author: ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)
#### Post date: [August 17, 2021, 9:35pm UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/6 "2021-08-17T21:35:06Z")

</div>

Thanks for the context, there. I figured (incorrectly) that Ninja was calling the commands directly and not going through the shell.

---

<div class="post-metadata">

### Author: ![mmarczell-graphisoft](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mmarczell-graphisoft/32/1714_2.png) [@mmarczell-graphisoft](https://discourse.cmake.org/u/mmarczell-graphisoft)
#### Post date: [August 19, 2021, 10:19am UTC](https://discourse.cmake.org/t/how-not-to-have-dollar-sign-in-target-link-options-mangled/3939/7 "2021-08-19T10:19:22Z")

</div>

Are you a CMake dev? Do I need file a bug report? Or is one of these related?  
[https://gitlab.kitware.com/cmake/cmake/-/issues/21647](https://gitlab.kitware.com/cmake/cmake/-/issues/21647)  
[https://gitlab.kitware.com/cmake/cmake/-/issues/16395](https://gitlab.kitware.com/cmake/cmake/-/issues/16395)
