[CMake] Find function that may need libraries?

Alexander Neundorf a.neundorf-work at gmx.net
Thu Oct 2 18:24:25 EDT 2008


On Thursday 02 October 2008, Matthew Woehlke wrote:
> Is there a standard way to find the library needed for a particular
> function, when possibly none is needed? For example, gethostbyname might
> be provided in libc, libnsl, libsocket, etc. on various platforms.
>
> I've found myself a couple times now writing a macro to do this, but I
> wonder if there is a standard way? If not, would this possibly be a good
> module to add?
>
> Here's my most recent incarnation, which piggybacks on the existing
> CheckFunctionExists module (borrows its test source). This takes the
> name of the variable to receive the required library (may be empty), the
> name of the function to look for, and a list of libraries that might
> contain the function. It first tries with no additional libraries to see
> if the default libraries suffice.

Nice idea :-)

> macro(find_library_sym var sym)
>      message(STATUS "Checking what library is needed for ${sym}")
>      try_compile(HAVE_${sym} ${CMAKE_BINARY_DIR}
>                  ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
>                  COMPILE_DEFINITIONS -DCHECK_FUNCTION_EXISTS=${sym}
>                 )
>      if(HAVE_${sym})
>          # No library needed
>          message(STATUS ""Checking what library is needed for ${sym} -
> none")
>      else(HAVE_${sym})
>          foreach(lib ${ARGN})
>              set(CMAKE_REQUIRED_LIBRARIES ${lib})

Is the line above actually necessary ?
I think not.
But instead the module should support the same CMAKE_REQUIRED_* variables as 
CheckFunctionExists.cmake does.

Alex


More information about the CMake mailing list