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

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.

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?

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:

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)

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.

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.

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).