Skip to content

Replace CheckFunctionExists with CheckSymbolExists - #1316

Open
StephenCWills wants to merge 2 commits into
madler:developfrom
StephenCWills:check-symbol-exists
Open

StephenCWills wants to merge 2 commits into
madler:developfrom
StephenCWills:check-symbol-exists

Conversation

@StephenCWills

Copy link
Copy Markdown

According to CMake documentation, CheckSymbolExists should be preferred over CheckFunctionExists.
https://gitlab.kitware.com/cmake/cmake/-/blob/a72e241200c9fcb1873ef7246dcc43093d782bf7/Modules/CheckFunctionExists.cmake#L35-45

Prefer using CheckSymbolExists or CheckSourceCompiles instead of this command, for the following reasons:

  • check_function_exists() can't detect functions that are inlined in headers or defined as preprocessor macros.
  • check_function_exists() can't detect anything in the 32-bit versions of the Win32 API, because of a mismatch in calling conventions.
  • check_function_exists() only verifies linking, it does not verify that the function is declared in system headers.

Furthermore, we recently discovered on our project that CheckFunctionExists straight-up doesn't work when CMAKE_TRY_COMPILE_TARGET_TYPE is set to STATIC_LIBRARY. See: diasurgical/DevilutionX#8691

Here is a simple CMake project to verify.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12...3.31)
project(test)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
include(CheckFunctionExists)
check_function_exists(nonexistent_function HAVE_NONEXISTENT_FUNCTION)

Output:

-- The C compiler identification is GNU 15.3.0
-- The CXX compiler identification is GNU 15.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for nonexistent_function
-- Looking for nonexistent_function - found
-- Configuring done (0.4s)
-- Generating done (0.0s)
-- Build files have been written to: /tmp/test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant