You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.1 KiB

  1. # - Try to find how to link to the standard math library, if anything at all is needed to do.
  2. # On most platforms this is automatic, but for example it's not automatic on QNX.
  3. #
  4. # Once done this will define
  5. #
  6. # STANDARD_MATH_LIBRARY_FOUND - we found how to successfully link to the standard math library
  7. # STANDARD_MATH_LIBRARY - the name of the standard library that one has to link to.
  8. # -- this will be left empty if it's automatic (most platforms).
  9. # -- this will be set to "m" on platforms where one must explicitly
  10. # pass the "-lm" linker flag.
  11. #
  12. # Copyright (c) 2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  13. # Redistribution and use is allowed according to the terms of the 2-clause BSD license.
  14. include(CheckCXXSourceCompiles)
  15. # a little test program for c++ math functions.
  16. # notice the std:: is required on some platforms such as QNX
  17. set(find_standard_math_library_test_program
  18. "#include<cmath>
  19. int main() { std::sin(0.0); std::log(0.0f); }")
  20. # first try compiling/linking the test program without any linker flags
  21. set(CMAKE_REQUIRED_FLAGS "")
  22. set(CMAKE_REQUIRED_LIBRARIES "")
  23. CHECK_CXX_SOURCE_COMPILES(
  24. "${find_standard_math_library_test_program}"
  25. standard_math_library_linked_to_automatically
  26. )
  27. if(standard_math_library_linked_to_automatically)
  28. # the test program linked successfully without any linker flag.
  29. set(STANDARD_MATH_LIBRARY "")
  30. set(STANDARD_MATH_LIBRARY_FOUND TRUE)
  31. else()
  32. # the test program did not link successfully without any linker flag.
  33. # This is a very uncommon case that so far we only saw on QNX. The next try is the
  34. # standard name 'm' for the standard math library.
  35. set(CMAKE_REQUIRED_LIBRARIES "m")
  36. CHECK_CXX_SOURCE_COMPILES(
  37. "${find_standard_math_library_test_program}"
  38. standard_math_library_linked_to_as_m)
  39. if(standard_math_library_linked_to_as_m)
  40. # the test program linked successfully when linking to the 'm' library
  41. set(STANDARD_MATH_LIBRARY "m")
  42. set(STANDARD_MATH_LIBRARY_FOUND TRUE)
  43. else()
  44. # the test program still doesn't link successfully
  45. set(STANDARD_MATH_LIBRARY_FOUND FALSE)
  46. endif()
  47. endif()