我在ubuntu22.04環(huán)境下解壓最新版的MRS_Toolchain,并在我的工程中使用CMake編譯項(xiàng)目
這是我的代碼鏈接:https://gitee.com/netube/v208_ble
目前可以順利通過(guò)編譯階段,但到了鏈接的時(shí)候出現(xiàn)了一些錯(cuò)誤,內(nèi)容如下:
/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的內(nèi)容
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)
# 設(shè)置可執(zhí)行目標(biāo)與源碼
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}
)
# 設(shè)置編譯宏
add_compile_definitions(CH32V20x_D8W)
# 設(shè)置交叉工具鏈
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++)
# 設(shè)置DEBUG與優(yōu)化等級(jí)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
# 設(shè)置語(yǔ)言標(biāo)準(zhǔn)
set(CMAKE_C_STANDARD 99)
set(CMAKE_CXX_STANDARD 11)
# 編譯參數(shù)
# 設(shè)定指令集 設(shè)定ABI 小數(shù)據(jù)段限制 函數(shù)調(diào)用期間保存和恢復(fù)寄存器的狀態(tài)
add_compile_options(-march=rv32imac -mabi=ilp32 -msmall-data-limit=8 -msave-restore)
# 不限制錯(cuò)誤/警告消息的長(zhǎng)度 char為有符號(hào)類型 函數(shù)放在單獨(dú)節(jié) 數(shù)據(jù)放在單獨(dú)節(jié) 禁止合并同名全局變量
add_compile_options(-fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common)
# 鏈接參數(shù)
# 設(shè)置sdk提供的ld文件
add_link_options(-T device/Ld/Link.ld)
# 設(shè)定指令集 設(shè)定ABI 刪除未使用的節(jié) 打印內(nèi)存使用情況 生成MAP文件 使用限制C庫(kù)和系統(tǒng)調(diào)用的規(guī)范文件
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)
# 編譯后自動(dòng)輸出 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
)