0

I'm looking into switching my C++ building workflow from use of VisualStudio sln/vcxproj files and Makefiles to using CMake. Compilation is fine, but I'm running into issues when trying to link .res files for our Windows dialogs.

Our project is structured as you would expect a VS project to be structured: it has a solution with a core project, and it relies symbols from other projects. One of the 'libraries' I need for building on Windows is our base dialog .res file, which has been pre-compiled and is available in a directory, but when I try to link that file I get an error stating that it can't link "BaseResource.res.lib".

So of course I've done some Googling on this, and have spent more than a few hours looking for something. The closest I've found is this StackOverflow question on the subject, and I've tried its suggestion which apparently worked for the original ask-er, but it doesn't seem to be working on my end. Currently within my CMakeLists I have:

project(my_project CXX)

...

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    ENABLE_LANGUAGE(RC)
    file(TO_CMAKE_PATH "${RES_SOURCE_ROOT}/Source/Resources/Dialogs/Resource.rc" 
    BASE_RESOURCE_RC)
    add_executable( BaseResource ${BASE_RESOURCE_RC})
    target_link_libraries(BaseResource "BaseResource.res")
    set(BASE_RESOURCE_LINK_FLAGS 
    "${RES_SOURCE_ROOT}/Lib/${PLATFORM_DIR}/BaseResource.res")
    set_target_properties(BaseResource PROPERTIES LINK_FLAGS 
    ${BASE_RESOURCE_LINK_FLAGS})
    ...
else()
    # Do *Nix things.
endif()

Where BaseResource is the previously-undeclared project which is different from this one, and PLATFORM_DIR is the platform-based directory where we keep our binaries, e.g. "windows/v140/32bit". However, when I attempt this, loading the project tells me:

Cannot find source file:
  D:/Path/To/Resource/Root/Source/Resources/Dialogs/Resource.rc
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm
.hpp .hxx .in .txx

No SOURCES given to target: BaseResource

To be clear, this project should not care about the sources of other projects - it is specifically meant to compile just this project and to use the libraries from other projects to do so. The use of the above workaround was solely in the hope that it might allow me to use the resource.

I'm honestly at a loss; it's using the MSVC compiler so there really should be no issues in doing this linking, which goes off without a hitch using the VisualStudio solution. Any help on this subject would be hugely appreciated.

Thanks, ~Jon

EDIT: To clarify further, the original error is when I attempt to use:

link_libraries("${RES_SOURCE_ROOT}/Lib/${PLATFORM_DIR}/BaseResource.res")

The original error I get is:

LINK : fatal error LNK1104: cannot open file 'BaseResource.res.lib'

The error with the .rc file is coming from a workaround, in the event that it was the right direction but lacking some other things.

  • You formulate the question (and the title too!) as the problem with `.res` file, but the error message is clearly about `.rc` file, which is absent (or at least CMake cannot find it). – Tsyvarev Aug 29 '19 at 21:25
  • Hi Tsyvarev, the original issue is still regarding the .res file. The only reason we get an error message about the .rc file is because I experimented with the suggestion in the linked StackOverflow question. The original error is that when I try to link "${RES_SOURCE_ROOT}/Lib/${PLATFORM_DIR}/BaseResource.res", CMake complains that it cannot find "BaseResource.res.lib". – Jonathan McDevitt Aug 29 '19 at 22:48
  • I included both error messages because it's possible that I was on the right path with the attempted solution, but I'm missing something and someone knows where to go. If you read the second paragraph of the question above, you'll notice that I do mention the original error: 'One of the 'libraries' I need for building on Windows is our base dialog .res file, which has been pre-compiled and is available in a directory, but when I try to link that file I get an error stating that it can't link "BaseResource.res.lib"'. – Jonathan McDevitt Aug 29 '19 at 23:06
  • I will make the original issue more apparent; reading through it is definitely buried, but it is there. ~Jon – Jonathan McDevitt Aug 29 '19 at 23:09
  • [The answer to the referenced question](https://stackoverflow.com/a/18336367/3440745) tells to use `set_target_properties(LINK_FLAGS)` instead of `link_libraries`. Your first code snippet follows that approach... but it has an **unrelated error** (see my the first comment about meaning of that error message). So, what do you want from us? – Tsyvarev Aug 29 '19 at 23:22
  • @Tsyvarev My original question asks how to link a .res Windows dialog library to a binary via CMake. The referenced question was mentioned because I followed the directions in its answer and they didn't work, thus the solution does not seem to be valid in my case. Ergo, the question remains the same: since the referenced solution is not working, **how would I go about linking a pre-compiled .res file to a binary**? The referenced question was added simply to show that I had been "doing my homework", and not coming here without trying to solve it first. ~Jon – Jonathan McDevitt Sep 03 '19 at 21:03
  • I see **no evidence** in your question post that the [answer to the referenced question](https://stackoverflow.com/a/18336367/3440745) doesn't work. Note, that the referenced answer **does not** use `link_libraries`/`target_link_libraries` for `.res` file (the call to `target_link_libraries` exists, but `FOO_LIBS` parameter for this call doesn't include `.res` file). Instead, the referenced answer **does** add this file as a `LINK_FLAGS` property. – Tsyvarev Sep 03 '19 at 22:25

0 Answers0