【问题标题】:Setting up Qt5 with CMakeLists.txt - file not found errors使用 CMakeLists.txt 设置 Qt5 - 找不到文件错误
【发布时间】:2016-09-02 00:00:06
【问题描述】:

我正在尝试在 OS X 上设置一个 cmake 项目。我几乎可以编译它,但我在让 Qt5 工作时遇到了一点麻烦。

当我尝试编译时,我得到:

fatal error: 'QtCore/QDir' file not found
fatal error: 'QtCore/QDate' file not found

当我检查我的代码时,IDE 将 QtCore 和 QtWidgets 都以红色突出显示,表示未找到它们。

你们中有人知道我错过了什么吗?

我的 CMakeLists.txt 如下:

cmake_minimum_required(VERSION 3.3)
project(miniLogbook)

subdirs(sources headers lib other)

#For a basic project, you should probably only need to edit these    variables

#Base QT directory
set(QT_PATH "/Users/rwardrup/Qt")

#Build output path of your QT Creator project
set(QT_BIN "./ml_debug")

#QT Version used, e.g. Probably found in ${QT_PATH}
set(QT_VERSION "5.7")

#Libraries to link to
set(QT_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Gui)

#Libraries required. Probably the same as above minus the '::'.     find_package() will be called on these

set(QT_LIBRARIES_REQUIRED Qt5Core Qt5Widgets Qt5Gui)
#################################################################

#Pull needed header files generated by QT first
set(QT_GENERATED qtGenerated)
add_custom_target(${QT_GENERATED})

#Get all header files in the directory
file(GLOB QT_GEN_HEADERS ${QT_BIN}/*.h)

#Copy them to the project dir
foreach(QT_GEN_HEADERS ${QT_GEN_HEADERS})
    add_custom_command(TARGET ${QT_GENERATED} PRE_BUILD COMMAND
            ${CMAKE_COMMAND} -E copy_if_different
            ${QT_GEN_HEADERS} ${CMAKE_SOURCE_DIR})
endforeach()

#Build tool paths
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
set(CMAKE_AR "/usr/bin/ar")
set(CMAKE_LINKER "/usr/bin/g++")

#find packages
foreach(QT_LIBRARIES_REQUIRED ${QT_LIBRARIES_REQUIRED})
    find_package( ${QT_LIBRARIES_REQUIRED} REQUIRED )
endforeach()

file(GLOB QT_CPP_GENERATED ${QT_BIN}/*.cpp)

set(SOURCES sources/main.cpp sources/mainwindow.cpp     headers/mainwindow.h ${QT_GEN_HEADERS} ${QT_CPP_GENERATED})

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.59.0 COMPONENTS filesystem)

if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(miniLogbook ${SOURCES})
    add_dependencies(${PROJECT_NAME} ${QT_GENERATED})
    target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES}     ${Boost_LIBRARIES})
endif()

【问题讨论】:

标签: c++ cmake qt5


【解决方案1】:

你的 foreach loops 被窃听了。

在这两种情况下,您都在用第一个元素覆盖要循环的值列表。

他们应该按照以下模式阅读:

foreach(_loop_var ${list_of_values})
  # do stuff with ${_loop_var}
endforeach(_loop_var)

我确定您面临的问题只是 CMake 没有搜索到您希望它搜索的所有 Qt5 模块的症状。您可能需要重新检查 CMake 缓存和日志输出。


旁注:将系统组件的绝对路径硬编码到 CMake 文件中是非常糟糕的做法。这使得 CMake(几乎)无用。如果你不想给 CMake 提供很多命令行参数来指定特定位置,read about toolchain files

如果这个 CMakeLists 文件是某个地方的示例模板,我建议不要使用该源来获取与 CMake 相关的建议。

【讨论】:

  • 谢谢。修复了那些 foreach 循环。我的项目结构类似于this example,其中我有:root |-sources | |-main.cpp | |-CMakeLists.txt |-LibProject | |-公司 | | '-test.h | |-src | | |-test.cpp | | '-CMakeLists.txt | '-CMakeLists.txt '-CMakeLists.txt
猜你喜欢
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多