Build fails with linker errors on MSVC when setting `OUTPUT_NAME` property of executable to the name of a Qt module
Problem Description
I have a project that defines a static Qt module and an executable. Qt modules expect to reside in a folder that matches the module name, therefore I'm reserving ${PROJECT_NAME}
for the Qt module. The executable target gets an alternative target name, in my case ${PROJECT_NAME}Exe
. However I want the final executable to be named ${PROJECT_NAME}
again. Here is an excerpt from CMakeLists.txt:
qt_add_library(${PROJECT_NAME} STATIC)
qt_add_qml_module(${PROJECT_NAME}
URI ${PROJECT_NAME}
[...]
)
qt_add_executable(${PROJECT_NAME}Exe ${OS_BUNDLE}
src/Main.cc)
set_target_properties(${PROJECT_NAME}Exe PROPERTIES
OUTPUT_NAME "${PROJECT_NAME}")
This works without problems on Linux, but behaves very strangely on MSVC Windows. The first time I build the project from scratch, I get a bunch of undefined symbols from the linker. It seems as if the executable would not be linked to the module at first. However, on the second and subsequent builds, the executable links and works fine! This is reproducible, only full rebuilds (from scratch) incur this problem, or at least I have not found a rebuild that triggers the problem yet.
Workarounds and Discussion
If I move the sources and CMakeLists.txt
for the main executable to a subfolder (like main
), the problem goes away, and everything works well on Linux and Windows with the OUTPUT_NAME
set.
In my humble opinion, it looks as if there would be some object files shared between the Qt module and the executable with the OUTPUT_NAME "${PROJECT_NAME}"
property set, so that the linker is confused on the first build iteration?
Am I trying something illegal in cmake, or is there a bug here?
System Information
I'm using ninja
for the build orchestration, cmake v3.29.2 build from sources, and a very recent Visual Studio, with ClangCl 16.0.6 as the compiler frontend.