为什么这个 CMake 脚本找到"alloca"但仍然失败?

Why does this CMake script find "alloca" and still fail?

本文关键字:alloca 失败 CMake 脚本 为什么      更新时间:2023-10-16

我正在我的一个项目中使用 alloca 函数,并决定使用 CMake 来确保它可用。所以我把这个位添加到我的CMakeLists.txt文件中:

include(CheckSymbolExists)
check_symbol_exists(alloca stdlib.h;cstdlib ALLOCA_EXISTS)
if (NOT ALLOCA_EXISTS)
    message(FATAL_ERROR "Platform does not support alloca")
endif ()

当我运行 CMake 时,这是(相关部分(输出:

-- Looking for alloca
-- Looking for alloca - found
CMake Error at CMakeLists.txt:11 (message):
  Platform does not support alloca

-- Configuring incomplete, errors occurred!

那么为什么显示的代码找到了函数但没有设置变量呢?还是别的什么?

指定标头时必须添加引号:

check_symbol_exists(alloca "stdlib.h;cstdlib" ALLOCA_EXISTS)

否则,将忽略ALLOCA_EXISTS,并使用值 TRUE 创建变量cstdlib