[CMake] Find function that may need libraries?

Matthew Woehlke mw_triad at users.sourceforge.net
Thu Oct 2 19:27:53 EDT 2008


Alexander Neundorf wrote:
> 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.

Oops, no, that's left over from when I was using CheckFunctionExists 
instead of my own try_compile (the existing module is too noisy for 
calling several times). Thanks for spotting that, I removed it from my 
own sources also :-).

> But instead the module should support the same CMAKE_REQUIRED_* variables as 
> CheckFunctionExists.cmake does.

Sure. I didn't say it was polished :-). It does what I need it, where I 
need it. As a proper module, it a start, but would need more integration.

-- 
Matthew
Please do not quote my e-mail address unobfuscated in message bodies.
-- 
This .sig is false



More information about the CMake mailing list