# I need to enforce link order such that a library always appears first in the link command line.

**URL:** <https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045>\
**Category:** Code\
**Created:** [May 3, 2023, 11:55pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045 "2023-05-03T23:55:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Goodrich](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alexander_goodrich/32/3409_2.png) [@Alexander\_Goodrich](https://discourse.cmake.org/u/Alexander_Goodrich)\
**Post date:** [May 3, 2023, 11:55pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/1 "2023-05-03T23:55:03Z")

</div>

Mimalloc on Windows has a clever mechanism for overriding malloc calls via an interception mechanic, but in order for this to work, two libraries must be included first in the link order: mimalloc-redirect.lib and mimalloc-override.lib.

I’ve tried manually massaging LINK\_LIBRARIES to force these two to be in the front, and I’ve also tried target\_link\_libraries(MyProj PRIVATE mimalloc Foo Bar) but in the resulting command line, Foo and Bar come first.

Any ideas? Is it simply a matter of forcing Foo to have a dependency on mimalloc, which I’ve pre-built and included as an interface library target.

---

<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:** [May 4, 2023, 2:39am UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/2 "2023-05-04T02:39:00Z")

</div>

If you need things to be first, that sounds like they should be object libraries rather than static libraries. That way, whatever symbols those objects provide will take precedence over anything from any libraries that might be linked in, including any libraries that the toolchain automatically adds (these don’t usually appear on the command line, but are added silently by the toolchain). Do you have the ability to compile the mimalloc libraries as object libraries instead?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [May 4, 2023, 7:46am UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/3 "2023-05-04T07:46:53Z")

</div>

Maybe defining an INTERFACE library for the mimalloc which links with all your application libraries (i.e. mimalloc depends on **all** libraries) will solve your problem:

```cmake
add_library(mimalloc INTERFACE)
# specify mimalloc as first link libraries
target_link_libraries(mimalloc INTERFACE /path/to/mimalloc-redirect.lib /path/to/mimalloc-override.lib)
# add after all the user's libraries
target_link_libraries(mimalloc INTERFACE Foo Bar ...)

# and your application links only with mimalloc
target_link_libraries(MyProj PRIVATE mimalloc)

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Goodrich](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alexander_goodrich/32/3409_2.png) [@Alexander\_Goodrich](https://discourse.cmake.org/u/Alexander_Goodrich)\
**Post date:** [May 4, 2023, 7:36pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/4 "2023-05-04T19:36:17Z")

</div>

Unfortunately, I don’t think I can - the mimalloc-redirect.lib is prebuilt and packaged with the mimalloc distribution, there’s no way I can build it inline as part of the overall project, which is what I think you have to do with an object library.

---

<div class="post-metadata">

**Author:** ![Alexander\_Goodrich](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alexander_goodrich/32/3409_2.png) [@Alexander\_Goodrich](https://discourse.cmake.org/u/Alexander_Goodrich)\
**Post date:** [May 4, 2023, 7:37pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/5 "2023-05-04T19:37:45Z")

</div>

This is interesting but it would require significant rework of our overall project structure because we include third party libraries via add\_subdirectory() before Foo and Bar get added as library targets. I can certainly try this as an experiment to see if it would work.

---

<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:** [May 5, 2023, 12:27pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/6 "2023-05-05T12:27:17Z")

</div>

The fact that it is for all libraries makes me feel like this belongs as part of the toolchain configuration or project-wide flags (though I think these come after target-specific flags).

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [May 9, 2025, 12:32pm UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/7 "2025-05-09T12:32:35Z")

</div>

Hi, I’m having the exact same issue. One thing I’ve seen is that in some cases simply adding mimalloc to `target_link_libraries` is enough for CMake to link it before any other library, including system libs. However, this doesn’t seem to happen in all cases. @ben.boeckel is `target_link_libraries` guaranteed to honor the link order passed to it, so that the libs will appear even before any system libs?

---

<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:** [May 12, 2025, 5:23am UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/8 "2025-05-12T05:23:04Z")

</div>

I think [this issue](https://gitlab.kitware.com/cmake/cmake/-/issues/26271) is of interest for history. The results of it are the [`LINK_LIBRARIES_STRATEGY`](https://cmake.org/cmake/help/latest/prop_tgt/LINK_LIBRARIES_STRATEGY.html) property and policy [`CMP0179`](https://cmake.org/cmake/help/latest/policy/CMP0179.html) (which depends on [`CMP0156`](https://cmake.org/cmake/help/latest/policy/CMP0156.html)).

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [May 12, 2025, 7:36am UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/9 "2025-05-12T07:36:35Z")

</div>

Thanks @ben.boeckel , I’ll take a look.

---

<div class="post-metadata">

**Author:** ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)\
**Post date:** [May 12, 2025, 10:24am UTC](https://discourse.cmake.org/t/i-need-to-enforce-link-order-such-that-a-library-always-appears-first-in-the-link-command-line/8045/10 "2025-05-12T10:24:14Z")

</div>

So I’ve looked at this further. I’m using CMake 3.29 on Windows, which is correctly generating linker invocations with mimalloc first (as specified in `target_link_libraries`); however in my case I had an .exe which had mimalloc first in `target_link_libraries` but itself didn’t include any mimalloc symbols. However, the .exe was also linked against a static library which did include mimalloc symbols. The result was that even though CMake was generating the correct linker invocation, the linker itself ended up placing mimalloc at the end of the dependency list (as reported by `dumpbin /DEPENDENTS`).

The solution for me was to force-include a mimalloc symbol on the .exe itself (as stated in [mi-malloc: Overriding Malloc](https://microsoft.github.io/mimalloc/overrides.html)). Doing that seems to have fixed the issue, and now mimalloc appears first.
