g++ 只查找 .lib 文件

g++ only looks for .lib files

我正在使用 Windows 10 和 mingw-w64。我尝试用静态链接库编译程序。我使用了这个命令:g++ main.cpp -Llibs/ -lfoo。但是 MinGW 说它找不到库文件,所以我尝试将 foo.a 重命名为 foo.lib 瞧,编译器找到了 foo.lib。为什么 MinGW 不再看到 *.a 个文件?

来自the documentation

when ld is called with the argument ‘-lxxx’ it will attempt to find, in the first directory of its search path,

  • libxxx.dll.a
  • xxx.dll.a
  • libxxx.a
  • xxx.lib
  • libxxx.lib
  • cygxxx.dll
  • libxxx.dll
  • xxx.dll

或者,您可以将 libs/foo.a 作为参数直接传递给 g++

问题是 mingw 不会查找 <libname>.a 文件,因为它不希望它们存在。

来自文档:

MinGW supports libraries named according to the ".lib" and ".dll" conventions, in addition to the normal "lib.a" convention common on *nix systems

这是因为 windows 没有像 *nix 那样为其库使用 lib 前缀(在 windows 上,共享库 foo 将是 foo.dll 而在 *nix 上它将是 libfoo.so)。我不确定你是如何设法让 .a 缺少 lib 前缀(除非你重命名它)但是你的 .a 文件 应该 lib 前缀(指定时省略 lib 部分,因此 libfoo.a 变为 -lfoo