Eclipse CDT MinGW 工具链:如何 link 使用不以 lib 前缀开头的库?

Eclipse CDT MinGW toolchain: how to link with a library that does not begin with lib prefix?

我在链接到我的 Eclipse CDT MinGW 项目中名为 xyz.a 的库时遇到问题。 Eclipse 项目找不到这个库,除非我将它重命名为 libxyz.a

我已添加:

我在网上找到的一些信息:

How do I specify the libraries to be searched by the linker?

    MinGW supports libraries named according to the "<name>.lib" and "<name>.dll" conventions, in addition to the normal "lib<name>.a" convention common on *nix systems. To include libraries named according to any of these conventions, simply add an associated "-l<name>" specification to the compiler command, ensuring it is placed after the name of the module in which the reference appears.
    Note that, if the library is not found in any of the default library search paths, you may also need to insert an appropriate "-L<dir>" switch to specify its location; (it is recommended that you place the "-L<dir>" switch before the "-l<name>" specification which requires it).
    Also note that the library names "lib<name>.a" and "lib<name>.lib" are not equivalent; if you have a library named according to the aberrant "lib<name>.lib" convention, it will not be found by an "-l<name>" specification -- if you cannot rename the library, you must use the form "-llib<name>" instead.

最简单的选择是将库添加到 GCC 的命令行。要使用 CDT 将库(无论名称是什么)添加到 Other object(在项目设置下 -> C/C++构建 -> GCC C 链接器 -> 杂项)

这是我在 GCC 命令行中添加了文件名为 badname.a 的库的屏幕截图。

构建运行时,这是生成的命令行:

Invoking: GCC C Linker
gcc  -o "SO"  ./src/SO.o  /home/me/workspace/SO/badname.a 

注意:上述解决方案的缺点是包含了整个库,而不仅仅是其中引用的对象。您也可以通过将它们添加到同一对话框中的 Linker flags 框中来添加所需的 any 链接器标志。

您可以 link 一个名为 oddname 的库 - 其中 oddname 包括任何 file-extension - 在 Eclipse CDT 中,像这样:

  • 在项目中导航Properties -> C/C++ Build -> 设置 -> 工具设置 -> GCC C++ 链接器 -> .

  • Libraries(-l)面板添加:oddname

  • 如有必要,在库搜索路径(-L)面板中将路径添加到 oddname

  • 确定

此设置会将选项 -l:oddname 添加到生成的 GCC link 命令。 -l:filename-l 选项的形式,表示 常规的 lib 前缀和 {.so|.a} 扩展是 not 隐含的,并且 filename 是要 linked 的库的实际文件名。 Here is the documentation