CMake check_function_exists only called one time i

2019-08-26 15:26发布

问题:

I have a CMake macro that calls check_function_exists() to detect several math functions.

By the output below, it seems that check_function_exists() is only called the first time;

macro(nco_check_funcs func def)
message(${ARGV0})
check_function_exists(${ARGV0} have_result)
message(${have_result})
if (NOT have_result)
  message("-- Using NCO defined version of ${ARGV0}")
  add_definitions(-D${ARGV1})
endif()
endmacro(nco_check_funcs)

nco_check_funcs(atan2 NEED_ATAN2)
nco_check_funcs(acosf NEED_ACOSF)
nco_check_funcs(asinf NEED_ASINF)

in the example below the macro is called 3 times, but the output of check_function_exists() only shows up 1 time

atan2
-- Looking for atan2
-- Looking for atan2 - found
1
acosf
1
asinf
1

回答1:

Those results of check_function_exists() are cached.

Check that the <function> is provided by libraries on the system and store the result in a <variable>. <variable> will be created as an internal cache variable.

Add the following to the begin of your macro:

unset(have_result CACHE)

Or if you want to keep the functionality of only searching ones for the function (and cache the result), you need the variable name to depend on the function like this:

check_function_exists(${ARGV0} have_result_${ARGV0})

Now each search for a function has its own result variable.

Reference

  • unset()


标签: cmake macros