Riley
Riley

Reputation: 23

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. 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. I downloaded the FreeType library and built it using CMake and my version of GCC. The libfretype.a file is in a subdirectory called junk. The headers are in a subdirectory called 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' .

Thanks in advance for telling me the silly mistake I made.

Upvotes: 1

Views: 1523

Answers (1)

Robert
Robert

Reputation: 2812

It basically means that the implementation of the function png_get_error_ptr is missing. 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. 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.

You need to include these libraries in the 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.

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

Upvotes: 3

Related Questions