我在ubuntu22.04环境下解压最新版的MRS_Toolchain,并在我的工程中使用CMake编译项目
这是我的代码链接:https://gitee.com/netube/v208_ble
目前可以顺利通过编译阶段,但到了链接的时候出现了一些错误,内容如下:
/home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld: /home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/lib/rv32imac/ilp32/libg.a(lib_a-exit.o): in function `.L0 ':
exit.c:(.text.exit+0x20): undefined reference to `_exit'
/home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld: /home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/lib/rv32imac/ilp32/crt0.o: in function `.L0 ':
(.text+0x2e): undefined reference to `main'
/home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld: CMakeFiles/ch32v208.dir/device/Debug/debug.c.o: in function `_write':
/home/netube/mywork/v208_ble/device/Debug/debug.c:171: undefined reference to `_heap_end'
/home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/../lib/gcc/riscv-none-embed/8.2.0/../../../../riscv-none-embed/bin/ld: /home/netube/mywork/v208_ble/device/Debug/debug.c:171: undefined reference to `_heap_end'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ch32v208.dir/build.make:465: ch32v208] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/ch32v208.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
最后附上CMakeLists.txt的内容
cmake_minimum_required(VERSION 3.12)
set(PROJECT_NAME "ch32v208")
project(${PROJECT_NAME})
# 添加源文件
file(GLOB USER_SOURCES user/*.c)
file(GLOB MSP_SOURCES msp/*.c)
file(GLOB BSP_SOURCES bsp/*.c)
file(GLOB APP_SOURCES app/*.c)
file(GLOB DEV_CORE_SOURCES device/Core/*.c)
file(GLOB DEV_DEBUG_SOURCES device/Debug/*.c)
file(GLOB DEV_PER_SOURCES device/Peripheral/src/*.c)
# 设置可执行目标与源码
add_executable(${PROJECT_NAME}
? ? device/Startup/startup_ch32v20x_D8W.S
? ? ${USER_SOURCES}
? ? ${MSP_SOURCES}
? ? ${BSP_SOURCES}
? ? ${APP_SOURCES}
? ? ${DEV_CORE_SOURCES}
? ? ${DEV_DEBUG_SOURCES}
? ? ${DEV_PER_SOURCES}
)
# 添加头文件目录
include_directories(
? ? PRIVATE
? ? user
? ? msp
? ? bsp
? ? app
? ? device/Core
? ? device/Debug
? ? device/Peripheral/inc
? ? lib
? ? ${OpenCV_INCLUDE_DIRS}
)
# 设置编译宏
add_compile_definitions(CH32V20x_D8W)
# 设置交叉工具链
set(CMAKE_C_COMPILER /home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/riscv-none-embed-gcc)
set(CMAKE_CXX_COMPILER /home/netube/mywork/MRS_Toolchain_Linux_x64_V1.80/RISC-V_Embedded_GCC/bin/riscv-none-embed-g++)
# 设置DEBUG与优化等级
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
# 设置语言标准
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
# 编译参数
# 设定指令集 设定ABI 小数据段限制 函数调用期间保存和恢复寄存器的状态
add_compile_options(-march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore)
# 不限制错误/警告消息的长度 char为有符号类型 函数放在单独节 数据放在单独节 禁止合并同名全局变量
add_compile_options(-fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common)
# 链接参数
# 设置sdk提供的ld文件
add_link_options(-T device/Ld/Link.ld)
# 设定指令集 设定ABI 删除未使用的节 打印内存使用情况 生成MAP文件 使用限制C库和系统调用的规范文件
add_link_options(-march=rv32imac -mabi=ilp32 -nostartfiles -Xlinker --gc-sections -Wl,--print-memory-usage -Wl,-Map,${PROJECT_NAME}.map --specs=nano.specs --specs=nosys.specs)
# 编译后自动输出 ELF HEX BIN
# 添加自定义命令以生成ELF、HEX和BIN文件
add_custom_command(
? ? TARGET ${PROJECT_NAME} POST_BUILD
? ? COMMAND ${CMAKE_OBJCOPY} -O elf32-littleriscv $${PROJECT_NAME}> ${CMAKE_BINARY_DIR}/output/${PROJECT_NAME}.elf
? ? COMMAND ${CMAKE_OBJCOPY} -O ihex $${PROJECT_NAME}> ${CMAKE_BINARY_DIR}/output/${PROJECT_NAME}.hex
? ? COMMAND ${CMAKE_OBJCOPY} -O binary $${PROJECT_NAME}> ${CMAKE_BINARY_DIR}/output/${PROJECT_NAME}.bin
)