# "FLAGS" literal is being output in link.txt, causing error

**URL:** https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263
**Category:** Code
**Tags:** os:linux, gen:makefiles
**Created:** [December 25, 2024, 11:15pm UTC](https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263 "2024-12-25T23:15:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DavidRV00](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davidrv00/32/5224_2.png) [@DavidRV00](https://discourse.cmake.org/u/DavidRV00)
#### Post date: [December 25, 2024, 11:15pm UTC](https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263/1 "2024-12-25T23:15:46Z")

</div>

Hello, I’m seeing an odd behavior when trying to build a Shared CUDA library using clang++ with CMake.

Here’s a simple reproducible example if you wanna try it out yourself: [GitHub - DavidRV00/cmake-clang-cuda-bug-repro: Simple reproducible example for a CMake + Clang++ + Cuda shared library bug](https://github.com/DavidRV00/cmake-clang-cuda-bug-repro)

I have a CMakeLists.txt like this:

```auto
cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
	set(CMAKE_CUDA_ARCHITECTURES 86)
endif()
PROJECT(bugrepro LANGUAGES C CXX CUDA)

add_library(mycudalib SHARED kernel.cu)

```

In kernel.cu there is just an empty function (pretty sure it doesn’t matter what’s in there).

I run cmake like this:

```auto
cmake \
    -DCUDAToolkit_ROOT=/opt/cuda \
    -DCMAKE_CUDA_COMPILER=clang++ \
    -S . -B ./build

```

And then I attemt to build with `make` with this command:

```auto
make -C ./build

```

But I get this error:

```auto
make: Entering directory '/home/.../build'
[50%] Linking CUDA shared library libmycudalib.so
clang++: error: no such file or directory: 'FLAGS'
make[2]: *** [CMakeFiles/mycudalib.dir/build.make:101: libmycudalib.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:87: CMakeFiles/mycudalib.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
make: Leaving directory '/home/.../build'

```

With some investigation, I see that this is caused by the literal string “FLAGS” being output into the clang command in ./build/CMakeFiles/mycudalib.dir/link.txt:

```auto
/usr/bin/clang++ FLAGS -fPIC -Xlinker --dependency-file=CMakeFiles/mycudalib.dir/link.d -shared -Wl,-soname,libmycudalib.so -o libmycudalib.so CMakeFiles/mycudalib.dir/kernel.cu.o -lcudadevrt -lcudart_static -lrt -lpthread -ldl -L"/opt/cuda/lib64"

```

And we can verify this is the problem by removing that little “FLAGS” string and retrying:

```auto
sed -i 's/FLAGS //g' ./build/CMakeFiles/mycudalib.dir/link.txt
make -C ./build # Success!

```

So, that works as a workaround, but I don’t believe it should be doing that.

Something else to note, this only happens when building as a Shared library. If I get rid of “SHARED” in the last line of the CMakeLists.txt, we avoid this error as well.

I’m using cmake version 3.31.3-dirty, artix linux.

Anybody have thoughts on this? Is this in need of a bug report?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 26, 2024, 5:52am UTC](https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263/2 "2024-12-26T05:52:42Z")

</div>

Reasonable chance this is related to [MR 9885](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9885). That change added the `<FLAGS>` placeholder to the Clang CUDA flags for executables and shared libraries.

---

<div class="post-metadata">

### Author: ![DavidRV00](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davidrv00/32/5224_2.png) [@DavidRV00](https://discourse.cmake.org/u/DavidRV00)
#### Post date: [December 29, 2024, 10:30pm UTC](https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263/3 "2024-12-29T22:30:58Z")

</div>

Thanks, that does seem reasonably likely. Although it doesn’t really look to me like there’s a problem in that commit itself; I’d bet it’s something to do with how that change interacts with the already-existing placeholder expansion logic.

I’ve spent a little time digging into the code, but I’m not familiar with the CMake codebase and I’m starting to a feel a little out of my depth.

I’ll submit a bug report.

---

<div class="post-metadata">

### Author: ![DavidRV00](https://discourse.cmake.org/user_avatar/discourse.cmake.org/davidrv00/32/5224_2.png) [@DavidRV00](https://discourse.cmake.org/u/DavidRV00)
#### Post date: [January 15, 2025, 11:40pm UTC](https://discourse.cmake.org/t/flags-literal-is-being-output-in-link-txt-causing-error/13263/4 "2025-01-15T23:40:51Z")

</div>

This bug has now been fixed:  
[https://gitlab.kitware.com/cmake/cmake/-/issues/26563](https://gitlab.kitware.com/cmake/cmake/-/issues/26563)
