C cannot link against libfreetype...

Hi,

I have rather an unusual issue I seem to be facing when trying to link with the freetype library...

Bash:
[ahhyes@devbox ~]$ ld -lfreetype
ld: error: unable to find library -lfreetype

However if I add -L/usr/local/lib it finds the library just fine... I would have thought /usr/local/lib was already in the search path.. Why is it necessary to add this?
 
Last edited by a moderator:

and


More information on this topic can be easily found via the forum search or your search engine of choice :)

Unless I'm mistaken, CMake on FreeBSD handles this automagically.
 
Unless I'm mistaken, CMake on FreeBSD handles this automagically.
It doesnt seem to be the case, it is a cmake project. It doesnt seem to pass -L/usr/local/lib to ld. If I manually edit link.txt which is generated by cmake and add in -L/usr/local/lib just before -lfreetype it will link.

Is there any cmake setting I can set to make this happen automatically?
 
What about using the ports framework?

It's better to try to compile through ports anyway because you don't need modify your own environment and you have control over the files that are installed.
 
ahhyes we really don't get a lot of information from you here so it's difficult to tell you anything helpful.

I just started a fresh project from scratch and it worked out of the box.

CMakeLists.txt
Code:
cmake_minimum_required(VERSION 3.20)

project(freetype_test)

find_package(
    Freetype
    REQUIRED
)

set(TARGET freetype_test)
add_executable(${TARGET})

target_compile_features(
    ${TARGET}
    PRIVATE
        cxx_std_20
)

target_sources(
    ${TARGET}
    PRIVATE
        main.cpp
)

target_link_libraries(
    ${TARGET}
    PRIVATE
        Freetype::Freetype
)

main.cpp
Code:
#include <ft2build.h>
#include FT_FREETYPE_H

#include <iostream>

int
main()
{
    FT_Library  library;

    auto error = FT_Init_FreeType(&library);
    if (error) {
        std::cerr << " FT_Init_FreeType() failed: " << error << std::endl;
        return EXIT_FAILURE;
    }

    std::cout << "Hello FreeType!" << std::endl;

    return 0;
}
 
Actually it seems to happen if pkg-config is used with cmake to find the package:

Code:
include(FindPkgConfig)
pkg_check_modules(FREETYPE REQUIRED freetype2)

add_executable( demo src/main.cpp)

target_link_libraries(demo ${FREETYPE_LIBRARIES})
target_include_directories(demo PRIVATE ${FREETYPE_INCLUDE_DIRS})

Fails with

ld: error: unable to find library -lfreetype

Using find_package(Freetype REQUIRED) works...


 
Looking at the linker commands, it seems when calling pkg_check_modules, it simply sets FREETYPE_LIBRARIES to "freetype" and the ld command ends up with -lfreetype (which fails).

Sadly the project I am trying to port makes use of pkg-config a lot in cmake which is proving to be annoying.
 
It seems I can make the pkg-config way work if I add:

link_directories(/usr/local/lib)

However the cmake manual discourages the use of link_directories - Is the solution here to just not use pkg-config with cmake on FreeBSD?
 
Sadly not all libraries ship a file under /usr/local/share/cmake/Modules/FindXXXXXX.cmake - So some things can use FIND_PACKAGE, and for other things, it's going to be pkg-config, looks like all the dependencies of the project I am trying to port that require using pkg-config are going to have the same issue. A bit of a pain.
 
Is there any suggestion to make the pkg-config method in cmake work without the need to add link_directories(/usr/local/lib) everywhere across the cmake project?
 
I don't know what pkg-config does. Is it some sort of Linux tool? Either way, are you not going about this the wrong way?
Surely you just use find_package(FreeType required) and it will add all the paths for you.
If you've tried this before, then apologies, I'm not reading the whole thread.

Edit: I see you state this as a reason above for not using find_package, but that's not a good thing. You can also use find_library and set paths within it to find libraries if cmake modules don't come with the OS you're running on.
 
Back
Top