How to link to the C math library with CMake?

前端 未结 2 1694
名媛妹妹
名媛妹妹 2020-11-29 09:10

How do I add the math library to my CMake file? This post references adding a target link library, yet I am not too familiar with C. An Additional post - Could

2条回答
  •  独厮守ぢ
    2020-11-29 09:43

    Many mathematical functions (pow, sqrt, fabs, log etc.) are declared in math.h and require the library libm to be linked. Unlike libc, which is automatically linked, libm is a separate library and often requires explicit linkage. The linker presumes all libraries to begin with lib, so to link to libm you link to m.

    You have to use it like target_link_libraries(ch4 m) to link libmto your target. The first argument must be a target. Thus it must be used after add_executable(ch4 ch4.c) like:

    add_executable(ch4 ch4.c)
    target_link_libraries(ch4 m)
    

提交回复
热议问题