8

I'm trying to link a compiled .res file with cmake but I can't seem to find out much info on how to do it.

The closest I have got is

SET(RESOURCE_FILE resource.res)

file(GLOB src_files 
"src/*.h"
"src/*.cpp"
"${RESOURCE_FILE}"
)

add_executable(exename  ${src_files})

and then manually linking the .res file thru the IDE (i.e. in visual studio dropping the .res file in the Linker additional dependencies). This means I have to reset the additional dependency every time I change the cmake file. Surely there is a better way than this

Forgive my inexperience with cmake, any help would be appreciated.

Biggy Smith
  • 910
  • 7
  • 14

2 Answers2

6

Default lib extension is a bit sticky one, but you can do as follows:

# ...
ADD_EXECUTABLE( FOO ${FOO_SRCS} )
TARGET_LINK_LIBRARIES( FOO ${FOO_LIBS} )
SET( FOO_LINKFLAGS ${CMAKE_CURRENT_SOURCE_DIR}/foo.res )
SET_TARGET_PROPERTIES( FOO PROPERTIES LINK_FLAGS ${FOO_LINKFLAGS} )

which would show up in MSVC as additional [linker] options (instead of dependencies). Hope that helps.

If there are other LINK_FLAGS defined, you may need to first store them in the FOO_LINKFLAGS and then append new ones.

Vaaksiainen
  • 228
  • 1
  • 6
3

In more modern CMake, this became quite simple: Just add your resource file to the list of source files.

target_sources(mybinary PRIVATE "src/main.cpp" "resources/mybinary.rc")

For my CMake project, using Visual Studio 2022, compiling the resources file with rc and linking it works like a charm.

marsl
  • 959
  • 1
  • 14
  • 25