You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CMake教程Step4中check_function_exists检测log/exp函数失败求助

Fixing CMake Step4 CheckFunctionExists Failure for log/exp

Hey there, let's work through this CMake Step4 issue you're stuck on! I've run into this exact problem before, so here's what's likely going wrong and how to fix it.

The Root Cause

The check_function_exists module works great, but here's the catch: on many Unix-like systems (and some Windows environments like MinGW), the log and exp functions live in the math library (libm) instead of the default standard C library. CMake doesn't link this library automatically when running these checks, so the linker throws an error during the test, causing the "Looking..." step to fail.

The Fix

You just need to tell CMake to link against the math library before running the function checks. Here's your modified top-level CMakeLists.txt code:

# does this system provide the log and exp functions?
include (CheckFunctionExists)

# Link against libm for systems where math functions aren't in the standard library
set(CMAKE_REQUIRED_LIBRARIES m)

check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

# Optional: Reset the library variable if you have other checks later that don't need libm
set(CMAKE_REQUIRED_LIBRARIES "")

Extra Tips for Cross-Platform Projects

If you're targeting multiple platforms (like Windows, macOS, Linux), you can make this conditional to avoid unnecessary library links on systems that don't need it:

# does this system provide the log and exp functions?
include (CheckFunctionExists)

# Only link libm on Unix-like systems (excluding macOS, which includes math functions in libc)
if(UNIX AND NOT APPLE)
    set(CMAKE_REQUIRED_LIBRARIES m)
endif()

check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

set(CMAKE_REQUIRED_LIBRARIES "")

Final Step

Don't forget to clear CMake's cache before re-running the configure step! Delete the CMakeCache.txt file in your build directory, then run cmake .. again. This ensures CMake picks up your new settings instead of using old cached values.

内容的提问来源于stack exchange,提问作者jwm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:58:32