CMake没有看到系统变量

时间:2015-08-05 22:08:46

标签: c++ qt cmake

为什么CMake看不到我的系统变量?

if (NOT DEFINED $ENV{QT_LIBS_DIR})
  message(FATAL "You need to have system-wide variable with path to Qt libs")
  set(QT_CMAKE_MODULES_FOUND FALSE)
elseif(NOT DEFINED $ENV{QT_LIBS_DIR})
  set(QT_CMAKE_MODULES "$ENV{QT_LIBS_DIR}/lib/cmake" CACHE PATH "CMake's modules which provided by Qt itself for Qt bindings")
  set(QT_CMAKE_MODULES_FOUND TRUE)
endif(NOT DEFINED $ENV{QT_LIBS_DIR})

我得到了致命的错误。请帮我调查一下。顺便说一句:

$ echo $QT_LIBS_DIR

输出为: /home/drew/Qt/5.5/gcc_64

1 个答案:

答案 0 :(得分:3)

为了检查环境变量,我使用if ("$ENV{QT_LIBS_DIR}" STREQUAL "")来查看是否定义了环境变量。所以你的例子就变成了:

if ("$ENV{QT_LIBS_DIR}" STREQUAL "")
  message(FATAL "You need to have system-wide variable with path to Qt libs")
  set(QT_CMAKE_MODULES_FOUND FALSE)
elseif()
  set(QT_CMAKE_MODULES "$ENV{QT_LIBS_DIR}/lib/cmake" CACHE PATH "CMake's modules which provided by Qt itself for Qt bindings")
  set(QT_CMAKE_MODULES_FOUND TRUE)
endif()

但是对于它的价值,我相信qt的find_package根据它在路径中首先找到的qmake可执行文件获取qt的路径。这样您就不需要使用环境变量。这适用于qt4;有关详细信息,请参阅How specify Qt version/location?

对于QT5,Qt5/CMake manual表示"为了使find_package成功,必须在CMAKE_PREFIX_PATH下面找到Qt 5,或者必须在CMake缓存中将Qt5_DIR设置为Qt5WidgetsConfig的位置.cmake文件。使用CMake的最简单方法是将CMAKE_PREFIX_PATH环境变量设置为Qt 5的安装前缀。"