【问题标题】:Qt5, cmake and MinGWQt5、cmake 和 MinGW
【发布时间】:2013-09-10 13:50:27
【问题描述】:

我正在尝试在 MinGW 下使用 Qt 5 和 cmake 构建一个“Hello World”项目。
这是 CMakeLists.txt 文件(取自在线文档):

project(Qt5_cmake_test)

cmake_minimum_required(VERSION 2.8.11)

set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)

# Find the QtWidgets library
find_package(Qt5Widgets)

# Add the source files from the current directory
aux_source_directory(. SRC_LIST)

# Tell CMake to create the executable
add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})

# Use the Widgets module from Qt5
target_link_libraries(${PROJECT_NAME} Qt5::Widgets)

源代码是在创建新项目时自动生成的(生成一个空窗口)。

从 Windows 命令提示符配置:cmake -G "MinGW Makefiles" ..\Qt5_cmake_test

我收到以下错误:

CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeMinGWFindMake.cmake:20 (message):
  sh.exe was found in your PATH, here:

  C:/Program Files (x86)/Git/bin/sh.exe

  For MinGW make to work correctly sh.exe must NOT be in your path.

  Run cmake from a shell that does not have sh.exe in your PATH.

  If you want to use a UNIX shell, then use MSYS Makefiles.

Call Stack (most recent call first):
  CMakeLists.txt:8 (project)


CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_C_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCCompiler.cmake

CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER_ENV_VAR
CMake Error: Error required internal CMake variable not set, cmake may be not be built correctly.
Missing variable is:
CMAKE_CXX_COMPILER
CMake Error: Could not find cmake module file:C:/Users/pietro.mele/projects/tests/buildSystem_test/Qt5_cmake_test-build/CMakeFiles/2.8.11.2/CMakeCXXCompiler.cma
ke
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

所以它似乎无法找到编译器。有没有办法让 cmake 自己找到它,或者只是给它 CMAKE_PREFIX_PATH 目录?
我是否必须在 makefile 中手动指定所有这些变量或在 Windows 中指定为环境变量?

我尝试了标准 Windows 命令提示符和 Qt 提供的命令提示符,结果相同。可以从 Windows 命令提示符构建,还是应该从 MinGW 的 shell 构建?

平台:
Qt 5.1
CMake 2.8.11.2
MinGW/GCC 4.8
视窗 7

【问题讨论】:

    标签: qt gcc cmake mingw qt5


    【解决方案1】:

    在运行cmake 之前从PATH 中获取git 路径。

    这是做到这一点的魔法:

    set PATH=%PATH:C:/Program Files (x86)/Git/bin;=%
    

    【讨论】:

      【解决方案2】:

      这个 CMakeLists.txt 文件可以正常工作:

      project(Qt5_cmake_test)
      
      cmake_minimum_required(VERSION 2.8.11)
      
      set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.1.1/5.1.1/mingw48_32")
      
      # Find includes in corresponding build directories
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      # Instruct CMake to run moc automatically when needed.
      set(CMAKE_AUTOMOC ON)
      
      # Find the Qt libraries
      find_package(Qt5Core REQUIRED)
      find_package(Qt5Widgets REQUIRED)
      
      # Add the source files from the current directory
      aux_source_directory(. SRC_LIST)
      
      # Tell CMake to create the executable
      add_executable(${PROJECT_NAME} WIN32 ${SRC_LIST})
      
      # Use Qt5 modules
      target_link_libraries(${PROJECT_NAME}
          Qt5::Widgets
          Qt5::WinMain)
      

      变化是:

      添加了find_package(Qt5Core REQUIRED)
      Qt5::WinMain 添加到target_link_libraries

      【讨论】:

        【解决方案3】:

        在我对 SO 的一些回答中,我已经描述了。 CMake 不喜欢 sh.exe。

        sh.exe was found in your PATH, here:
        
          C:/Program Files (x86)/Git/bin/sh.exe
        

        解决方案:尽快重命名C:/Program Files (x86)/Git/bin/sh.exe

        例如:

        C:/Program Files (x86)/Git/bin/shxx.exe
        

        但是不要忘记所有东西都是在什么时候建成的。再次正确重命名。

        【讨论】:

        • 好吧,考虑到我必须为每次运行 CMake 重命名它,然后运行 ​​git,反之亦然,......我相信它会变得烦人。要么我会忽略警告,要么我会编写一个脚本,当我调用 git 时会自动执行它。
        • @Pietro :让我很恼火。在我的环境中,它是 c:\msys\1.0\bin\sh.exe 。现在还没有弄清楚为什么要打扰 sh.exe CMake。
        • 您还可以从 PATH 环境变量中删除 C:/Program Files (x86)/Git/bin。
        • 这太疯狂了!不要只是重命名这样的东西。
        猜你喜欢
        • 2013-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多