我知道MFC很老了,但是VS写代码是真不习惯。
探索记录
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\ATLMFC\include\afx.h(24): fatal error C1189: #error: Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version. Please #define _AFXDLL or do not use /MD[d]
NMAKE : fatal error U1077: “C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\bin\Hostx86\x86\cl.exe”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
加宏定义
target_compile_definitions(MFCDemo PRIVATE
-DWIN32
-D_DEBUG
-D_WINDOWS
-D_VC80_UPGRADE=0x0600
-D_UNICODE
-DUNICODE
-D_AFXDLL
)
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\HW2.dir\objects1.rsp /out:HW2.exe /implib:HW2.lib /pdb:C:\msys64\home\pikachu\HW2\cmake-build-debug\HW2.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\HW2.dir/intermediate.manifest CMakeFiles\HW2.dir/manifest.res" failed (exit code 1120) with the following output:
msvcrtd.lib(exe_main.obj) : error LNK2019: 无法解析的外部符号 _main,该符号在函数 "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) 中被引用
HW2.exe : fatal error LNK1120: 1 个无法解析的外部命令
NMAKE : fatal error U1077: “C:\Users\pikachu\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\202.6948.80\bin\cmake\win\bin\cmake.exe”: 返回代码“0xffffffff”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
加了
set(CMAKE_MFC_FLAG 2)
set_target_properties(MFCDemo PROPERTIES LINK_FLAGS "/SUBSYSTEM:WINDOWS")
之后是
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2017\COMMUN~1\VC\Tools\MSVC\1416~1.270\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\MFCDemo.dir\objects1.rsp /out:MFCDemo.exe /implib:MFCDemo.lib /pdb:C:\msys64\home\pikachu\HW2\cmake-build-debug\MFCDemo.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console /SUBSYSTEM:WINDOWS kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\MFCDemo.dir/intermediate.manifest CMakeFiles\MFCDemo.dir/manifest.res" failed (exit code 1120) with the following output:
msvcrtd.lib(exe_winmain.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ) 中被引用
MFCDemo.exe : fatal error LNK1120: 1 个无法解析的外部命令
NMAKE : fatal error U1077: “C:\Users\pikachu\AppData\Local\JetBrains\Toolbox\apps\CLion\ch-0\202.6948.80\bin\cmake\win\bin\cmake.exe”: 返回代码“0xffffffff”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
NMAKE : fatal error U1077: “"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"”: 返回代码“0x2”
Stop.
这个的解决是
target_link_options(MFCDemo PRIVATE
/ENTRY:wWinMainCRTStartup
)
经过测试,还要配合
target_compile_definitions(MFCDemo PRIVATE
-DWIN32
-D_DEBUG
-D_WINDOWS
-D_UNICODE
-DUNICODE
-D_AFXDLL
)
总结分享
总结一下,先贴配置精简版
set(CMAKE_C_STANDARD 90)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
set(CMAKE_MFC_FLAG 1)
find_package(MFC)
if(NOT MFC_FOUND)
message(FATAL_ERROR "MFC is not found, please check MFC installation")
endif()
add_executable(MFCApplication1 WIN32 )
target_sources(MFCApplication1 PRIVATE
xxx.cpp
)
target_compile_definitions(MFCApplication1 PRIVATE
-DWIN32
-D_DEBUG
-D_WINDOWS
-D_UNICODE
-DUNICODE
-D_AFXDLL
)
target_link_options(MFCApplication1 PRIVATE
/ENTRY:wWinMainCRTStartup
)
首先是
set(CMAKE_MFC_FLAG 1)
find_package(MFC)
可以参考:
https://cmake.org/cmake/help/v3.18/variable/CMAKE_MFC_FLAG.html
https://cmake.org/cmake/help/v3.18/module/FindMFC.html
然后是
add_executable(MFCApplication1 WIN32 )
这里的加不加WIN32
会影响到
/subsystem:console
还是
/subsystem:windows
接下来是一堆宏定义和入口函数
宏定义最开始是从VS里抄的
后来的测试发现这些定义跟main函数还是有关系的,至于具体是啥,我就没去弄了。
/ENTRY:wWinMainCRTStartup
这个就是解决找不到winmain
的问题,这里我不知道具体原因,最后这么改才能正常运行
参考资料
- cmake相关
https://cmake.org/cmake/help/v3.18/module/FindMFC.html
https://cmake.org/cmake/help/v3.18/variable/CMAKE_MFC_FLAG.html - 函数入口
https://stackoverflow.com/questions/45724766/how-to-set-c-application-entry-point-to-main-on-windows-using-cmake
https://ywjheart.wordpress.com/2016/03/02/using-cmake-part-ii-build-mfc-applications/
https://bbs.csdn.net/topics/80054096 - /subsystem:“console”
https://www.cnblogs.com/ydxt/p/3852790.html - 最开始搜索到的
https://blog.csdn.net/liu_wan001/article/details/83011553