0

I am working on STM32F4 and trying to compile it using Visaul Studio Code and Cmake .I have go it to successfully compile without using any libraries. And now I am trying to add {arm_math.h} , but somehow I always end up getting undefined refernce to {arm_math.h} Here is my Cmake file . Can anyone pls point out whats wrong in my Cmake file .

Also I am see Cmake complaining about target_link_libraries(${CMAKE_PROJECT_NAME} ${link_LIBS}). I am not sure how to use this with add_library

[cmake] CMake Error at CMakeLists.txt:127 (target_link_libraries):
[cmake]   Cannot specify link libraries for target "test" which is not built
[cmake]   by this project.
cmake_minimum_required(VERSION 3.22)

# Setup compiler settings
set(CMAKE_C_STANDARD                11)
set(CMAKE_C_STANDARD_REQUIRED       ON)
set(CMAKE_C_EXTENSIONS              ON)
set(CMAKE_CXX_STANDARD              20)
set(CMAKE_CXX_STANDARD_REQUIRED     ON)
set(CMAKE_CXX_EXTENSIONS            ON)
set(PROJ_PATH                       ${CMAKE_CURRENT_SOURCE_DIR})
message("Build type: "              ${CMAKE_BUILD_TYPE})

#
# Core project settings
#
project(test)          # Modified
enable_language(C CXX ASM)

#
# Core MCU flags, CPU, instruction set and FPU setup
# Needs to be set properly for your MCU
#
set(CPU_PARAMETERS
    -mthumb

    # This needs attention to properly set for used MCU
    -mcpu=cortex-m4                 # Modified
    -mfpu=fpv4-sp-d16               # Modified
    -mfloat-abi=hard                # Modified
)

# Set linker script
set(linker_script_SRC               ${PROJ_PATH}/STM32F407VGTX_FLASH.ld) # Modified
#Link libraraies
set(link_DIRS ${link_DIRS}
    ${CMAKE_CURRENT_SOURCE_DIR}/Middlewares/ST/ARM/DSP/Lib/
)
set(EXECUTABLE                      ${CMAKE_PROJECT_NAME})

#Link Libraries
set(link_LIBS ${link_LIBS}
    :${PROJ_PATH}Middlewares/ST/ARM/DSP/Lib/libarm_cortexM4lf_math.a
)
add_library(test STATIC libarm_cortexM4lf_math.a)
#
# List of source files to compile
#
set(sources_SRCS                    # Modified
    ${PROJ_PATH}/Core/Src/main.c
    ${PROJ_PATH}/Core/Src/adc.c
    ${PROJ_PATH}/Core/Src/can.c
    ${PROJ_PATH}/Core/Src/dac.c
    ${PROJ_PATH}/Core/Src/gpio.c
    ${PROJ_PATH}/Core/Src/i2c.c
    ${PROJ_PATH}/Core/Src/log.c
    ${PROJ_PATH}/Core/Src/tim.c
    ${PROJ_PATH}/Core/Src/usart.c
    ${PROJ_PATH}/Core/Src/retarget.c
    ${PROJ_PATH}/Core/Src/rpm.c
    ${PROJ_PATH}/Core/Src/stm32f4xx_hal_msp.c
    ${PROJ_PATH}/Core/Src/stm32f4xx_it.c
   # ${PROJ_PATH}/Core/Src/syscalls.c
    ${PROJ_PATH}/Core/Src/sysmem.c
    ${PROJ_PATH}/Core/Src/system_stm32f4xx.c
    ${PROJ_PATH}/Core/Startup/startup_stm32f407vgtx.s
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_can.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dac.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c

)

#
# Include directories
#
set(include_path_DIRS               # Modified
    ${PROJ_PATH}/Core/Inc
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Inc
    ${PROJ_PATH}/Drivers/CMSIS/Device/ST/STM32F4xx/Include
    ${PROJ_PATH}/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy
    ${PROJ_PATH}/Drivers/CMSIS/Include
    ${PROJ_PATH}/Drivers/CMSIS/Device/ST/STM32F4xx/Include
    ${PROJ_PATH}Middlewares/ST/ARM/DSP/Inc

)

#
# Symbols definition
#
set(symbols_SYMB                    # Modified
    "DEBUG"
    "STM32F407xx"
    "USE_HAL_DRIVER"
    "ARM_MATH_CM4"
)

# Linker options
set(linker_OPTS ${linker_OPTS})

# Link directories setup
# Must be before executable is added
link_directories(${CMAKE_PROJECT_NAME} ${link_DIRS})

target_link_libraries(${CMAKE_PROJECT_NAME} ${link_LIBS})

# Executable files
add_executable(${EXECUTABLE} ${sources_SRCS})

# Include paths
target_include_directories(${EXECUTABLE} PRIVATE ${include_path_DIRS})

# Project symbols
target_compile_definitions(${EXECUTABLE} PRIVATE ${symbols_SYMB})

# Compiler options
target_compile_options(${EXECUTABLE} PRIVATE
    ${CPU_PARAMETERS}
    -Wall
    -Wextra
    -Wpedantic
    -Wno-unused-parameter
    # Full debug configuration
    -Og -g3 -ggdb
)

# Linker options
target_link_options(${EXECUTABLE} PRIVATE
    -T${linker_script_SRC}
    ${CPU_PARAMETERS}
    -Wl,-Map=${CMAKE_PROJECT_NAME}.map
    --specs=nosys.specs
    -u _printf_float                # STDIO float formatting support
    -Wl,--start-group
    -lc
    -lm
    -lstdc++
    -lsupc++
    -Wl,--end-group
    -Wl,--print-memory-usage
)

# Execute post-build to print size
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
    COMMAND ${CMAKE_SIZE} $<TARGET_FILE:${EXECUTABLE}>
)

# Convert output to hex and binary
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O ihex $<TARGET_FILE:${EXECUTABLE}> ${EXECUTABLE}.hex
)

# Convert to bin file -> add conditional check?
add_custom_command(TARGET ${EXECUTABLE} POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:${EXECUTABLE}> ${EXECUTABLE}.bin
)

add_definitions(-DARM_MATH_CM4)
rioV8
  • 24,506
  • 3
  • 32
  • 49
ro88
  • 59
  • 6
  • 2
    As far as I know, `test` is a **reserved target name** in CMake. So it should complain on the line `add_library(test ...)`. But even with other target name, your `add_library` call is **wrong**: it should contain **source files**, not a prebuilt library. Probably, you confused it with the IMPORTED library, which denotes a prebuilt library. Have you checked [that question](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) and its answers about linking pre-existed library in CMake? – Tsyvarev Oct 20 '22 at 23:32
  • Yes. I did . Its mostly for global library. I am new to Cmake. I am looking to link the library which is in the same project location . Do I use IMPORTED for the library within the same project directory ? – ro88 Oct 21 '22 at 14:48
  • 1
    Whether the library is **located** inside your project or outside doesn't affect on the way of linking with the library. What affects is whether your project **builds** the library with `add_library` call or not. If your project doesn't build the library with `add_library`, then for link with the library you could define the IMPORTED target. – Tsyvarev Oct 21 '22 at 14:59
  • I got that working . Thanks for the help. I added the full path in target_link_library which I was missing **target_link_libraries(${EXECUTABLE} ${PROJ_PATH}/Middlewares/ST/ARM/DSP/Lib/libarm_cortexM4lf_math.a)** – ro88 Oct 21 '22 at 15:25

0 Answers0