You should use the CHECK_FUNCTION_EXISTS command to check if pow can be used without additional flags. If this check fails, you can add m library to CMAKE_REQUIRED_LIBRARIES variable, assuming that linking against libm is what's missing. But you'll need to CHECK_FUNCTION_EXISTS again to make sure the linking is sufficient.

Sample code:

include(CheckFunctionExists)

if(NOT POW_FUNCTION_EXISTS AND NOT NEED_LINKING_AGAINST_LIBM)
  CHECK_FUNCTION_EXISTS(pow POW_FUNCTION_EXISTS)
  if(NOT POW_FUNCTION_EXISTS)
      unset(POW_FUNCTION_EXISTS CACHE)
      list(APPEND CMAKE_REQUIRED_LIBRARIES m)
      CHECK_FUNCTION_EXISTS(pow POW_FUNCTION_EXISTS)
      if(POW_FUNCTION_EXISTS)
          set(NEED_LINKING_AGAINST_LIBM True CACHE BOOL "" FORCE)
      else()
          message(FATAL_ERROR "Failed making the pow() function available")
      endif()
  endif()
endif()

if (NEED_LINKING_AGAINST_LIBM)
     target_link_libraries(your_target_here m)
endif()
Answer from arrowd on Stack Overflow
🌐
CMake
cmake.org › cmake › help › latest › command › target_link_libraries.html
target_link_libraries — CMake 4.3.0-rc3 Documentation
Alternatively, generator expressions like $<CONFIG> provide finer per-configuration linking of <item>. For a more structured approach, higher granularity can be achieved by creating and linking to IMPORTED library targets with the IMPORTED_CONFIGURATIONS property set, particularly in find modules. Items containing ::, such as Foo::Bar, are assumed to be IMPORTED or ALIAS library target names and will cause an error if no such target exists. See policy CMP0028. See the CMAKE_LINK_LIBRARIES_STRATEGY variable and corresponding LINK_LIBRARIES_STRATEGY target property for details on how CMake orders direct link dependencies on linker command lines.
Discussions

Linking a lib file in cmake
Hi all, am very new to CMake so pardon me if get something wrong. I have a .dll, .lib, and .h which I want to add to my CMake project and use in cpp code. Here’s my project structure. I tried adding the lib reference using target_link_libraries, and here’s my CMakeLists.txt cmake_minim... More on discourse.cmake.org
🌐 discourse.cmake.org
0
0
April 21, 2022
How to link to the C math library with CMake? - Stack Overflow
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] LINK : fatal error LNK1104: cannot open file 'm.lib' 2019-07-06T11:14:03.727Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
How to statically link external library by target_link_libraries()?
I am trying to achieve the equivalent of following command by CMake. gcc -static -O0 -g main.c /usr/lib/x86_64-linux-gnu/libpthread.a My cmake script successfully built executable file, but gave segmentation fault at run. Can anybody point me what should I fix ? More on discourse.cmake.org
🌐 discourse.cmake.org
1
0
August 18, 2020
c++ - How to properly link libraries with cmake? - Stack Overflow
I can't get the additional libraries I am working with to link into my project properly. I am using CLion, which uses cmake to build its projects. I am trying to use several libraries in conjunctio... More on stackoverflow.com
🌐 stackoverflow.com
🌐
CMake Discourse
discourse.cmake.org › code
Portable checking for libm for embedded use - Code - CMake Discourse
August 22, 2024 - I have been using a check like ... libm: check_symbol_exists(pow "math.h" CMath_HAVE_LIBC_POW) if (NOT CMath_HAVE_LIBC_POW) set(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES}) set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} m) ...
🌐
CMake
cmake.org › cmake › help › latest › command › link_libraries.html
link_libraries — CMake 4.3.0-rc2 Documentation
Link libraries to all targets added later · Specify libraries or flags to use when linking any targets created later in the current directory or below by commands such as add_executable() or add_library(). See the target_link_libraries() command for meaning of arguments
🌐
Reddit
reddit.com › r/visualstudio › linking libm through cmake in visual studio
r/VisualStudio on Reddit: Linking libm through CMake in visual studio
December 30, 2018 - I've written a C library that builds on linux with CMake and Make and I decided to build it on windows as well (since CMake is cross platform I figured why not). I imported the project as a CMake project and the build files were generated O.K., however when I attempt to build the program, it fails because it cannot find 'm.lib' which is the libm math library I linked with -lm on linux.
🌐
CMake Discourse
discourse.cmake.org › code
Linking a lib file in cmake - Code - CMake Discourse
April 21, 2022 - Hi all, am very new to CMake so pardon me if get something wrong. I have a .dll, .lib, and .h which I want to add to my CMake project and use in cpp code. Here’s my project structure. I tried adding the lib reference using target_link_libraries, and here’s my CMakeLists.txt cmake_minimum_required(VERSION 3.14) set(PROJECT_NAME "mantra_mfs100") project(${PROJECT_NAME} LANGUAGES CXX) # This value is used when generating builds using this plugin, so it must # not be changed set(PLUGIN_NAME "...
🌐
ICS
ics.com › blog › find-and-link-libraries-cmake
Find and Link Libraries with CMake | ICS
June 21, 2023 - To do this, we call target_link_libraries. Libraries can be linked PUBLIC, PRIVATE or INTERFACE. PUBLIC: Used by the library and its consumers. PRIVATE: Only needed by the library. INTERFACE: Not used by the library but needed for consumers. After the edits our main CMakeLists.txt will look like this:
Find elsewhere
🌐
CMake Discourse
discourse.cmake.org › code
How to statically link external library by target_link_libraries()? - Code - CMake Discourse
August 18, 2020 - I am trying to achieve the equivalent of following command by CMake. gcc -static -O0 -g main.c /usr/lib/x86_64-linux-gnu/libpthread.a My cmake script successfully built executable file, but gave segmentation fault at r…
Top answer
1 of 1
227

My recommendation is to start simple, and then complicate your project further.

Let me try to explain how linking works in CMake. The idea is that you build modules in CMake, and link them together. Let's ignore header files for now, as they can be all included in your source files.

Say you have file1.cpp, file2.cpp, main.cpp. You add them to your project with:

ADD_LIBRARY(LibsModule 
    file1.cpp
    file2.cpp
)

Now you added them to a module called LibsModule. Keep that in mind. Say you want to link to pthread for example that's already in the system. You can combine it with LibsModule using the command:

target_link_libraries(LibsModule -lpthread)

And if you want to link a static library to that too, you do this:

target_link_libraries(LibsModule liblapack.a)

And if you want to add a directory where any of these libraries are located, you do this:

target_link_libraries(LibsModule -L/home/user/libs/somelibpath/)

Now you add an executable, and you link it with your main file:

ADD_EXECUTABLE(MyProgramExecBlaBla main.cpp)

(I added BlaBla just to make it clear that the name is custom). And then you link LibsModule with your executable module MyProgramExecBlaBla

target_link_libraries(MyProgramExecBlaBla LibsModule)

And this will do it.

What I see in your CMake file is a lot of redundancy. For example, why do you have texture_mapping, which is an executable module in your include directories? So you need to clean this up and follow the simple logic I explained. Hopefully it works.


In summary, it looks like this:

project (MyProgramExecBlaBla)  #not sure whether this should be the same name of the executable, but I always see that "convention"
cmake_minimum_required(VERSION 2.8)

ADD_LIBRARY(LibsModule 
    file1.cpp
    file2.cpp
)

target_link_libraries(LibsModule -lpthread)
target_link_libraries(LibsModule liblapack.a)
target_link_libraries(LibsModule -L/home/user/libs/somelibpath/)
ADD_EXECUTABLE(MyProgramExecBlaBla main.cpp)
target_link_libraries(MyProgramExecBlaBla LibsModule)

The most important thing to understand is the module structure, where you create modules and link them all together with your executable. Once this works, you can complicate your project further with more details. Good luck!


Note: Keep in mind that this is the simple way to use CMake. The better cross-platform way would be using find_package, which locates a package/library, and provides the libraries and includes in CMake variables so that you could link your program to them. Here's how to do this for boost, for example.

🌐
Stack Exchange
robotics.stackexchange.com › questions › 32866 › how-do-i-link-a-library-in-cmake
How do I link a library in cmake? - Robotics Stack Exchange
July 9, 2011 - Put the following in your CMakeLists.txt: # set the path to the library folder link_directories(/usr/local/lib) # link the libraries to the executable target_link_libraries (my_node foo)
🌐
PragmaticLinux
pragmaticlinux.com › home › how to link a shared library with gcc and cmake
How to link a shared library with GCC and CMake - PragmaticLinux
March 18, 2022 - This time around you specify the library using the function call target_link_libraries(): ... Whenever you want to link more shared libraries, you just need to add another parameter.
🌐
CMake
cmake.org › cmake › help › latest › variable › CMAKE_LINK_LIBRARY_USING_FEATURE.html
CMAKE_LINK_LIBRARY_USING_ — CMake 4.3.0-rc3 Documentation
This variable defines how to link a library or framework for the specified <FEATURE> when a LINK_LIBRARY generator expression is used. Both of the following conditions must be met for this variable to have any effect: The associated CMAKE_LINK_LIBRARY_USING_<FEATURE>_SUPPORTED variable must ...
🌐
Embedded Related
embeddedrelated.com › showthread › comp.arch.embedded › 178689-1.php
What does 'm' mean in this cmake file?
January 12, 2014 - > > Thanks, > > > ............................. > include_directories(${CMAKEDEMO_SOURCE_DIR}/w01-cpp) > link_directories(${CMAKEDEMO_BINARY_DIR}/w01-cpp) > > #the one C file > add_executable(cdemo cdemo.c) > target_link_libraries(cdemo m) #link the math library > [ ... ] I'd think it calls for linking to the libm math library, e.g.
🌐
GitHub
github.com › Kitware › CMake › blob › master › Tests › LinkStatic › CMakeLists.txt
CMake/Tests/LinkStatic/CMakeLists.txt at master · Kitware/CMake
October 7, 2022 - project(LinkStatic C) · if(NOT CMAKE_C_COMPILER_ID MATCHES "GNU|LCC") message(FATAL_ERROR "This test works only with the GNU or LCC compiler!") endif() · find_library(MATH_LIBRARY NAMES libm.a) if(MATH_LIBRARY) get_filename_component(MATH_LIB_DIR ${MATH_LIBRARY} PATH) link_directories(${MATH_LIB_DIR}) # Name the library both with a full path and as "-lm" to ·
Author   Kitware
🌐
Reddit
reddit.com › r/cmake › help linking using cmake on larger/ish projects: library linking
r/cmake on Reddit: Help Linking Using CMAKE on Larger/ish Projects: Library Linking
November 4, 2021 -

I've been working on my first larger project which is starting to expand fast, in terms of number of files and sub-directories. I've been trying to use CMake to mostly automate the building process, but right now I feel like I am having to repeat myself quite a bit in the head CMake file when it comes to library linking. For each library and executable I have to first use LINK_LIBRARIES and then use TARGET_LINK_LIBRARIES. If I leave out the first highlighted section (between the #--- sections in the Head CMake file below) the libraries in my src directory do not link properly and if I leave out the TARGET_LINK_LIBRARIES the executables fail to link properly. I am requiring cmake verion 3.0.0.

If this is the way it is supposed to work, then it is what it is; however, while looking this up, I've seen many CMake projects which do not have to link in this way. I'd appreciate any input, even outside of the scope of this question, as this is my first real time using CMake on a large project.

I've included a shortened version of my working tree, the template I've used for creating libraries in src subdirectories, and how the tests are generated.

Head CMake File, shortened to remove Release/debug support, project information, etc.

LINK_LIBRARIES(stdc++fs tinyxml2 sqlite3 spdlog fmt)
INCLUDE_DIRECTORIES(thirdparty/include $ENV{MSYS2INCLUDE} include src/UI/Base)
FIND_PACKAGE(wxWidgets REQUIRED COMPONENTS net core base html ribbon)
FIND_PACKAGE(Threads)
ADD_COMPILE_DEFINITIONS(SPDLOG_FMT_EXTERNAL)
ADD_COMPILE_OPTIONS( --LOTS OF WARNINGS)
INCLUDE(${wxWidgets_USE_FILE})

# ------------------------------------------------------
LINK_LIBRARIES(${wxWidgets_LIBRARIES} Threads::Threads)
# ------------------------------------------------------

ADD_SUBDIRECTORY(thirdparty)
ADD_SUBDIRECTORY(src)
ADD_SUBDIRECTORY(tests)

ADD_EXECUTABLE(XMLTags xmltagextractor.cpp)
ADD_EXECUTABLE(CreatorUI capp.cpp)

# ------------------------------------------------------
TARGET_LINK_LIBRARIES(CreatorUI CreatorBase ThirdParty UI Threads::Threads
                      UI_BASE ${wxWidgets_LIBRARIES} Creator )
TARGET_LINK_LIBRARIES(XMLTags CreatorBase ThirdParty Creator )
# ------------------------------------------------------

I've shortened it to make it easier to glance at but this is my current working tree

.
|   CMakeLists.txt // Head CMake file, Builds CAPP and xmlTagExtractor
|   capp.cpp // Main exe file 1
|   capp.hpp
|   xmltagextractor.cpp // Main exe file 2
|       
+---include // Include directory for projects .h and .hpp files
|   +---Creator
|   |   |   DescriptionTools.hpp
|   |   |   
|   |   \---Base
|   |           CreatorFactory.hpp
|   |           LoggingTools.hpp
|   |           etc. 
|   |           
|   \---UI
|           primativeDesign.hpp
|           
+---src // files to be compiled into libraries
|   |   CMakeLists.txt // Adds Creator and UI subdirectories
|   |   
|   +---Creator // Makes Creator library, see template 1
|   |   |   CMakeLists.txt
|   |   |   DescriptionTools.cpp
|   |   |   etc.
|   |   |   
|   |   \---Base // Makes Creator_Base library, see template 1
|   |           CMakeLists.txt
|   |           CreatorFactory.cpp
|   |           etc.
|   |           
|   \---UI // Makes UI library, see template 1
|       |   CMakeLists.txt
|       |   primativeDesign.cpp
|       |   etc.
|       |   
|       \---Base // Makes UIBase Library, see template 1
|               baseProject.cpp
|               baseProject.h
|               CMakeLists.txt
|               etc.
|               
+---tests // Builds all testing executables, see template 2
|       CMakeLists.txt
|       ParLoad.cpp
|       
\---thirdparty // third party Shortened because this works fine
    |   CMakeLists.txt
    |   
    \---include
            magic_enum.hpp

CMake Templates:

Template 1: Basic library creation

ADD_SUBDIRECTORY(...) # if required
SET(subname_sources
        $all_cpp_files
    )
ADD_LIBRARY(subname ${subname_sources})
LINK_LIBRARIES(subname) # link this to the global space
TARGET_LINK_LIBRARIES(subname -required-libs) # link required libs to this lib
# both of these last two lines seem to be required

Template 2: Test Creation (does not register with CMakeTests)

file(GLOB test_sources RELATIVE ${CMAKE_SOURCE_DIR}/tests *.cpp)
foreach(test ${test_sources})
    string(REPLACE ".cpp" "" testname ${test})
    add_executable(${testname} ${test})
    target_link_libraries(${testname} CreatorBase ThirdParty Creator UI ...)
endforeach(test ${test_sources})

Thank you!

🌐
CMake
cmake.org › pipermail › cmake › 2006-September › 011250.html
[CMake] How to link libraries correctly?
September 22, 2006 - Hello, consider the following CMakeLists.txt ---- file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/bar.c "void foo() { }\n") file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/foo.c "void bar() { }\n") add_library(foo SHARED ${CMAKE_CURRENT_BINARY_DIR}/foo.c) target_link_libraries(foo -lz -lcups -lcrypt) add_library(bar SHARED ${CMAKE_CURRENT_BINARY_DIR}/bar.c) target_link_libraries(bar foo) ---- This: * creates a library 'libfoo.so' and a library 'libbar.so' * 'libfoo.so' depends on lot of other (dynamic) system-libraries * 'libbar.so' depends only on 'libfoo.so' 'cmake' seems to mislink the example above; after m