gpt4 book ai didi

c++ - 如何在Windows上使用MinGW编译C++?

转载 作者:行者123 更新时间:2023-12-02 10:56:21 27 4
gpt4 key购买 nike

我收到以下错误cannot find -lstdc++collect2.exe: error: ld returned 1 exit status这是一个简单的Hello World程序。g++ version返回

g++ (MinGW.org GCC Build-2) 9.2.0
g++ -v <filename.cpp>返回
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-9.2.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-static --enable-shared --enable-threads --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --enable-libgomp --disable-libvtv --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --disable-build-format-warnings --prefix=/mingw --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --enable-nls --with-pkgversion='MinGW.org GCC Build-2'
Thread model: win32
gcc version 9.2.0 (MinGW.org GCC Build-2)
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/cc1plus.exe -quiet -v -iprefix c:\mingw\bin\../lib/gcc/mingw32/9.2.0/ hw.cpp -quiet -dumpbase hw.cpp -mtune=generic -march=i586 -auxbase hw -version -o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/mingw32"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include/c++/backward"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include"
ignoring duplicate directory "/mingw/lib/gcc/mingw32/9.2.0/../../../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../include"
ignoring duplicate directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/include-fixed"
ignoring nonexistent directory "c:/mingw/lib/gcc/../../lib/gcc/mingw32/9.2.0/../../../../mingw32/include"
ignoring duplicate directory "/mingw/include"
#include "..." search starts here:
#include <...> search starts here:
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/mingw32
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include/c++/backward
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/../../../../include
c:\mingw\bin\../lib/gcc/mingw32/9.2.0/include-fixed
End of search list.
GNU C++14 (MinGW.org GCC Build-2) version 9.2.0 (mingw32)
compiled by GNU C version 9.2.0, GMP version 6.1.2, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: cb31672760a9c1b42d1ddb86616116cd
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/as.exe -v -o C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o C:\Users\move78~1\AppData\Local\Temp\ccQus9Kp.s
GNU assembler version 2.32 (mingw32) using BFD version (GNU Binutils) 2.32
COMPILER_PATH=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/;c:/mingw/bin/../libexec/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/
LIBRARY_PATH=c:/mingw/bin/../lib/gcc/mingw32/9.2.0/;c:/mingw/bin/../lib/gcc/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib/;c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../
COLLECT_GCC_OPTIONS='-v' '-shared-libgcc' '-mtune=generic' '-march=i586'
c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/collect2.exe -plugin c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/liblto_plugin-0.dll -plugin-opt=c:/mingw/bin/../libexec/gcc/mingw32/9.2.0/lto-wrapper.exe -plugin-opt=-fresolution=C:\Users\move78~1\AppData\Local\Temp\ccxROszG.res -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -plugin-opt=-pass-through=-ladvapi32 -plugin-opt=-pass-through=-lshell32 -plugin-opt=-pass-through=-luser32 -plugin-opt=-pass-through=-lkernel32 -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lmingw32 -plugin-opt=-pass-through=-lmingwex -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lmoldname -plugin-opt=-pass-through=-lmsvcrt -Bdynamic -u ___register_frame_info -u ___deregister_frame_info c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0 -Lc:/mingw/bin/../lib/gcc -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/lib -Lc:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../.. C:\Users\move78~1\AppData\Local\Temp\cctjBjko.o -lstdc++ -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lmingwex -lmingw32 -lmingwex -lgcc_s -lgcc -lmoldname -lmsvcrt c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++
collect2.exe: error: ld returned 1 exit status

最佳答案

您需要构建3件东西:

  • 您的工具(如g++.exe)需要位于PATH中(或在调用它们时通过其完整路径指定,例如C:\mingw\mingw32\bin\g++.exe)。
  • 编译器需要能够找到包含文件。如果需要,可以使用-I标志(或使用C_INCLUDE_PATH(对于C)和CPLUS_INCLUDE_PATH(对于C++)环境变量)添加包含路径(指向文件夹,而不是实际文件)。
  • 链接器需要能够找到您的库(例如libstdc++.a)。如果需要,您可以使用-L标志添加库路径(指向文件夹,而不是实际文件)(或者可以使用LIBRARY_PATH环境变量)。

  • 如果使用MSYS2 shell,则应该已经设置好这些东西。
    来自 http://winlibs.com/的独立构建会查找相对位置,并且只要将其解压缩到磁盘上就可以使用。
    您没有提到从哪个 shell 程序运行命令(命令提示符,PowerShell,MSYS),但是重要的是,无论您使用哪种 shell 程序,都必须正确遵守以上内容。
    据我所知 http://winlibs.com/在MSYS Shell,命令提示符和PowerShell中都可以工作(尽管在使用PowerShell时,必须将以 -开头的参数放在引号中)。

    关于c++ - 如何在Windows上使用MinGW编译C++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63209477/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com