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.

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})
            try_compile(HAVE_${sym} ${CMAKE_BINARY_DIR}
                        ${CMAKE_ROOT}/Modules/CheckFunctionExists.c
                        COMPILE_DEFINITIONS -DCHECK_FUNCTION_EXISTS=${sym}
                        CMAKE_FLAGS -DLINK_LIBRARIES:STRING=${lib}
                       )
            if(HAVE_${sym})
message(STATUS ""Checking what library is needed for ${sym} - ${lib}")
                set(${var} ${lib})
            endif(HAVE_${sym})
        endforeach(lib ${ARGN})
        set(CMAKE_REQUIRED_LIBRARIES)
    endif(HAVE_${sym})
    if(NOT HAVE_${sym})
message(FATAL_ERROR ""Checking what library is needed for ${sym} - not found")
    endif(NOT HAVE_${sym})
endmacro(find_library_sym)


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

_______________________________________________
CMake mailing list
[email protected]
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to