CMake 项目:未定义对“xxx”的引用

cmake project: undefined reference to 'xxx'

提问人:Cjsah 提问时间:10/21/2022 最后编辑:the busybeeCjsah 更新时间:10/21/2022 访问量:89

问:

我正在尝试编译 CLion 项目。

the Project looks like this

和 CMakeList.txt

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.23)

# specify cross-compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER  arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# project settings
project(main_board C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)

#Uncomment for hardware floating point
add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)

#Uncomment for software floating point
#add_compile_options(-mfloat-abi=soft)

add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)

# uncomment to mitigate c++17 absolute addresses warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")

# Enable assembler files preprocessing
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
    message(STATUS "Maximum optimization for speed")
    add_compile_options(-Ofast)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    message(STATUS "Maximum optimization for speed, debug info included")
    add_compile_options(-Ofast -g)
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
    message(STATUS "Maximum optimization for size")
    add_compile_options(-Os)
else ()
    message(STATUS "Minimal optimization, debug info included")
    add_compile_options(-Og -g)
endif ()

include_directories(Core/Inc USB_DEVICE/App USB_DEVICE/Target Drivers/STM32F4xx_HAL_Driver/Inc Drivers/STM32F4xx_HAL_Driver/Inc/Legacy Middlewares/Third_Party/FreeRTOS/Source/include Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2 Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F Middlewares/ST/STM32_USB_Device_Library/Core/Inc Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc Drivers/CMSIS/Device/ST/STM32F4xx/Include Drivers/CMSIS/Include Middlewares/ST/ARM/DSP/Inc)
include_directories(Core/app/Inc Core/app/protocol Core/bsp/Inc Core/components/algorithm Core/components/algorithm/Include Core/components/controller Core/components/devices Core/components/support)

add_definitions(-DUSE_HAL_DRIVER -DSTM32F407xx -DARM_MATH_CM4 -D__FPU_PRESENT=1U -DARM_MATH_MATRIX_CHECK -DARM_MATH_ROUNDING)
add_definitions(-Wno-attributes) # ignore warning: 'packed' attribute ignored'

file(GLOB_RECURSE SOURCES "startup/*.*" "Library/*.*" "Middlewares/*.*" "Drivers/*.*" "USB_DEVICE/*.*" "Core/*.*")

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F407IGHx_FLASH.ld)

add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})

add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)

add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
        COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
        COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")

一旦我尝试将 和 包含在我的 CLion 项目中,我就会得到大量: main.cpp:(.text+0x23c): undefined reference to glViewport 错误消息,并且在基本目录中构建我的 main.cpp 失败。arm_math.h

一旦我尝试调用中定义的函数。它会有很多错误:arm_math.hundefined reference to 'xxx'

同时,我的另一个项目也有同样的问题。它无法正确使用静态库文件。

FAILED: main_board.elf 
cmd.exe /C "cd . && C:\PROGRA~2\GNUARM~1\102021~1.07\bin\AR19DD~1.EXE -O3 -DNDEBUG -mfloat-abi=hard -mfpu=fpv4-sp-d16 -Wl,-gc-sections,--print-memory-usage,-Map=C:/Users/Cjsah/Desktop/rm/final/main_board/build/main_board.map -mcpu=cortex-m4 -mthumb -mthumb-interwork -T C:/Users/Cjsah/Desktop/rm/final/main_board/STM32F407IGHx_FLASH.ld @CMakeFiles\main_board.elf.rsp -o main_board.elf  && cmd.exe /C "cd /D C:\Users\Cjsah\Desktop\rm\final\main_board\build && arm-none-eabi-objcopy -Oihex C:/Users/Cjsah/Desktop/rm/final/main_board/build/main_board.elf C:/Users/Cjsah/Desktop/rm/final/main_board/build/main_board.hex && arm-none-eabi-objcopy -Obinary C:/Users/Cjsah/Desktop/rm/final/main_board/build/main_board.elf C:/Users/Cjsah/Desktop/rm/final/main_board/build/main_board.bin""
c:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: CMakeFiles/main_board.elf.dir/Core/app/Src/chassis_behaviour.c.obj: in function `chassis_behaviour_control_set':
chassis_behaviour.c:(.text.chassis_behaviour_control_set+0x6c): undefined reference to `arm_sin_f32'
c:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: CMakeFiles/main_board.elf.dir/Core/app/Src/chassis_task.c.obj: in function `chassis_task':
chassis_task.c:(.text.chassis_task+0x2da): undefined reference to `arm_sin_f32'
c:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: chassis_task.c:(.text.chassis_task+0x2ec): undefined reference to `arm_cos_f32'
c:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: CMakeFiles/main_board.elf.dir/Core/app/Src/gimbal_task.c.obj: in function `gimbal_feedback_update.constprop.0':
gimbal_task.c:(.text.gimbal_feedback_update.constprop.0+0x7a): undefined reference to `arm_cos_f32'
c:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: gimbal_task.c:(.text.gimbal_feedback_update.constprop.0+0x8c): undefined reference to `arm_sin_f32'

看起来,CLion 只是在我自己的程序中没有链接找到正确的链接。如果我将其包含在内。问题是由编译器引起的,还是因为我写的语句少了?arm_cortexM4l_math.libarm_math.h

c gcc arm clion 未定义引用

评论

0赞 the busybee 10/21/2022
您不必将适当的库添加到您的 CMakeList.txt 中吗?
1赞 Stephen Newell 10/21/2022
这回答了你的问题吗?什么是未定义的引用/未解析的外部符号错误,如何解决?

答: 暂无答案