我目前正在尝试将qt4设置为与CLion IDE中的cmake一起使用,但是我在指定安装路径时遇到了麻烦。我正在运行Ubuntu,所以我使用sudo apt install qt4-default下载了qt4。它将安装到哪里?如何告诉cmake在哪里可以找到软件包配置文件?

这是我的CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.15)
project(ContactClasses)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp Contact.h Contact.cpp ContactList.h ContactList.cpp ContactFactory.h ContactFactory.cpp)
set(CMAKE_PREFIX_PATH "/usr/share/qt4")

find_package(Qt4Core REQUIRED)

add_executable(Homework_6 ${SOURCE_FILES})

target_link_libraries(ContactClasses Qt4::Core)

当我尝试重新加载它时,出现此错误:
CMake Error at CMakeLists.txt:8 (find_package):
  By not providing "FindQt4Core.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "Qt4Core", but
  CMake did not find one.

  Could not find a package configuration file provided by "Qt4Core" with any
  of the following names:

    Qt4CoreConfig.cmake
    qt4core-config.cmake

  Add the installation prefix of "Qt4Core" to CMAKE_PREFIX_PATH or set
  "Qt4Core_DIR" to a directory containing one of the above files.  If
  "Qt4Core" provides a separate development package or SDK, be sure it has
  been installed.

最佳答案

FindQt4中有专门的CMake模块,请参见其documentation

您将其称为如下:

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt4 REQUIRED QtCore)
add_executable(Homework_6 ${SOURCE_FILES})
target_link_libraries(Homework_6 PUBLIC Qt4::QtCore)

关于c++ - 如何在Ubuntu中使用cmake设置qt4?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60370402/

10-11 00:57