CMake C 项目无法在 arm-none-eabi 工具链下编译

CMake C project does not compile under arm-none-eabi toolchain

我目前正在研究传感器的驱动程序。当我进行设置时,一切正常,我可以编译我的代码。但是当我写完驱动程序并想测试时,CMake 给了我很多我不太明白的错误。这是输出(pastebin):

CMakeError.log: https://pastebin.com/2jSRqCCs

CMakeOutput.log: https://pastebin.com/2Sy4pdXb

CMakeCache.txt: https://pastebin.com/KpFCTexM

我使用操作系统Windows10,用arm-none-eabi-gcc[=31编译代码=].对于 Makefile 编译,我使用 mingw32-make.

提前致谢!

编辑

CMakeLists.txt

# CMake minimum version
cmake_minimum_required(VERSION 3.12)

project(mpu9250_driver
    VERSION 1.0 
    DESCRIPTION "MPU9250 general purpose driver"
    LANGUAGES C CXX
)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
enable_language(C CXX)

add_library(mpu9250_driver STATIC
    src/MPU9250.c
)

target_include_directories(mpu9250_driver PUBLIC
    ./include
)

CMake 输出:

[main] Building folder: driver-mpu9250 
[main] The folder containing the CMake cache is missing. The cache will be regenerated.
[main] Configuring folder: driver-mpu9250 
[proc] Executing command: "G:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=MinSizeRel "-DCMAKE_C_COMPILER:FILEPATH=G:\Program Files (x86)\GNU Arm Embedded Toolchain 2021.07\bin\arm-none-eabi-gcc.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=G:\Program Files (x86)\GNU Arm Embedded Toolchain 2021.07\bin\arm-none-eabi-g++.exe" "-Hc:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250" "-Bc:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build" -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.3.1
[cmake] -- The CXX compiler identification is GNU 10.3.1
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - failed
[cmake] -- Check for working C compiler: G:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.07/bin/arm-none-eabi-gcc.exe
[cmake] -- Check for working C compiler: G:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.07/bin/arm-none-eabi-gcc.exe - broken
[cmake] CMake Error at G:/Program Files/CMake/share/cmake-3.21/Modules/CMakeTestCCompiler.cmake:69 (message):
[cmake]   The C compiler
[cmake] 
[cmake]     "G:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.07/bin/arm-none-eabi-gcc.exe"
[cmake] 
[cmake]   is not able to compile a simple test program.
[cmake] 
[cmake]   It fails with the following output:
[cmake] 
[cmake]     Change Dir: C:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build/CMakeFiles/CMakeTmp
[cmake]     
[cmake]     Run Build Command(s):G:/VSARM/mingw/bin/mingw32-make.exe -f Makefile cmTC_db21f/fast && G:/VSARM/mingw/bin/mingw32-make.exe  -f CMakeFiles\cmTC_db21f.dir\build.make CMakeFiles/cmTC_db21f.dir/build
[cmake]     mingw32-make.exe[1]: Entering directory 'C:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build/CMakeFiles/CMakeTmp'
[cmake]     Building C object CMakeFiles/cmTC_db21f.dir/testCCompiler.c.obj
[cmake]     G:\PROGRA~2\GNUARM~12021~1.07\bin\AR19DD~1.EXE    -o CMakeFiles\cmTC_db21f.dir\testCCompiler.c.obj -c "C:\Users\User\Documents\Visual Studio Code\pico\driver-mpu9250\build\CMakeFiles\CMakeTmp\testCCompiler.c"
[cmake]     Linking C executable cmTC_db21f.exe
[cmake]     "G:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_db21f.dir\link.txt --verbose=1
[cmake]     "G:\Program Files\CMake\bin\cmake.exe" -E rm -f CMakeFiles\cmTC_db21f.dir/objects.a
[cmake]     G:\PROGRA~2\GNUARM~12021~1.07\bin\ARM-NO~2.EXE qc CMakeFiles\cmTC_db21f.dir/objects.a @CMakeFiles\cmTC_db21f.dir\objects1.rsp
[cmake]     G:\PROGRA~2\GNUARM~12021~1.07\bin\AR19DD~1.EXE -Wl,--whole-archive CMakeFiles\cmTC_db21f.dir/objects.a -Wl,--no-whole-archive -o cmTC_db21f.exe -Wl,--out-implib,libcmTC_db21f.dll.a -Wl,--major-image-version,0,--minor-image-version,0 
[cmake]     g:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: unrecognized option '--major-image-version'
[cmake]     g:/progra~2/gnuarm~1/102021~1.07/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld.exe: use the --help option for usage information
[cmake]     collect2.exe: error: ld returned 1 exit status
[cmake]     CMakeFiles\cmTC_db21f.dir\build.make:98: recipe for target 'cmTC_db21f.exe' failed
[cmake]     mingw32-make.exe[1]: *** [cmTC_db21f.exe] Error 1
[cmake]     mingw32-make.exe[1]: Leaving directory 'C:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build/CMakeFiles/CMakeTmp'
[cmake]     Makefile:126: recipe for target 'cmTC_db21f/fast' failed
[cmake]     mingw32-make.exe: *** [cmTC_db21f/fast] Error 2
[cmake]     
[cmake]     
[cmake] 
[cmake]   
[cmake] 
[cmake]   CMake will not be able to correctly generate this project.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:4 (project)
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "C:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "C:/Users/User/Documents/Visual Studio Code/pico/driver-mpu9250/build/CMakeFiles/CMakeError.log"
https://pastebin.com/hz5ARaiY

您需要使用 CMAKE_TOOLCHAIN_FILE 而不是没有任何 sysroot 等的 hackish 编译器更改...

-DCMAKE_C_COMPILER:FILEPATH=G:\Program Files (x86)\GNU Arm Embedded Toolchain 2021.07\bin\arm-none-eabi-gcc.exe"

参考:https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html