# CMAKE \<LINK\_LIBRARIES\> separated by spaces not commas in linker command

**URL:** https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003
**Category:** Usage
**Tags:** tool:cmake
**Created:** [September 18, 2023, 7:26pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003 "2023-09-18T19:26:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![cwilliamson](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@cwilliamson](https://discourse.cmake.org/u/cwilliamson)
#### Post date: [September 18, 2023, 7:26pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/1 "2023-09-18T19:26:36Z")

</div>

I’m trying to get CMake to use a custom compiler to build some code. I’ve setup a cmake toolchain file that works great for everything besides linking in static libraries. The linker requires all libraries to be passed using a -library=example.lib,example2.lib. Cmake seems to default to using spaces to separate libraries but this linker won’t take it. Here is my linker call example from my toolchain file:

```auto
set(CMAKE_C_LINK_EXECUTABLE "${LINKER} <OBJECTS> -library=<LINK_LIBRARIES> <LINK_FLAGS> -output=<TARGET>")

```

This produces everything except the librarys are passed like this:

```auto
-library= example1.lib example2.lib

```

Not this:

```auto
-library=example1.lib,example2.lib

```

How can I get CMake to separate the \<LINK\_LIBRARIES\> variable with commas not spaces? It even puts one between the -library= and the first lib.

---

<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: [September 19, 2023, 1:38pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/2 "2023-09-19T13:38:52Z")

</div>

I don’t recall seeing a variable that controls this. We may need to introduce a new toggle for it.

@brad.king

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [September 19, 2023, 1:49pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/3 "2023-09-19T13:49:45Z")

</div>

The `<LINK_LIBRARIES>` placeholder is meant to be replaced by a command-line fragment by the generators, so there is no way to control this currently.

Would `-library=example1.lib -library=example2.lib` be sufficient?

---

<div class="post-metadata">

### Author: ![cwilliamson](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@cwilliamson](https://discourse.cmake.org/u/cwilliamson)
#### Post date: [September 19, 2023, 3:43pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/4 "2023-09-19T15:43:37Z")

</div>

Hi Brad,

Yes that would work! I tried figuring it out but my lack of CMake knowledge stopped me. ‘  
Is there a method where that would be possible? I figured it was since a lot of libraries are prefaced with “-l” and I would just need to swap that out with “-library=“.

Thanks for the response!

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [September 19, 2023, 3:46pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/5 "2023-09-19T15:46:54Z")

</div>

In your custom compiler cmake code, try this:

```cmake
set(CMAKE_LINK_LIBRARY_FLAG "-library=")

```

---

<div class="post-metadata">

### Author: ![cwilliamson](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@cwilliamson](https://discourse.cmake.org/u/cwilliamson)
#### Post date: [September 19, 2023, 5:42pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/6 "2023-09-19T17:42:08Z")

</div>

Hi Brad,

I gave that a shot but nothing changed on my out put, is it something with the \<LINK\_LIBRARIES\> variable?  
My command is still:

```auto
set(CMAKE_STATIC_LIBRARY_SUFFIX_C .lib)
set(CMAKE_LINK_LIBRARY_FLAG "-library=")
set(CMAKE_C_LINK_EXECUTABLE "\"${CMAKE_CCRH_LINKER}\" <OBJECTS> <LINK_FLAGS> <LINK_LIBRARIES> -output=<TARGET>")

```

With the output of:

```auto
../modules/system/libsystem.lib ../modules/system/libblank_2.lib

```

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [September 19, 2023, 5:50pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/7 "2023-09-19T17:50:37Z")

</div>

CMake prefers to name libraries by the path to the file on disk. This avoids risk of the linker’s search path containing a conflict. If the `-library=` flag supports that, try:

```cmake
set(CMAKE_LINK_LIBRARY_FILE_FLAG "-library=")

```

---

<div class="post-metadata">

### Author: ![cwilliamson](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/67e7ee/32.png) [@cwilliamson](https://discourse.cmake.org/u/cwilliamson)
#### Post date: [September 19, 2023, 5:53pm UTC](https://discourse.cmake.org/t/cmake-link-libraries-separated-by-spaces-not-commas-in-linker-command/9003/8 "2023-09-19T17:53:49Z")

</div>

Yay! Worked like a charm! Ugh so many options for similar things, I’m sure it makes sense if you understand deeply how CMake works. Looks like I need to do some more research so I can better help myself in the future.  
Thank you so much for your help!
