Visual Studio 代码 cmake 导致错误 ld.exe:无法识别的选项'--major-image-version'



使用windows 10,我正试图为W5100S-EVB-Pico构建示例代码。

我从https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf 9.2节开始,它设置了在windows上构建。我能够使用visual studio代码在那里构建示例文件。

现在我想使用Wiznet。从https://github.com/Wiznet/RP2040-HAT-C开始,我遵循文件getting_started.md中的指示,然后转到vs code,并遵循9.2的模式。我打开文件夹…RP2040-HAT-Cexampleshttpserver选择合适的编译器,并单击build。我得到一个错误,导致我添加这一行cmake_minimum_required(VERSION 3.25)Build现在的结果如下:

[variant] Loaded new set of variants
[kit] Successfully loaded 5 kits from C:UsersrwAppDataLocalCMakeToolscmake-tools-kits.json
[proc] Executing command: "D:Program Files (x86)Arm GNU Toolchain arm-none-eabi12.2 rel1binarm-none-eabi-gcc.exe" -v
[proc] Executing command: "C:Program FilesCMakebincmake.EXE" -Sd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server -Bd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build -G "NMake Makefiles"
[proc] The command: "C:Program FilesCMakebincmake.EXE" -Sd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server -Bd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build -G "NMake Makefiles" exited with code: 1 and signal: null
[main] Configuring folder: server 
[proc] Executing command: "C:Program FilesCMakebincmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug "-DCMAKE_C_COMPILER:FILEPATH=D:Program Files (x86)Arm GNU Toolchain arm-none-eabi12.2 rel1binarm-none-eabi-gcc.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=D:Program Files (x86)Arm GNU Toolchain arm-none-eabi12.2 rel1binarm-none-eabi-g++.exe" -Sd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server -Bd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build -G "NMake Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] CMake Warning (dev) in CMakeLists.txt:
[cmake]   No project() command is present.  The top-level CMakeLists.txt file must
[cmake]   contain a literal, direct call to the project() command.  Add a line of
[cmake]   code such as
[cmake] 
[cmake]     project(ProjectName)
[cmake] 
[cmake]   near the top of the file, but after cmake_minimum_required().
[cmake] 
[cmake]   CMake is pretending there is a "project(Project)" command on the first
[cmake]   line.
[cmake] This warning is for project developers.  Use -Wno-dev to suppress it.
[cmake] 
[cmake] -- The C compiler identification is GNU 12.2.1
[cmake] -- The CXX compiler identification is GNU 12.2.1
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - failed
[cmake] -- Check for working C compiler: D:/Program Files (x86)/Arm GNU Toolchain arm-none-eabi/12.2 rel1/bin/arm-none-eabi-gcc.exe
[cmake] -- Check for working C compiler: D:/Program Files (x86)/Arm GNU Toolchain arm-none-eabi/12.2 rel1/bin/arm-none-eabi-gcc.exe - broken
[cmake] CMake Error at C:/Program Files/CMake/share/cmake-3.25/Modules/CMakeTestCCompiler.cmake:70 (message):
[cmake]   The C compiler
[cmake] 
[cmake]     "D:/Program Files (x86)/Arm GNU Toolchain arm-none-eabi/12.2 rel1/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: D:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build/CMakeFiles/CMakeScratch/TryCompile-2zbfaa
[cmake]     
[cmake]     Run Build Command(s):nmake -f Makefile /nologo cmTC_d4dcefast &&   "C:Program Files (x86)Microsoft Visual Studio2022BuildToolsVCToolsMSVC14.34.31933binHostX86x86nmake.exe"  -f CMakeFilescmTC_d4dce.dirbuild.make /nologo -L                  CMakeFilescmTC_d4dce.dirbuild
[cmake]     Building C object CMakeFiles/cmTC_d4dce.dir/testCCompiler.c.obj
[cmake]         "D:Program Files (x86)ARMGNU~112DBAB~1.2REbinAR19DD~1.EXE"    -o CMakeFilescmTC_d4dce.dirtestCCompiler.c.obj -c D:cygwinhomerwholdenRP2040-HAT-CexampleshttpserverbuildCMakeFilesCMakeScratchTryCompile-2zbfaatestCCompiler.c
[cmake]     Linking C executable cmTC_d4dce.exe
[cmake]         "C:Program FilesCMakebincmake.exe" -E rm -f CMakeFilescmTC_d4dce.dir/objects.a
[cmake]         "D:Program Files (x86)ARMGNU~112DBAB~1.2REbinARM-NO~2.EXE" qc CMakeFilescmTC_d4dce.dir/objects.a @CMakeFilescmTC_d4dce.dirobjects1
[cmake]         "D:Program Files (x86)ARMGNU~112DBAB~1.2REbinAR19DD~1.EXE" -Wl,--whole-archive CMakeFilescmTC_d4dce.dir/objects.a -Wl,--no-whole-archive -o cmTC_d4dce.exe -Wl,--out-implib,libcmTC_d4dce.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFilescmTC_d4dce.dirlinkLibs.rsp
[cmake]     d:/program files (x86)/armgnu~1/12dbab~1.2re/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: unrecognized option '--major-image-version'
[cmake]     d:/program files (x86)/armgnu~1/12dbab~1.2re/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: use the --help option for usage information
[cmake]     collect2.exe: error: ld returned 1 exit status
[cmake]     NMAKE : fatal error U1077: '"D:Program Files (x86)ARMGNU~112DBAB~1.2REbinAR19DD~1.EXE"' : return code '0x1'
[cmake]     Stop.
[cmake]     NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2022BuildToolsVCToolsMSVC14.34.31933binHostX86x86nmake.exe"' : return code '0x2'
[cmake]     Stop.
[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
[cmake] 
[cmake] 
[cmake] -- Configuring incomplete, errors occurred!
[cmake] See also "D:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "D:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build/CMakeFiles/CMakeError.log".
[proc] The command: "C:Program FilesCMakebincmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug "-DCMAKE_C_COMPILER:FILEPATH=D:Program Files (x86)Arm GNU Toolchain arm-none-eabi12.2 rel1binarm-none-eabi-gcc.exe" "-DCMAKE_CXX_COMPILER:FILEPATH=D:Program Files (x86)Arm GNU Toolchain arm-none-eabi12.2 rel1binarm-none-eabi-g++.exe" -Sd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server -Bd:/cygwin/home/rw/holden/RP2040-HAT-C/examples/http/server/build -G "NMake Makefiles" exited with code: 1 and signal: null

一些谷歌搜索也让我添加了这行set(CMAKE_SYSTEM_NAME Generic)到CMakeLists.txt,现在看起来像

set(TARGET_NAME w5x00_http_server)
cmake_minimum_required(VERSION 3.25)
set(CMAKE_SYSTEM_NAME Generic)
add_executable(${TARGET_NAME}
${TARGET_NAME}.c
)
target_link_libraries(${TARGET_NAME} PRIVATE
pico_stdlib
hardware_spi
hardware_dma
ETHERNET_FILES
IOLIBRARY_FILES
HTTPSERVER_FILES
)
pico_enable_stdio_usb(${TARGET_NAME} 1)
pico_enable_stdio_uart(${TARGET_NAME} 0)
pico_add_extra_outputs(${TARGET_NAME})

我试图修复损坏的编译器错误,这使事情变得更糟,并返回CMakeLists.txt到它以前的状态并没有回到上面所示的。有某种"干净"的功能我可以做吗?

EDIT2: Tsyvarev在这个答案下的讨论中提出了一个非常好的观点。因为你的CMakeLists.txt缺少project(...)行,它很可能在第一行被CMake隐式调用。特定的变量,如CMAKE_CXX_COMPILERCMAKE_SYSTEM_NAME,需要在project()行之前设置。值得尝试相应地更新CMakeLists.txt。然而,第一个EDIT仍然存在(这也是tsyvarev的发现)。您很可能试图从不足的CMakeLists.txt

构建。编辑:我没有注意到Tsyvarev在评论中提到的内容,在查看了适当的CMakeLists.txt之后,我不得不同意他的观点. 您肯定也遗漏了一些目标,并且您可能没有给我们完整的(新的)错误信息。这里缺少的目标是(例如):
ETHERNET_FILES
IOLIBRARY_FILES
HTTPSERVER_FILES

您很可能也会遇到与此相关的错误。然而,最初的错误实际上与set(CMAKE_SYSTEM_NAME Generic)有关。


原创文章:

缺失的cmake_minimum_required()应该只是抛出一个警告,而不是一个错误IIRC。

CMake中唯一存在的clean函数是rm [path-to-build-directory]。当您发出cmake -S. -Bbuild命令(或类似命令)时,您将创建一个构建文件夹,其中CMake生成编译项目所需的文件。

关于你的问题。set(CMAKE_SYSTEM_NAME Generic)应该添加到工具链文件或者至少是CMakeLists.txt中的第一个变量集。我相信(这是一个大胆的猜测),它应该在cmake_minimum_required()之前,这对于CMAKE_CXX_COMPILER等变量也是一样的。

我的建议是:

  1. set(CMAKE_SYSTEM_NAME Generic)移动到CMakeLists.txt文件的顶部
  2. 删除build文件夹
  3. 运行cmake并重新构建。

如果以上没有帮助,那么我需要你为我澄清一下:

  • 我回到了VSCode"你的意思是你通过开发者提示打开它,就像你链接的书一样。
  • 你传递什么命令来运行cmake/build它,即如果你从Developer powershell/命令提示符运行会发生什么:cmake -S. -BbuildForStackOverflow -DCMAKE_SYSTEM_NAME=Generic,然后从这些生成的文件构建它?

我的错误是打开'server'文件夹并假设所有东西都在那里。我从来都不知道"工具链文件"在哪里。解决方案是首先在VS code中打开文件夹RP2040-HAT-C并选择适当的编译器。VS code然后使用该目录下的.txt文件进行初始化cmake。现在,在VS code的状态行,我可以点击"所有"旁边的构建,选择我想要的目标(服务器)和构建。成功!

感谢所有帮助我的人。

最新更新