CMake MFC project in Visual Studio missing resource editor
Asked Answered
D

1

8

I'm having a hard time opening the interactive resource editor in a MFC CMake project that otherwise compiles and runs just fine.

The project files are laid out as follows:

.
├── CMakeLists.txt
├── inc
│   ├── <...>
│   ├── resource.h
├── res
│   ├── MyApp.ico
│   ├── MyApp.rc
│   └── MyApp.rc2
└── src
    ├── CMakeLists.txt
    └── <...>

Top CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(MyProject)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 14)
if (MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /we4715") # makes missing return as error
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")     # parallel build
endif()
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/output)

# MFC
add_definitions(-D_AFXDLL)
set(CMAKE_MFC_FLAG 1)

add_subdirectory(src)

src CMakeLists.txt:

include_directories(${CMAKE_SOURCE_DIR}/inc)
include_directories(${CMAKE_SOURCE_DIR}/res)
include_directories(${CMAKE_SOURCE_DIR})

set(SOURCES
        <...>
        )

set(HEADERS
        <...>
        )

add_executable(
    ${PROJECT_NAME}
    WIN32 
    ${SOURCES}
    ${HEADERS}
    ${CMAKE_SOURCE_DIR}/res/MyApp.rc
    ${CMAKE_SOURCE_DIR}/res/MyApp.rc2
    )

install(TARGETS MyApp
        RUNTIME DESTINATION .)

In Visual Studio (Pro) the project loads just fine and compiles for every configuration I need, but if I try to open an .rc file I get errors on missing headers from MFC and ATL (afxres.h, winres.h, dde.rh, ...). The resource file won't open in the resource editor like it does on a VS solution.

The mssage happens when double-clicking on MyApp.rc, and I get

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

I use the latest VS version available as of today which is 15.8.5, and this happens when opening the CMake project in Visual Studio.

I have already installed the ATL/MFC components in my Visual Studio installation. Like I said, it compiles just fine so dependencies are resolved correctly.

Am I missing something? Is this even possible?

Dysphasia answered 27/9, 2018 at 13:0 Comment(0)
C
0

I had the same problem. It seems to be a known limitation of the built-in CMake support in Visual Studio: https://developercommunity.visualstudio.com/t/cmake-project-cant-open-rc-file/172331

The workaround is to use CMake from the command line to generate a Visual Studio solution and then open that instead:

cmake -G "Visual Studio 16" -B build
start build/*.sln

Replace "Visual Studio 16" with whatever version you're using.

Cosma answered 2/2 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.