简体   繁体   English

使用 GCC 链接到 static 库时遇到问题

[英]Having trouble linking to a static library with GCC

I'm working on a big project with glfw but I have been stymied trying to link to a static library with gcc, hence this toy example.我正在使用glfw进行一个大项目,但我一直在尝试使用 gcc 链接到 static 库,因此这个玩具示例受到了阻碍。 I'm not sure why I'm having so much trouble with such a simple thing.我不知道为什么我在这么简单的事情上遇到这么多麻烦。

This is the extent of my source code:这是我的源代码的范围:

#include <stdio.h>
#include <ft2build.h>
#include FT_FREETYPE_H 

int main(){
    FT_Library ft;
    if (FT_Init_FreeType(&ft))
    {
        printf("ERROR::FREETYPE: Could not init FreeType Library\n");
    }

    FT_Face face;
    if (FT_New_Face(ft, "fonts/arial.ttf", 0, &face))
    {
        printf("ERROR::FREETYPE: Failed to load font\n"); 
    }
    return 0;
}

I'm running Linux Mint.我正在运行 Linux Mint。 I downloaded the FreeType library and built it using CMake and my version of GCC.我下载了 FreeType 库并使用 CMake 和我的 GCC 版本构建它。 The libfretype.a file is in a subdirectory called junk . libfretype.a文件位于名为junk的子目录中。 The headers are in a subdirectory called include .标头位于名为include的子目录中。

We compile it with the following:我们用以下代码编译它:

gcc -Wall -Wextra -g -v -Iinclude -Ljunk vex.c -lfreetype -o vex

and I get a ton of errors like sfnt.c:(.text+0x218): undefined reference to 'png_get_error_ptr' .我得到了大量的错误,比如sfnt.c:(.text+0x218): undefined reference to 'png_get_error_ptr'

Thanks in advance for telling me the silly mistake I made.提前感谢您告诉我我犯的愚蠢错误。

It basically means that the implementation of the function png_get_error_ptr is missing.这基本上意味着缺少 function png_get_error_ptr的实现。 So, the compiler could not generate an executable because some code is missing.因此,编译器无法生成可执行文件,因为缺少某些代码。

The function png_get_error_ptr is implemented in a library named libpng . function png_get_error_ptr在名为libpng的库中实现。 Sometimes, some libraries have some dependencies on another project, in the general case, you need to include all the dependencies to your build to resolve the linker errors .有时,某些库对另一个项目有一些依赖项,在一般情况下,您需要将所有依赖项包含到您的构建中以解决linker errors

You need to include these libraries in the linker:您需要在 linker 中包含这些库:

gcc -Wall -Wextra -g -v -Iinclude -Ljunk vex.c -lfreetype -lpng -lz -o vex 
                                                           ^     ^

-lz is to link against zlib because libpng rely on zlib if I remember correctly. -lz是与zlib链接,因为如果我没记错的话, libpng依赖于zlib

http://libpng.org/pub/png/libpng-manual.txt http://libpng.org/pub/png/libpng-manual.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM