CHECK_LIBRARY_EXISTS broken by CMAKE_TRY_COMPILE_TARGET_TYPE==STATIC_LIBRARY
When building Curl for iOS, I get the following error:
ld: library not found for -lsocket
I previously opened an issue on Curl, but I think it may be related to CMake. I would like to mention that I have been building Curl for iOS for years using the same toolchain, and it still works on the github macOS machines in CI (but they are still on macOS 10, I am on 11.2.3).
According to this StackOverflow answer, I believe that libsocket
is not a thing with iOS:
I'm not really sure about libintl, but libsocket is a library that only exists on System V-style Unixes (e.g. Solaris, HP-UX). If you're on Linux or a BSD-derivative (that includes Mac OS X), then you don't need to link to libsocket because sockets are implemented in libc which is linked in by default. Try linking without -lsocket and -lintl.
Moreover, looking for it in my iPhoneOS sdk, I cannot find it:
% find /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk/ | grep socket
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk//usr/include/sys/socket.h
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS14.4.sdk//usr/include/sys/socketvar.h
I tried to CHECK_LIBRARY_EXISTS
both for my macOS 11.2.3 and for iOS, using this ios cmake toolchain. It finds it for iOS, which I don't understand.
Here is my test CMakeLists.txt:
cmake_minimum_required(VERSION 3.10.2)
project(test_libsocket_exists)
include(CheckLibraryExists)
message(STATUS "Looking for libsocket in ${CMAKE_LIBRARY_PATH}")
check_library_exists("socket" connect "${CMAKE_LIBRARY_PATH}" HAVE_LIBSOCKET)
if(${HAVE_LIBSOCKET})
message(STATUS "Found libsocket!")
else()
message(STATUS "Did not find libsocket")
endif()
For macOS, I ran it with:
cmake -Bbuild -S.
and got the following output:
-- Looking for libsocket in
-- Looking for connect in socket
-- Looking for connect in socket - not found
-- Did not find libsocket
Note how ${CMAKE_LIBRARY_PATH}
is empty: "Looking for libsocket in".
For iOS, I ran the test with:
cmake -DCMAKE_TOOLCHAIN_FILE=/tmp/ios.toolchain.cmake -DPLATFORM=OS64 -Bbuild -S.
and got the following output:
-- Looking for libsocket in
-- Looking for connect in socket
-- Looking for connect in socket - found
-- Found libsocket!
Again, ${CMAKE_LIBRARY_PATH}
is empty, but this time it found something...
Am I right in thinking that this may be a bug? Again, Curl currently builds fine on the github CI macOS machines (which run macOS 10 and possibly an older iPhoneOS sdk).