Linker errors when building JUCE as static library

I’m building JUCE modules as a static library, by creating a project in the projucer with a CLion exporter and then adding Builds/CLion as a subdirectory in my parent CMakeLists.

I keep getting the linker error from JUCE_CORE:

this_will_fail_to_link_if_some_of_your_compile_units_are_built_in_debug_mode

However: I only get this linker error sometimes, and only after cleaning the project. I’m defining _DEBUG=1 and DEBUG=1 (as well as _NDEBUG and NDEBUG for release) for all my targets.

The only workaround that I’ve found works is to rewrite/rename my targets in the parent CMake file and build from scratch.

Is there a way to silence the error? I’m confident that my code is being compiled correctly, the linker is just freaking out. I don’t really understand how those checks are supposed to work, any ideas what I should be doing to debug?

Alright I’ve got a fix, but no understanding of why this is happening.

Surround any code that calls into juce with an #ifdef JUCE_MODULE_AVAILABLE_juce_core block, comment out #include "JuceHeader.h", clean the project, build, bring back the #include directive and build.

I can confirm this is happening only when a project is cleaned. I’m guessing something is strange with my CMake build.