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)
Answer from usr1234567 on Stack Overflow
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 206607085-CLion-Enabling-math-h-for-C-projects
CLion - Enabling for C projects – IDEs Support (IntelliJ Platform) | JetBrains
August 24, 2015 - //---------------------------------------------------------------CMakeList cmake_minimum_required(VERSION 3.6) project(Section3_Lecture21_Constants) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall -pedantic") set(SOURCE_FILES main.c) add_executable(Section3_Lecture21_Constants ${SOURCE_FILES}) target_link_libraries(Section3_Lecture21_Constants m) //--------------------------------------------------------main.c #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { #define PI 3.141593 printf("%f\n",PI); printf("%.10f\n", M_PI); return 0; }
🌐
GitHub
github.com › android › ndk › issues › 1274
[BUG] Incorrect math.h included from cmath for NDK r21 · Issue #1274 · android/ndk
June 9, 2020 - Most probable explanation is that sysroot/usr/include/c++/v1/cmath includes "generic C" sysroot/usr/include/math.h (where missing functions declared as macros and therefore no global namespace functions exeist) instead of "C++" sysroot/usr/include/c++/v1/math.h (that has inline declaration of missing functions and includes generic sysroot/usr/include/math.h via "include_next")
Author   timofey-retailnext
Discussions

How to link to the C math library with CMake? - Stack Overflow
Is m a placeholder? or is it just common knowledge that math.c is in a library called m? Or is it declared in a way that I don't fathom in the script above? 2019-01-17T19:30:46.933Z+00:00 ... Awesome, I get it now. I linked to a question that I had asked earlier about how "libm" becomes just "m" just for completeness. 2019-01-18T15:18:41.447Z+00:00 ... With MSVC this results in the following build error: [CMake... More on stackoverflow.com
🌐 stackoverflow.com
How to link <math.h> library using CMake? - clion
Just out of curiosity - if the ... link the math library. The same might hold for other target systems if gcc is used. ... @usr1234567 no, it is not, there is nothing there that helps nor matches SEGV's answer here. This is a very obscure usage. ... cmake_minimum_requi... More on stackoverflow.com
🌐 stackoverflow.com
Adding math library with `-lm` to CMake build of nrf mesh breaks SES project generation
I am building a project with CMake, inside nRF SDK for Mesh 3.2.0, which uses the math library: 1. source file includes math.h 2. CMakeLists.txt adds m to target_link_libraries More on devzone.nordicsemi.com
🌐 devzone.nordicsemi.com
1
0
August 22, 2019
How do I get "math.h" in a derivation so that CMake finds it?
Why not just use a nix development shell? There’s a lot of flakes out there of sample dev shells. They completely get around the need for derivations and such More on reddit.com
🌐 r/NixOS
2
3
November 6, 2020
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 360007378619-Clion-Cmake-auto-include-math-h-library
Clion Cmake auto include math.h library – IDEs Support (IntelliJ Platform) | JetBrains
is there a way to include math library automatically without the need to add manually to Cmakelists.txt for every project? ... You don't need to add math.h to the CMakeLists.txt, you just need to use #include <math.h> in your .c/.cpp file.
🌐
CMake
cmake.org › cmake › help › latest › command › math.html
math — CMake 4.3.0-rc3 Documentation
Hexadecimal notation as in C code, i. e. starting with "0x". ... Decimal notation. Which is also used if no OUTPUT_FORMAT option is specified. ... math(EXPR value "100 * 0xA" OUTPUT_FORMAT DECIMAL) # value is set to "1000" math(EXPR value "100 * 0xA" OUTPUT_FORMAT HEXADECIMAL) # value is set to "0x3e8"
🌐
Reddit
reddit.com › r/nixos › how do i get "math.h" in a derivation so that cmake finds it?
r/NixOS on Reddit: How do I get "math.h" in a derivation so that CMake finds it?
November 6, 2020 -

This is probably a dumb question, but I'm trying to get a small CMake project to compile and no luck. The error message I get is as follows:

CMake Error at cmake/Modules/FindLibM.cmake:80 (message):
  Could not find LibM headers or libraries!
Call Stack (most recent call first):
  CMakeLists.txt:53 (find_package)

Looking at the source code, i see this in FindLibM.cmake:

# Check for the header files:
find_path( LibM_INCLUDE_DIR
  NAMES math.h
  HINTS /usr/include /usr/local/include /usr/local/bic/include
  PATH_SUFFIXES
  )
#set( LibM_INCLUDE_DIR TRUE )



# Check for the libraries:
set( LibM_LIBRARIES "" )

find_library( LibM_LIBRARY
  NAMES m
  HINTS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu /usr/lib/i386-linux-gnu
  #PATH_SUFFIXES 
  NO_DEFAULT_PATH
  )

It goes without saying but I'm no expert on C/C++, so I'm just trying to follow other derivations in nixpkgs. I believe math.h is part of the standard library, which I believe (?) is glibc. The derivation for this includes a libm.so (I'm no teven sure if it's the right library), but including it as part of the buildInputs doesn't do anything. It also has no header files.

So, where can I find that header? I believe this program will compile with either the header files or the library. Lastly, do you think patching the cmake code to help it find this "LibM" library could work?

EDIT

I managed to get it working by running substituteInPlace in the CMake file, now it finds all the libraries but for some annoying reason the C compiler complaints that it can't find the headers, despite them being there. I'll see what I can do.

Find elsewhere
🌐
CMake
cmake.org › pipermail › cmake › 2003-September › 004262.html
[Cmake] How to Include standard libraries...
September 8, 2003 - For the math library, just do this: IF(UNIX) TARGET_LINK_LIBRARYES(myexe m) ENDIF(UNIX) Windows compilers include it automatically. For threads: On Visual Studio, the multi-threaded runtime library is used by default.
🌐
CMake
cmake.org › cmake › help › v3.13 › command › math.html
math — CMake 3.13.5 Documentation
HEXADECIMAL = Result in output variable will be formatted in C code Hexadecimal notation. DECIMAL = Result in output variable will be formatted in decimal notation. ... math(EXPR value "100 * 0xA" DECIMAL) results in value is set to "1000" math(EXPR value "100 * 0xA" HEXADECIMAL) results in ...
🌐
FreeBSD
forums.freebsd.org › development › userland programming and scripting
problem with math.h | The FreeBSD Forums
October 12, 2022 - man exp EXP(3) FreeBSD Library Functions Manual EXP(3) NAME exp, expf, expl, exp2, exp2f, exp2l, expm1, expm1f, expm1l, pow, powf, powl – exponential and power functions LIBRARY Math Library (libm, -lm) ... You are using clang and libstdc++? Normally you would use clang++ as the linker driver and that just does the right thing (links with libc++). You may need to use -rpath so that your exe knows where to load libstdc++ (or else use LD_LIBRARY_PATH, but that is a really bad idea). Click to expand... I am using Cmake and set it to use clang on the CmakeList.txt file, not too sure how it is using clang exactly.
🌐
GitHub
github.com › pyk › cmake-tutorial › blob › master › src › math.h
cmake-tutorial/src/math.h at master · pyk/cmake-tutorial
#ifndef CMAKE_TUTORIAL_MATH_H · #define CMAKE_TUTORIAL_MATH_H · · namespace math { · int add(int a, int b); int sub(int a, int b); int mul(int a, int b); ·
Author   pyk
🌐
Stack Overflow
stackoverflow.com › questions › 41257915 › c-program-not-able-to-find-math-h-when-compiling-with-cmake-and-including-open
C++ program not able to find math.h when compiling with CMake and including OpenCV libraries - Stack Overflow
December 21, 2016 - 2) When using newer CMake that whole include_directories should be replaced by a simple target_include_directories(Optimotive PUBLIC ${CMAKE_CURRENT_LIST_DIR}/../Optimotive) making the foreach obsolete. 4) Are you sure CMake detected the correct compiler? ... Yea, I might benefit from using an updated version of CMake, but it should still work with old. Also, I'm not sure how know if it is detecting the right compiler. ... If you go in /Applications/Xcode.app/Contents/Developer/Toolchains/, what do you get if you do find . -iname math.h?
🌐
CMake
cmake.org › cmake › help › v3.0 › command › math.html
math — CMake 3.0.2 Documentation
This documents an old version of CMake. Click here to see the latest release. Mathematical expressions.
🌐
CMake
cmake.org › cmake › help › v3.20 › guide › tutorial › index.html
CMake Tutorial — CMake 3.20.6 Documentation
January 31, 2023 - If the platform has log and exp then we will use them to compute the square root in the mysqrt function. We first test for the availability of these functions using the CheckSymbolExists module in MathFunctions/CMakeLists.txt. On some platforms, we will need to link to the m library. If log and exp are not initially found, require the m library and try again. include(CheckSymbolExists) check_symbol_exists(log "math.h" HAVE_LOG) check_symbol_exists(exp "math.h" HAVE_EXP) if(NOT (HAVE_LOG AND HAVE_EXP)) unset(HAVE_LOG CACHE) unset(HAVE_EXP CACHE) set(CMAKE_REQUIRED_LIBRARIES "m") check_symbol_exists(log "math.h" HAVE_LOG) check_symbol_exists(exp "math.h" HAVE_EXP) if(HAVE_LOG AND HAVE_EXP) target_link_libraries(MathFunctions PRIVATE m) endif() endif()
🌐
GitHub
github.com › termux › termux-packages › issues › 1149
libc++ cmath math functions not found because of -isystem $PREFIX/include added by CMake · Issue #1149 · termux/termux-packages
July 18, 2017 - In file included from /data/data/com.termux/files/home/src/ldc/dmd2/mars.h:70: In file included from /data/data/com.termux/files/home/src/llvm-4.0.1.src/include/llvm/ADT/Triple.h:13: In file included from /data/data/com.termux/files/home/src/llvm-4.0.1.src/include/llvm/ADT/Twine.h:13: In file included from /data/data/com.termux/files/home/src/llvm-4.0.1.src/include/llvm/ADT/SmallVector.h:20: In file included from /data/data/com.termux/files/home/src/llvm-4.0.1.src/include/llvm/Support/MathExtras.h:18: In file included from /data/data/com.termux/files/home/src/llvm-4.0.1.src/include/llvm/Suppor
Author   joakim-noah
🌐
CMake
cmake.org › cmake › help › v3.16 › guide › tutorial › index.html
CMake Tutorial — CMake 3.16.9 Documentation
December 21, 2016 - When this project is built it will first build the MakeTable executable. It will then run MakeTable to produce Table.h. Finally, it will compile mysqrt.cxx which includes Table.h to produce the MathFunctions library.
🌐
Cedanet
cedanet.com.au › cmake › math.php
Math operations in cmake - ceda
CMake has a math command to assign a variable with the evaluation of a mathematical expression.