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 OverflowHow to link to the C math library with CMake? - Stack Overflow
How to link <math.h> library using CMake? - clion
Adding math library with `-lm` to CMake build of nrf mesh breaks SES project generation
How do I get "math.h" in a derivation so that CMake finds it?
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)
I am frankly a bit surprised that this kind of question still doesn't have a proper answer for Modern CMake. These days, the recommended (and portable) approach is this:
find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
target_link_libraries(MyTarget PUBLIC ${MATH_LIBRARY})
endif()
Cmakelists.txt file is like it:
cmake_minimum_required(VERSION 3.6)
project(project_name)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")
set(SOURCE_FILES main.c)
add_executable(project_name ${SOURCE_FILES})
And you must add this command, for <math.h>
target_link_libraries(project_name PRIVATE m)
That's all.
Add below command in CMakeList.txt
target_link_libraries(${PROJECT_NAME} m)
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.