Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined references when linking with poppler on Ubuntu 17.10 #69

Open
misos1 opened this issue Jul 17, 2018 · 7 comments
Open

Undefined references when linking with poppler on Ubuntu 17.10 #69

misos1 opened this issue Jul 17, 2018 · 7 comments

Comments

@misos1
Copy link

misos1 commented Jul 17, 2018

When I run cargo build on attached sample rust program test.zip I am getting undefined references:

In function `test':
          /home/miso/test/test.c:6: undefined reference to `g_get_current_dir'
          /home/miso/test/test.c:7: undefined reference to `g_build_filename'
          /home/miso/test/test.c:8: undefined reference to `g_filename_to_uri'

Dependencies are from these ubuntu packages: libpoppler-glib-dev libcairo2-dev libpango1.0-dev

It is working for example on OSX but on both I am getting these warnings:

warning: redundant linker flag specified for library `cairo`
warning: redundant linker flag specified for library `gobject-2.0`
warning: redundant linker flag specified for library `glib-2.0`

These warnings are probably because pkg-config is not checking whether emitting duplicated dependencies from multiple packages into stdout:

cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu
cargo:rustc-link-lib=poppler-glib
cargo:rustc-link-lib=gio-2.0
cargo:rustc-link-lib=gobject-2.0
cargo:rustc-link-lib=glib-2.0
cargo:rustc-link-lib=cairo
cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu
cargo:rustc-link-lib=cairo
cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu
cargo:rustc-link-lib=pangocairo-1.0
cargo:rustc-link-lib=pango-1.0
cargo:rustc-link-lib=gobject-2.0
cargo:rustc-link-lib=glib-2.0
cargo:rustc-link-lib=cairo

Here is output from terminal from cargo build -v on Ubuntu:

     Running `/home/miso/test/target/debug/build/test-9b662f66ec35ba4e/build-script-build`
     Running `rustc --crate-name test test.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fe9134e5c08aec2d -C extra-filename=-fe9134e5c08aec2d --out-dir /home/miso/test/target/debug/deps -C incremental=/home/miso/test/target/debug/incremental -L dependency=/home/miso/test/target/debug/deps -L native=/usr/lib/x86_64-linux-gnu -L native=/usr/lib/x86_64-linux-gnu -L native=/usr/lib/x86_64-linux-gnu -L native=/home/miso/test/target/debug/build/test-201ba572ee26061f/out -l poppler-glib -l gio-2.0 -l gobject-2.0 -l glib-2.0 -l cairo -l cairo -l pangocairo-1.0 -l pango-1.0 -l gobject-2.0 -l glib-2.0 -l cairo -l static=test`
warning: redundant linker flag specified for library `cairo`

warning: redundant linker flag specified for library `gobject-2.0`

warning: redundant linker flag specified for library `glib-2.0`

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.1y16o1qfye96o7m0.rcgu.o" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.3rngp6bm2u2q5z0y.rcgu.o" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.4oc10dk278mpk1vy.rcgu.o" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.oa3rad818d8sgn4.rcgu.o" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.z2ztxthfpf83dor.rcgu.o" "-o" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d" "/home/miso/test/target/debug/deps/test-fe9134e5c08aec2d.crate.allocator.rcgu.o" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-L" "/home/miso/test/target/debug/deps" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "/home/miso/test/target/debug/build/test-201ba572ee26061f/out" "-L" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-l" "poppler-glib" "-l" "gio-2.0" "-l" "gobject-2.0" "-l" "glib-2.0" "-l" "cairo" "-l" "pangocairo-1.0" "-l" "pango-1.0" "-Wl,-Bstatic" "-Wl,--whole-archive" "-l" "test" "-Wl,--no-whole-archive" "-Wl,--start-group" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-0888da25bc5aa659.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-c21a3f7e9b2ce71d.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-a89ec74248f6f925.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-9232e45013d81e94.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_system-afbe0a28ca4ea99f.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-bcf95ff3aa4e3276.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-26716181a02171e3.rlib" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-978afee7e21437ec.rlib" "-Wl,--end-group" "/home/miso/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-3f4ad009a7cf735f.rlib" "-Wl,-Bdynamic" "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "pthread" "-l" "gcc_s" "-l" "c" "-l" "m" "-l" "rt" "-l" "pthread" "-l" "util" "-l" "util"
  = note: /home/miso/test/target/debug/build/test-201ba572ee26061f/out/libtest.a(test.o): In function `test':
          /home/miso/test/test.c:6: undefined reference to `g_get_current_dir'
          /home/miso/test/test.c:7: undefined reference to `g_build_filename'
          /home/miso/test/test.c:8: undefined reference to `g_filename_to_uri'
          /home/miso/test/test.c:9: undefined reference to `poppler_document_new_from_file'
          /home/miso/test/test.c:11: undefined reference to `poppler_document_get_n_pages'
          /home/miso/test/test.c:13: undefined reference to `g_object_unref'
          /home/miso/test/test.c:14: undefined reference to `g_free'
          /home/miso/test/test.c:15: undefined reference to `g_free'
          /home/miso/test/test.c:16: undefined reference to `g_free'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: Could not compile `test`.

Caused by:
  process didn't exit successfully: `rustc --crate-name test test.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=fe9134e5c08aec2d -C extra-filename=-fe9134e5c08aec2d --out-dir /home/miso/test/target/debug/deps -C incremental=/home/miso/test/target/debug/incremental -L dependency=/home/miso/test/target/debug/deps -L native=/usr/lib/x86_64-linux-gnu -L native=/usr/lib/x86_64-linux-gnu -L native=/usr/lib/x86_64-linux-gnu -L native=/home/miso/test/target/debug/build/test-201ba572ee26061f/out -l poppler-glib -l gio-2.0 -l gobject-2.0 -l glib-2.0 -l cairo -l cairo -l pangocairo-1.0 -l pango-1.0 -l gobject-2.0 -l glib-2.0 -l cairo -l static=test` (exit code: 101)

Rust version stable 1.27.1.

When I try to emit dependencies manually into stdout then it is compiling without problems.

@alexcrichton
Copy link
Member

Thanks for the report! Since this is largely just forwarding flags from pkg-config though, I'm not sure what this repository would do differently?

@misos1
Copy link
Author

misos1 commented Jul 18, 2018

Preventing already emitted strings from emitting them again to stdout should remove these warnings like when you call pkg-config with multiple packages it will output each library only once.

Maybe problem with compilation is on rustc side or there are needed some additional flags due to something what this crate is emitting I am not sure. The best would be to compare what is this crate emitting to stdout or doing vs what you would emit by calling "pkg-config --libs poppler-glib cairo pangocairo" or "pkg-config --libs-only-l --libs-only-L poppler-glib cairo pangocairo" which both worked when I parsed and put output in this form "cargo:rustc-flags=-l cairo -l poppler -L /usr/...".

@misos1
Copy link
Author

misos1 commented Jul 19, 2018

I found the problem. I tried to link to another package libpng, test.c:

#include <png.h>
void test()
{
	unsigned char x[8];
	png_sig_cmp(x, 0, sizeof(x));
}

With this code in build.rs:

fn main()
{
	pkg_config::probe_library("libpng").unwrap();
	cc::Build::new().file("test.c").compile("test");
}

On ubuntu gives error "undefined reference to `png_sig_cmp'". On OSX is this working.

But this compiles ok:

fn main()
{
	cc::Build::new().file("test.c").compile("test");
	pkg_config::probe_library("libpng").unwrap();
}

Same is true also for example with poppler. So library probing must be done after compilation. But this beats one purpose of pkg_config crate because then I cannot get include paths for package for compilation stage. When is called probe_library both before and after compilation it is also not working.

Maybe solution could be to not emit "rustc-link-lib" immediately to stdout during probe_library but to have possibility to emit it later after I collect "pkg_config::Library" structures from probe_library and compile c code.

Core of problem is that during final compilation test lib must be on command line before libpng. There is output from first build.rs (some details omitted, visible are mainly -l flags):

cargo:rustc-link-lib=png16
cargo:rustc-link-lib=z
cargo:rustc-link-lib=static=test
...
     Running `rustc --crate-name test test.rs --crate-type bin ... -l png16 -l z -l static=test`
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" ... "-l" "png16" "-l" "z" ... "-l" "test" ... "-l" "dl" "-l" "rt" "-l" "pthread" "-l" "pthread" "-l" "gcc_s" "-l" "c" "-l" "m" "-l" "rt" "-l" "pthread" "-l" "util" "-l" "util"
  = note: /home/miso/test/target/debug/build/test-4eee4af9b8645a50/out/libtest.a(test.o): In function `test':
          /home/miso/test/test.c:8: undefined reference to `png_sig_cmp'
          collect2: error: ld returned 1 exit status

And here for second:

cargo:rustc-link-lib=static=test
cargo:rustc-link-lib=png16
cargo:rustc-link-lib=z
     Running `rustc --crate-name test test.rs --crate-type bin ... -l static=test -l png16 -l z`
    Finished dev [unoptimized + debuginfo] target(s) in 1.20s

"cargo:rustc-link-lib=static=test" is emitted to stdout by "cc::Build::new().file("test.c").compile("test");"

So problem is in order of -l flags passed to cc called from rustc.

In this case:

fn main()
{
	pkg_config::probe_library("libpng").unwrap();
	cc::Build::new().file("test.c").compile("test");
	pkg_config::probe_library("libpng").unwrap();
}

Rustc is called with "-l png16 -l z -l static=test -l png16 -l z" but it will pass only first "-l png16" to cc '"-l" "png16" "-l" "z" ... "-l" "test"'.

@alexcrichton
Copy link
Member

You can disable printing andprint it out later yourself perhaps?

@misos1
Copy link
Author

misos1 commented Jul 19, 2018

But not when using metadeps: https://docs.rs/metadeps/1.1.2/metadeps/

And printing it manually is not so comfortable, maybe here I could first call probe without printing and use include paths and after compilation enable printing and probe again but this is not very nice and still not doable with metadeps.

@alexcrichton
Copy link
Member

Maybe this is a bug for metadeps in that case? I'm not sure what should change in this library?

@misos1
Copy link
Author

misos1 commented Jul 19, 2018

Maybe there can be configurable sink with trait std::io::Write in Config where will go prints instead of fixed stdout. Maybe this could be good also for cc-rs crate. Or maybe it could collect these strings into vector.

Regarding rustc warnings about redundant linker flags maybe this crate could output same flags only once but then should keep latter occurrences so right dependencies are conserved. This is causing just warnings but as I realised rustc is ignoring duplicated "-l" flags when passing them to cc in way that are kept only first occurrences which could cause linker errors when is order of probed packages reordered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants