我目前正在努力将我们的应用程序从 .sln/.vcxproj 移植到 CMake 和 Ninja。该项目构建良好,但是当尝试在调试模式下运行 .exe 时,它会抛出调试断言失败:程序 mfc140ud.dll -> appcore.cpp。
这对应于调用堆栈后面的
ASSERT(AfxGetThread() == NULL);
,这是由我们代码中的CWinApp g_oWinApp;
触发的。然而,这并没有改变,并且在 .sln/.vcxproj 中构建时运行良好,所以我希望在将编译指令从 .vcxproj 传输到 CMake 时我错过了一些东西。请参阅下面的每个内容的编辑副本。
cmake_minimum_required(VERSION 3.28.0)
project(ProjectName LANGUAGES CXX RC)
add_compile_definitions(-DUNICODE -D_UNICODE)
add_compile_definitions(-D_AFXDLL)
add_compile_definitions(-DWIN32)
add_compile_definitions(-D_CONSOLE)
set(CMAKE_MFC_FLAG 2)
find_package(cpprestsdk REQUIRED)
add_executable(ProjectName src/file1.cpp src/file2.cpp src/file3.rc)
target_precompile_headers(ProjectName PRIVATE src/stdafx.h)
target_compile_options(ProjectName PRIVATE /permissive)
target_include_directories(ProjectName PRIVATE ../headersToInclude)
target_link_libraries(ProjectName PRIVATE cpprestsdk::cpprest)
target_link_libraries(ProjectName PRIVATE Dbghelp)
target_link_libraries(ProjectName PRIVATE rpcrt4)
target_link_libraries(ProjectName PRIVATE Ws2_32)
target_link_libraries(ProjectName PRIVATE Netapi32)
target_link_libraries(ProjectName PRIVATE Pathcch)
target_link_libraries(ProejctName PRIVATE HomeBrewLibrary)
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug_Unicode|Win32'">
<ClCompile>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>../headersToInclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<PreprocessorDefinitions>_AFXDLL;WIN32;_CONSOLE;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MinimalRebuild>true</MinimalRebuild>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<StructMemberAlignment>1Byte</StructMemberAlignment>
<TreatWChar_tAsBuiltInType>true</TreatWChar_tAsBuiltInType>
<PrecompiledHeader>Use</PrecompiledHeader>
<BrowseInformation>true</BrowseInformation>
<WarningLevel>Level3</WarningLevel>
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
<CompileAs>CompileAsCpp</CompileAs>
<LanguageStandard>stdcpp14</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>Dbghelp.lib;rpcrt4.lib;Ws2_32.lib;Netapi32.lib;wldap32.lib;</AdditionalDependencies>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<SuppressStartupBanner>false</SuppressStartupBanner>
<GenerateDebugInformation>true</GenerateDebugInformation>
<ProgramDatabaseFile>$(OutDir)$(ProjectName).pdb</ProgramDatabaseFile>
<SubSystem>Console</SubSystem>
<TargetMachine>MachineX86</TargetMachine>
</Link>
<ResourceCompile>
<AdditionalIncludeDirectories>$(IntDir);../headersToIncludeinclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ResourceCompile>
</ItemDefinitionGroup>
任何建议将不胜感激,我迄今为止所做的调查暗示这是失败的,因为 AfxGetThread 返回一个预先存在的线程。然而,如何在 CMake 构建而不是 .Sln 构建中创建它让我感到困惑。
我尝试按照以下方式更改 WIN32 编译选项:简单显示窗口程序给出错误 VS2012 但是,我只是在将 WinMain 添加到 add_executable() 中时遇到问题,并且从 add_compile_definitions 中删除它时没有任何更改。
更新:我查看了返回的线程,并将其跟踪到我们也正在构建的 .dll。仔细检查发现缺少 _USRDLL 和 WINDOWS 编译定义。我添加了这些,这已经按预期工作了。