MINGW64中找不到GCC的解决方法
一、问题描述
我在MINGW64下运行了下面的命令,想要配置ffmpeg以便后续进行编译:
./configure --prefix=../lib --disable-static --enable-shared
不幸的是返回了这样一条提示
33132@Midsummer MINGW64 /d/ToolChainlib/FFmpeg/ffmpeg-4.4.4
$ ./configure --prefix=../lib --disable-static --enable-shared
gcc is unable to create an executable file.
If gcc is a cross-compiler, use the --enable-cross-compile option.
Only do this if you know what cross compiling means.
C compiler test failed.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
ffmpeg-user@ffmpeg.org mailing list or IRC #ffmpeg on irc.libera.chat.
Include the log file "ffbuild/config.log" produced by configure as this will help
solve the problem.
导致上述提示出现的直接原因是MSYS2 MINGW64
环境没找到GCC
编译器,但是很奇怪,我在windows自带的控制台中运行能找到GCC编译器:
C:\Users\33132>gcc --version
gcc (Rev3, Built by MSYS2 project) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
二、解决方案
查阅了一些资料发现MSYS2 MINGW64
环境和windows本身的环境变量不通用,那么我们需要手动指定该环境的环境变量,以下是操作步骤:
-
寻找GCC编译器位置
C:\Users\33132>where gcc D:\Software\Compiler\ucrt64\bin\gcc.exe
-
添加环境变量
上面得到了GCC编译器的位置是:
D:\Software\Compiler\ucrt64\bin\gcc.exe
那么我们将整个工具链的目录设置为环境变量,其工具链目录转换到
MSYS2 MINGW64
环境中,是下面这样的:/d/Software/Compiler/ucrt64/bin
MSYS2 MINGW64
环境其实是提供了一个类 linux 的 shell 环境,因此我们各种操作和linux是差不多的,那么我们用记事本打开配置文件notepad.exe ~/.bashrc
在文档的最后面添加一行,表示添加工具链的目录为环境变量:
export PATH="/d/Software/Compiler/ucrt64/bin:$PATH"
然后
Ctrl+S
快捷键保存,然后关闭记事本(不关闭记事本,命令行没法继续执行)。之后使用以下命令让配置文件立即生效:source ~/.bashrc
-
测试效果
33132@Midsummer MINGW64 /d/ToolChainlib/FFmpeg/ffmpeg-4.4.4 $ gcc --version gcc.exe (Rev3, Built by MSYS2 project) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
出现以上类似的结果,则表示gcc编译器找到了,至此,over。