Symbol lookup in CMake fails although symbol is pr

2019-07-05 05:48发布

问题:

I am trying to check whether symbol getaddrinfo_a exists using CMake:

list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
check_symbol_exists(getaddrinfo_a netdb.h HAVE_GETADDRINFO_A)
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)

While getaddrinfo_a is defined in netdb.h (given that _GNU_SOURCE is defined), CMake fails to find it:

-- Looking for getaddrinfo_a
-- Looking for getaddrinfo_a - not found.

Any idea what am I doing wrong?

回答1:

According to the getaddrinfo_a man page, the function requires libanl at link time. Try setting CMAKE_REQUIRED_LIBRARIES before invoking check_symbol_exists:

list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
list(APPEND CMAKE_REQUIRED_LIBRARIES anl)
check_symbol_exists(getaddrinfo netdb.h HAVE_GETADDRINFO_A)