How to use Resource Files (.rc) in CMake?

I have added a resource file to my project like so:

target_sources(foobar PRIVATE foobar.rc)

I’m able to successfully compile my project with Visual Studio. However, my Ninja build fails.

What’s the error?

I figured out the cause of my issue.

For future CMake users the following code should be enough.

target_sources(foobar PRIVATE foobar.rc)

However, I was providing a custom toolchain. Which was the cause of my problem.

And here is the problem.

On visual studio here is how you set the system include directories:

set(CMAKE_VS_SDK_INCLUDE_DIRECTORIES ${CUSTOM_INCLUDE_DIRECTORIES})

On Ninja (IE everything else) you have to do this:

        # Include Directories
        foreach(LANG  C CXX)
            set(CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES ${CUSTOM_INCLUDE_DIRECTORIES})
        endforeach()

Do you see the bug? It took me having to manually run the RC commands to find it.
Because Ninja was hiding/obscuring the RC fatal error message.

fatal error RC1015: cannot open include file 'winres.h'

The problem was CMAKE_RC_STANDARD_INCLUDE_DIRECTORIES was set to nothing!
Here is the fix:

        # Include Directories, NOTE THE RC LANGUAGE IS IMPORTANT!
        foreach(LANG  C CXX RC)
            set(CMAKE_${LANG}_STANDARD_INCLUDE_DIRECTORIES ${CUSTOM_INCLUDE_DIRECTORIES})
        endforeach()

This is why my build was working on Visual Studio but not Ninja.