"Cannot find source file" when library is linked to a particular executable

I am in the process of migrating an existing project to CMake. In it, I have several libraries and executables;

When I add one of the library targets (let’s say library lib) to the main executable (let’s call it exe), and “generate”, I currently get a strange error message I cannot find the exact reason for:

Configuring done (1.1s)
CMake Error in librarypath/CMakeLists.txt:
  Cannot find source file:
    C:/Source/library_autogen/S5YYZ2ZCNV_/qrc_library.cpp

Generating done (8.9s)

So, the executable linking to the library lib causes an error in the library, even when the library itself compiled fine before!

I added the main executable last (since it requires basically all other libraries in the project); all libraries build fine; I can create the executable target , but as soon as I add the target_link_library(exe PRIVATE lib) call, the CMake generation fails with above error.

I ask myself where the “wrong” search directory comes from (note the missing configuration after S5YYZ2ZCNV_ - in the `S5YYZ2ZCNV_Debug directory, the qrc_library.cpp file actually exists, created from a previous Debug build. And how/why can linking via target_link_library call even trigger a failed search for a source in a dependent library?

In lib, there’s Resources/library.qrc , AUTORCC is enabled, and as I said, the lib compiles fine, and it can also be linked to another executable in another folder.

I tried adding –log-level=DEBUG and –debug-output to the cmake command line but this didn’t give me any additional info on where this error is coming from.

I’m sure it’s just some stupid error on my end but I can’t figure out how to further debug this…

I forgot to say, both the library and the executable have AUTORCC set on; but as does the other executable where it works. When I remove the .qrc file from the linked library, the error disappears.

With the help of copilot I found out this is an AUTORCC issue with multi-config generators apparently.

It can be fixed by, instead of adding the .qrc to the sources directly (in the add_librarycall):

qt_add_resources(mylibrary_QRC resource.qrc)
target_sources(librarytarget PRIVATE ${mylibrary_QRC})

Then I don’t get this error… is this a known limitation / issue with AUTORCC? That it sometimes works and sometimes doesn’t?

While not a direct answer to your question, if you’re using Qt 6, consider the non-QRC form of qt_add_resources(). Instead of listing your resources in a .qrc file and passing that .qrc file to qt_add_resources(), add the individual resources directly. The syntax form is something like this:

qt_add_resources(target resourceName
    PREFIX prefix   # Optional with Qt 6.5 or later
    FILES files...
    [otherOptions...]
)

This is the recommended approach for Qt 6. It is more direct and easier to use. Under the covers, it generates a .qrc file internally, but you’re shielded from all those mechanics. It would be interesting to see if you encounter the same error if you added your resources this way instead of giving it your own .qrc file.

1 Like

Thanks for the hint. For now I’m happy with the workaround, but I will put this on my to do list to try in the near future :wink:.