我有一个像这样的 C++ 项目结构:
mfc_ninja
├── CMakeLists.txt
├── source.cpp
CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(mylib)
add_definitions(-D_AFXDLL)
set(CMAKE_MFC_FLAG 2)
add_library(${PROJECT_NAME} SHARED source.cpp)
source.cpp
#include <afxwin.h>
我用 Visual Studio 2019 打开目录
mfc_ninja
。然后我转到菜单 Project 和 Generate cache,它按预期工作。当我尝试构建项目时,出现错误:
Error C1083 Cannot open include file: 'afxwin.h': No such file or directory
我没有使用 MFC 的经验。我想将现有的 Visual Studio 解决方案移植到纯 CMake。对于可执行文件,我发现你必须用
add_executable
定义 WIN32
。但是对于add_library
,这不存在。
如何使用纯 CMake 针对 MFC 构建库?
我更新了
CMakeLists.txt
以使用FindMFC
,如评论中所述:
cmake_minimum_required(VERSION 3.19)
project(mylib)
find_package(MFC REQUIRED)
message(STATUS "MFC_FOUND: ${MFC_FOUND}")
add_library(${PROJECT_NAME} SHARED source.cpp)
输出告诉我它没有找到 MFC:
1> CMake generation started for default configuration: 'x64-Debug'.
1> Command line: "cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\benjamin.buch\source\repos\mfc_ninja\out\install\x64-Debug" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\PROFESSIONAL\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "C:\Users\benjamin.buch\source\repos\mfc_ninja" 2>&1"
1> Working directory: C:\Users\benjamin.buch\source\repos\mfc_ninja\out\build\x64-Debug
1> [CMake] -- The C compiler identification is MSVC 19.28.29915.0
1> [CMake] -- The CXX compiler identification is MSVC 19.28.29915.0
1> [CMake] -- Detecting C compiler ABI info
1> [CMake] -- Detecting C compiler ABI info - done
1> [CMake] -- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting C compile features
1> [CMake] -- Detecting C compile features - done
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- Looking for MFC
1> [CMake] -- Looking for MFC - not found
1> [CMake] -- MFC_FOUND: NO
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Users/benjamin.buch/source/repos/mfc_ninja/out/build/x64-Debug
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> Extracted includes paths.
1> CMake generation finished.
让我有点困惑的是,尽管
REQUIRED
这不会因错误消息而中止。
看起来 Visual Studio 解决方案正在使用某种嵌入式 MFC。要将 MFC 与 CMake 一起使用,必须通过 Visual Studio 安装程序进行安装。
不幸的是,我只有安装程序的德语截图:
因为
REQUIRED
不能与 FindMFC
一起使用,所以必须明确检查。
cmake_minimum_required(VERSION 3.19)
project(mylib)
find_package(MFC)
if(NOT MFC_FOUND)
message(FATAL_ERROR "MFC was not found, please install ASP.NET/MFC for newest build tools")
endif()
add_library(${PROJECT_NAME} SHARED source.cpp)