【问题标题】:How do I set compile options for cmake msvc build in docker?如何在 docker 中为 cmake msvc build 设置编译选项?
【发布时间】:2019-07-03 20:05:10
【问题描述】:

我已经按照本指南设置了一个 Windows docker 容器来构建 msvc 项目:https://devblogs.microsoft.com/cppblog/using-msvc-in-a-docker-container-for-your-c-projects/

我正在使用 cmake 进行构建,但我无法设置编译器选项以便进行构建。我需要设置 /Z7(调试信息格式)才能在 docker 容器中构建,否则我会收到错误:

LINK : 致命错误 LNK1318: 意外的 PDB 错误

我知道这是问题所在,因为如果我在 CMakeCache.txt 文件中手动设置 /Z7,它就会构建。

我尝试过使用目标编译选项,add_compile_options 和 set (CMAKE_CXX_FLAGS_DEBUG ...)。

这是我目前的 CMakeLists.txt:

cmake_minimum_required(VERSION 2.8.9)
project (ConsoleApplication1)
add_executable(ConsoleApplication1 ConsoleApplication1.cpp)
target_compile_options(ConsoleApplication1 PRIVATE /Z7 /W4)

应用是通过VS2017创建的默认ConsoleApplication。

完整的错误信息: 完整的错误信息:

PS C:\consoleapplication1\build> cmake ..
-- Building for: Visual Studio 15 2017
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.16299.
-- The C compiler identification is MSVC 19.16.27030.1
-- The CXX compiler identification is MSVC 19.16.27030.1
-- Check for working C compiler: C:/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe
CMake Error: Remove failed on file: C:/consoleapplication1/build/CMakeFiles/CMakeTmp/Debug/cmTC_714d8.pdb: System Error: No such file or directory
-- Check for working C compiler: C:/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe -- broken
CMake Error at C:/BuildTools/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.12/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "C:/BuildTools/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/consoleapplication1/build/CMakeFiles/CMakeTmp

    Run Build Command:"C:/BuildTools/MSBuild/15.0/Bin/MSBuild.exe" "cmTC_714d8.vcxproj" "/p:Configuration=Debug" "/p:VisualStudioVersion=15.0"
    Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.

    Build started 7/4/2019 1:01:26 PM.
    Project "C:\consoleapplication1\build\CMakeFiles\CMakeTmp\cmTC_714d8.vcxproj" on node 1 (default targets).
    PrepareForBuild:
      Creating directory "cmTC_714d8.dir\Debug\".
      Creating directory "C:\consoleapplication1\build\CMakeFiles\CMakeTmp\Debug\".
      Creating directory "cmTC_714d8.dir\Debug\cmTC_714d8.tlog\".
    InitializeBuildStatus:
      Creating "cmTC_714d8.dir\Debug\cmTC_714d8.tlog\unsuccessfulbuild" because "AlwaysCreate" was specified.
    ClCompile:
      C:\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\CL.exe /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_714d8.dir\Debug\\" /Fd"cmTC_714d8.dir\Debug\vc141.pdb" /Gd /TC /analyze- /FC /errorReport:queue C:\consoleapplication1\build\CMakeFiles\CMakeTmp\testCCompiler.c
      Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27030.1 for x86
      Copyright (C) Microsoft Corporation.  All rights reserved.

      cl /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /Oy- /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_714d8.dir\Debug\\" /Fd"cmTC_714d8.dir\Debug\vc141.pdb" /Gd /TC /analyze- /FC /errorReport:queue C:\consoleapplication1\build\CMakeFiles\CMakeTmp\testCCompiler.c

      testCCompiler.c
    Link:
      C:\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\link.exe /ERRORREPORT:QUEUE /OUT:"C:\consoleapplication1\build\CMakeFiles\CMakeTmp\Debug\cmTC_714d8.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:/consoleapplication1/build/CMakeFiles/CMakeTmp/Debug/cmTC_714d8.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:/consoleapplication1/build/CMakeFiles/CMakeTmp/Debug/cmTC_714d8.lib" /MACHINE:X86 /SAFESEH  /machine:X86 cmTC_714d8.dir\Debug\testCCompiler.obj
    LINK : fatal error LNK1318: Unexpected PDB error; RPC (23) '(0x000006E7)' [C:\consoleapplication1\build\CMakeFiles\CMakeTmp\cmTC_714d8.vcxproj]
    Done Building Project "C:\consoleapplication1\build\CMakeFiles\CMakeTmp\cmTC_714d8.vcxproj" (default targets) -- FAILED.

    Build FAILED.

    "C:\consoleapplication1\build\CMakeFiles\CMakeTmp\cmTC_714d8.vcxproj" (default target) (1) ->
    (Link target) ->
      LINK : fatal error LNK1318: Unexpected PDB error; RPC (23) '(0x000006E7)' [C:\consoleapplication1\build\CMakeFiles\CMakeTmp\cmTC_714d8.vcxproj]

        0 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:01.55




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:3 (project)


-- Configuring incomplete, errors occurred!
See also "C:/consoleapplication1/build/CMakeFiles/CMakeOutput.log".
See also "C:/consoleapplication1/build/CMakeFiles/CMakeError.log".

有没有人能够完成这项工作:docker windows 容器、cmake、msvc?我很想看看你当时使用的 CMakeLists.txt。

编辑:添加 CMakeLists.txt 和完整的错误输出

【问题讨论】:

  • 通过编译器选项设置/Z7 似乎是一种正确的方法。可能您的代码有其他问题,但如果不查看此代码,我们将无法帮助您。请准备好minimal reproducible example
  • 用我当前的 CMakeLists.txt 更新了我的问题,我尝试编译的程序只是一个默认的控制台应用程序。
  • 请也显示确切的错误信息(它应该包括构建的目标、文件名和其他相关信息)。
  • 添加了完整的错误输出。如您所见,它在构建时使用 /Zi 而不是 /Z7。如果我在 CMakeCache.txt 文件中手动更改它,它会生成没有错误。
  • 我发现这个错误报告解释了我的问题:github.com/docker/for-win/issues/829 但我仍然不明白为什么我不能覆盖 cmake 使用的编译器设置。

标签: docker visual-c++ cmake


【解决方案1】:

正如 Tsyvarev 注意到的那样,问题是编译器检查而不是我的代码的编译,它与这里提到的在绑定挂载中构建有关:https://github.com/docker/for-win/issues/829#issuecomment-397149736

由于在项目文件中设置“调试信息格式”(链接中的解决方法 B)对我来说不是解决方案,我找到了一种解决方法来跳过编译器检查。这是适合我的 CMakeLists.txt。

cmake_minimum_required(VERSION 3.0)
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)
project (ConsoleApplication1)
add_executable(ConsoleApplication1 ConsoleApplication1.cpp)
target_compile_options(ConsoleApplication1 PRIVATE /Z7 /W4)

我不确定设置 COMPILER_FORCED 是否有副作用,但至少它允许我在 docker 容器中构建。

这里还有一个关于这个问题的错误报告给 MS:https://developercommunity.visualstudio.com/content/problem/607325/lnk1318-unexpected-pdb-error-when-building-from-a.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 2017-10-26
    • 2021-09-06
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多