Search code examples
rustfreetype

Why is a link error reported when using freetype-rs?


I have a basic project that uses freetype-rs, but it reports a link error when I run cargo run. My system is OS X Yosemite 10.10.2.

Directory listing

./Cargo.toml
./src/main.rs

Cargo.toml

[package]

name = "usefree"
version = "0.0.1"
authors = ["zhch <zhch@gmail.com>"]

[dependencies]
freetype-rs = "0.1.0"

src/main.rs

#[macro_use]
extern crate freetype;

fn main() {
    println!("Hello,World!");
}

Error from cargo run

Compiling usefree v0.0.1 (file:///Users/zhangcheng/temp/d3/san)
error: linking with `cc` failed: exit code: 1
note: "cc" "-m64" "-L" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib" "/Users/zhangcheng/temp/d3/san/target/debug/usefree.o" "-o" "/Users/zhangcheng/temp/d3/san/target/debug/usefree" "-Wl,-force_load,/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a" "-Wl,-dead_strip" "-nodefaultlibs" "/Users/zhangcheng/temp/d3/san/target/debug/deps/libfreetype-4323fafa5d970f54.rlib" "/Users/zhangcheng/temp/d3/san/target/debug/deps/libfreetype_sys-a42cc5659b21e38e.rlib" "/Users/zhangcheng/temp/d3/san/target/debug/deps/liblibc-ef5cbad4ef5c7a1e.rlib" "/Users/zhangcheng/temp/d3/san/target/debug/deps/libbitflags-dd68b8369bcd8ff0.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/libstd-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/libcollections-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/librustc_unicode-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/librand-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/liballoc-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/liblibc-74fa456f.rlib" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib/libcore-74fa456f.rlib" "-L" "/Users/zhangcheng/temp/d3/san/target/debug" "-L" "/Users/zhangcheng/temp/d3/san/target/debug/deps" "-L" "/Users/zhangcheng/.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib" "-L" "/Users/zhangcheng/temp/d3/san/.rust/lib/x86_64-apple-darwin" "-L" "/Users/zhangcheng/temp/d3/san/lib/x86_64-apple-darwin" "-l" "freetype" "-l" "c" "-l" "m" "-l" "System" "-l" "pthread" "-l" "c" "-l" "m" "-Wl,-rpath,@loader_path/../../../../../.multirust/toolchains/nightly/lib/rustlib/x86_64-apple-darwin/lib" "-Wl,-rpath,/usr/local/lib/rustlib/x86_64-apple-darwin/lib" "-l" "compiler-rt"
note: ld: warning: directory not found for option '-L/Users/zhangcheng/temp/d3/san/.rust/lib/x86_64-apple-darwin'
ld: warning: directory not found for option '-L/Users/zhangcheng/temp/d3/san/lib/x86_64-apple-darwin'
ld: library not found for -lfreetype
clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error
Could not compile `usefree`.

After brew install freetype, rust compiles freetype but the error still there.

    Updating registry `https://github.com/rust-lang/crates.io-index`
   Compiling libc v0.1.8
   Compiling bitflags v0.1.1
   Compiling pkg-config v0.3.5
   Compiling libz-sys v0.1.6
   Compiling freetype-sys v0.1.2
   Compiling freetype-rs v0.1.0
   Compiling usefree v0.0.1 (file:///Users/zhangcheng/temp/d3/san)
error: linking with `cc` failed: exit code: 1

Solution

  • Well, it builds a Rust library which wants to link with freetype but there is no such library on your system. Since you're using Mac OS X, it is not really unexpected. Freetype is native to Linux world, not for macs.

    You need to install freetype, for example, with brew:

    brew install freetype
    

    It would install the library to /usr/local/lib.

    If you do use brew, it would automatically add a corresponding pkg-config configuration to your system, and because freetype-sys (a dependency of freetype-rs) uses pkg-config to discover the library, everything will start working automatically.

    If you're not using brew, chances are you won't get a pkg-config configuration, and no necessary flags would be added to the build automatically. Frankly, I don't know how to overcome this without changing freetype-sys build to explicitly add /usr/local/lib (or whatever path the library is installed into). You can add a build script to your program which would append the path to freetype to the build options, but it won't affect the compilation of dependency. It may or may not work.