Skip to content

CMake Error, "Can't link to the standard math library" on Raspberry Pi Pico

Summary

When attempting to use Eigen with the Raspberry Pi Pico SDK, CMake configuration is halted when Eigen attempts to find out how to link the standard math library.

When compiling the trivial program used in the test, there are numerous linker errors unless the pico_stdlib library is added as a library to the test program with target_link_libraries.

Environment

  • Operating System : N/A
  • Architecture : Arm Cortex M0+ / ARMv6-M
  • Eigen Version : 3.4
  • Compiler Version : gcc-arm-non-eabi 12.2
  • Compile Flags : unsure
  • Vector Extension : unsure

Minimal Example

// test.cpp
#include<cmath>
int main(int argc, char **){
  return int(std::sin(double(argc)) + std::log(double(argc)));
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.13)

include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)

project(test_project C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)
pico_sdk_init()

add_subdirectory($ENV{SYGALDRY_ROOT}/dependencies/eigen eigenbuild) # comment out this line and target "test" can compile no problem

add_executable(test
    test.cpp
)

pico_enable_stdio_usb(test 1)

pico_add_extra_outputs(test)

target_link_libraries(test pico_stdlib) # without this line, target "test" cannot compile

Steps to reproduce

  1. Procure a Raspberry Pi Pico development board
  2. Follow the getting started guide, e.g. install the GNU arm compiler, cmake, etc., and clone the pico SDK
  3. Attempt to compile the minimum example as seen above; CMake is unable to configure
  4. Remove the link add_subdirectory(...) where Eigen is added to the configure process and compile again; compilation is successful
  5. Remove the link target_link_libraries(test pico_stdlib); linking fails as numerous low-level standard library symbols are undefined

Relevant logs

CMake Error at /home/tw/tw/repos/sygaldry/dependencies/eigen/CMakeLists.txt:108 (message):
  Can't link to the standard math library.  Please report to the Eigen
  developers, telling them about your platform.

Anything else that might help

I tried to see if I could resolve the issue by adding another branch to FindStandardMathLibrary.cmake resembling the following, but this test also failed. I presume that I misunderstand either the way the test works, the way pico_stdlib is linked when added to a target with target_link_libraries, or most likely both.

  set(CMAKE_REQUIRED_LIBRARIES "pico_stdlib")
  CHECK_CXX_SOURCE_COMPILES(
    "${find_standard_math_library_test_program}"
    standard_math_library_linked_to_as_pico_stdlib)

  if(standard_math_library_linked_to_as_pico_stdlib)

    # the test program linked successfully when linking to the 'pico_stdlib' library
    set(STANDARD_MATH_LIBRARY "pico_stdlib")
    set(STANDARD_MATH_LIBRARY_FOUND TRUE)

  else()

    # the test program still doesn't link successfully
    set(STANDARD_MATH_LIBRARY_FOUND FALSE)

  endif()

The Pico SDK CMake files define a CMake variable PICO_SDK that can be used to detect the platform. Perhaps this can be useful when working around the issue described.