[CMake] CHECK_FUNCTION_EXISTS() issue

Brad King brad.king at kitware.com
Thu Aug 3 09:35:58 EDT 2006


Crni Gorac wrote:

> Back to my OpenMP based project, I was able to build following cmake
> test regarding is given compiler supporting OpenMP or not.  Basic idea
> is like in corresponding macro from autoconf macros archive: try to
> check is one of OpenMP functions (omp_set_num_threads() here)
> available when various flags passed to compiler (OpenMP support is
> turned on with a single flag on each compiler supporting OpenMP, it's
> only that flags are different for different compilers).  So cmake test
> would look as follows:
> 
> #####
>  INCLUDE(CheckFunctionExists)
> 
>  MESSAGE(STATUS "Check for compiler OpenMP support...")
>  SET(OPENMP_FLAG)
>  SET(OPENMP_FLAG_FOUND FALSE)
>  SET(
>    OPENMP_FLAGS
>    "-fopenmp" # gcc
>    "-openmp" # icc
>    "-mp" # SGI & PGI
>    "-xopenmp" # Sun
>    "-omp" # Tru64
>    "-qsmp=omp" # AIX
>  )
>  SET(OPENMP_FUNCTION omp_set_num_threads)
> 
>  FOREACH(FLAG ${OPENMP_FLAGS})
>    IF(NOT OPENMP_FLAG_FOUND)
>      MESSAGE(STATUS "Check if \"${FLAG}\" OpenMP flag working...")
> 
>      SET(CMAKE_REQUIRED_FLAGS ${FLAG})
>      CHECK_FUNCTION_EXISTS(${OPENMP_FUNCTION} OPENMP_FUNCTION_FOUND)
> 
>      IF(OPENMP_FUNCTION_FOUND)
>        SET(OPENMP_FLAG ${FLAG})
>        SET(OPENMP_FLAG_FOUND TRUE)
>      ENDIF(OPENMP_FUNCTION_FOUND)
>    ENDIF(NOT OPENMP_FLAG_FOUND)
>  ENDFOREACH(FLAG ${OPENMP_FLAGS})
> 
>  IF(NOT OPENMP_FLAG_FOUND)
>    MESSAGE(FATAL_ERROR "Given compiler does not support OpenMP.")
>  ENDIF(NOT OPENMP_FLAG_FOUND)
> 
>  MESSAGE(STATUS "Check for compiler OpenMP support: yes")
>  SET_TARGET_PROPERTIES(
>    my-executable PROPERTIES
>    COMPILE_FLAGS ${OPENMP_FLAG}
>    LINK_FLAGS ${OPENMP_FLAG}
>  )
> #####
> 
> However, there is a problem with CHECK_FUNCTION_EXIST() macro, namely
> after calling it first time, looks like the result is cached and macro
> body is not executed when macro later called again within FOREACH
> loop.  So my question is - is there a way to employ this macro to
> check if given function exists in kind of loop?

The result is cached so that CMake doesn't have to do the same tests 
over and over each time it re-runs to update the build system.  You 
should use a different variable name for each test.  Using 
OPENMP_FUNCTION_FOUND${FLAG} instead of OPENMP_FUNCTION_FOUND should 
work because variables can contain dashes.

-Brad



More information about the CMake mailing list