cmake DEFINED似乎无法识别变量 [英] cmake DEFINED does not seems to recognize variable
本文介绍了cmake DEFINED似乎无法识别变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在cmake中有以下代码:
I have this code in cmake:
#mandatory
SET(BOOST_DIR "$ENV{BOOST_HOME}")
if (DEFINED ${BOOST_DIR})
#global include directories
include_directories(${BOOST_DIR})
else(DEFINED ${BOOST_DIR})
message( STATUS "BOOST_HOME at ${BOOST_DIR}")
message( FATAL_ERROR "Undefined BOOST_HOME env var.")
endif(DEFINED ${BOOST_DIR})
定义了环境变量BOOST_HOME(/ home / ferran / boost)。行为很奇怪,因为输出为:
The env var BOOST_HOME is defined (/home/ferran/boost). The behavior is very odd because the output is:
-- BOOST_HOME at /home/ferran/boost
CMake Error at CMakeLists.txt:14 (message):
Undefined BOOST_HOME env var.
-- Configuring incomplete, errors occurred!
那么,var存在并打印其内容,而同时不存在吗?
So, the var exists and prints its contents and at the same time does not exists?
推荐答案
您需要避免在 BOOST_DIR
> if 语句:
You need to avoid dereferencing the variable BOOST_DIR
in the if
statement:
set(BOOST_DIR "$ENV{BOOST_HOME}")
if(BOOST_DIR) # <--- Use 'BOOST_DIR', not 'DEFINED ${BOOST_DIR}'
#global include directories
include_directories(${BOOST_DIR})
else()
message(STATUS "BOOST_HOME at ${BOOST_DIR}")
message(FATAL_ERROR "Undefined BOOST_HOME env var.")
endif()
通过取消引用 BOOST_DIR
,您可以有效地查询CMake是否具有名为的变量/ home / ferran / boost
定义。
By dereferencing BOOST_DIR
, you're effectively querying if CMake has a variable called /home/ferran/boost
defined.
这篇关于cmake DEFINED似乎无法识别变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文