With some compilers, using pow
and certain other functions in a C program requires linking to the m
library. However, some compilers don't require this and would error out on linking to the m
library. An almost identical situation exists for C++ with std::thread
and pthread
, but the CMake module FindThreads
alleviates this entirely - is there some similar module for libm?
What is the best way to detect what to do with CMake? This is my current solution, which is less than ideal because there are many more C compilers than just GCC and MSVC:
if(NOT MSVC)
target_link_libraries(my-c-target PUBLIC m)
endif()
This works for my purposes but I'm pretty sure there are cases where it would fail and require manual user intervention, which isn't fun for someone who doesn't know about this obscurity. Ideally I don't want the user to have to specify whether their compiler is weird or not via the commandline; I want to detect it automatically within CMake, since this is the entire point of CMake.
-lm
because it was the first error I ever got from that compiler. It is not unlikely that my particular code will be used in such a scenario. – Aubelibm
is set using CMake'starget_link_libraries(MY_TARGET m)
when using the Visual Studio compiler v16. – Hertfordshire