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

Linking error on OSX #1032

Closed
th0114nd opened this issue Dec 11, 2014 · 10 comments · Fixed by #1045
Closed

Linking error on OSX #1032

th0114nd opened this issue Dec 11, 2014 · 10 comments · Fixed by #1045

Comments

@th0114nd
Copy link

I just updated to the nightly versions of rust and cargo this morning,
which fail on compiling 66cd1a

Versions: rustc --verbose=verbose; cargo --version gives

rustc 0.13.0-nightly (8bca470c5 2014-12-08 00:12:30 +0000)
binary: rustc
commit-hash: 8bca470c5acf13aa20022a2c462a89f72de721fc
commit-date: 2014-12-08 00:12:30 +0000
host: x86_64-apple-darwin
release: 0.13.0-nightly

cargo 0.0.1-pre-nightly (0f6667c 2014-12-08 21:19:01 +0000)

The error output is

       Fresh docopt v0.6.13 (https://github.com/docopt/docopt.rs#38cc4572)
       Fresh semver v0.1.3 (https://github.com/rust-lang/semver#9ea33243)
       Fresh pkg-config v0.1.0
       Fresh url v0.2.2 (https://github.com/servo/rust-url#bdda3a7b)
       Fresh glob v0.1.0 (https://github.com/rust-lang/glob#6b26e613)
       Fresh gcc v0.1.0 (https://github.com/alexcrichton/gcc-rs#3caf7309)
       Fresh pkg-config v0.1.0 (https://github.com/alexcrichton/pkg-config-rs#9b3b44a2)
       Fresh toml v0.1.4 (https://github.com/alexcrichton/toml-rs#25e10a82)
       Fresh tar v0.1.0 (https://github.com/alexcrichton/tar-rs#c431eeae)
       Fresh gcc v0.1.0
       Fresh openssl-sys v0.2.2 (https://github.com/sfackler/rust-openssl#6cdd2cf5)
       Fresh time v0.1.1 (https://github.com/rust-lang/time#c1c02adb)
       Fresh libz-sys v0.1.0 (https://github.com/alexcrichton/libz-sys#6c19f130)
       Fresh miniz-sys v0.1.0 (https://github.com/alexcrichton/flate2-rs#5cf5e4f5)
       Fresh curl-sys v0.1.0 (https://github.com/carllerche/curl-rust#6f007b49)
       Fresh libssh2-sys v0.0.1 (https://github.com/alexcrichton/ssh2-rs#982dc47a)
       Fresh flate2 v0.1.0 (https://github.com/alexcrichton/flate2-rs#5cf5e4f5)
       Fresh curl v0.1.0 (https://github.com/carllerche/curl-rust#6f007b49)
       Fresh libgit2-sys v0.0.1 (https://github.com/alexcrichton/git2-rs#53785d74)
       Fresh registry v0.1.0 (file:///Users/tim/cargo)
       Fresh git2 v0.0.1 (https://github.com/alexcrichton/git2-rs#53785d74)
   Compiling cargo v0.1.0 (file:///Users/tim/cargo)
     Running `rustc src/bin/cargo.rs --crate-name cargo --crate-type bin -g --out-dir /Users/tim/cargo/target --dep-info /Users/tim/cargo/target/.fingerprint/cargo-f2e7efe4b1ea498a/dep-bin-cargo -L /Users/tim/cargo/target -L /Users/tim/cargo/target/deps --extern flate2=/Users/tim/cargo/target/deps/libflate2-5a9d56138eb3bd6b.rlib --extern tar=/Users/tim/cargo/target/deps/libtar-36ff090b04fc02d0.rlib --extern curl=/Users/tim/cargo/target/deps/libcurl-0df19872d4b5399f.rlib --extern registry=/Users/tim/cargo/target/deps/libregistry-d86966ea9cace796.rlib --extern glob=/Users/tim/cargo/target/deps/libglob-939306db26ee8d93.rlib --extern git2=/Users/tim/cargo/target/deps/libgit2-09174fe7d724f48b.rlib --extern docopt=/Users/tim/cargo/target/deps/libdocopt-64adc584d5ef7fee.rlib --extern toml=/Users/tim/cargo/target/deps/libtoml-b7ada17e70343ce1.rlib --extern semver=/Users/tim/cargo/target/deps/libsemver-dd25c811d0e0fb2d.rlib --extern time=/Users/tim/cargo/target/deps/libtime-8bde1abd230448f0.rlib --extern url=/Users/tim/cargo/target/deps/liburl-b469e1d44ac2e587.rlib --extern cargo=/Users/tim/cargo/target/libcargo-f2e7efe4b1ea498a.rlib -L /Users/tim/cargo/target/build/libgit2-sys-ce6d220b35959092/out/lib -L /Users/tim/cargo/target/build/libssh2-sys-cf42cea080951617/out/lib -L /Users/tim/cargo/target/build/miniz-sys-a684ca24728e1bf9/out -L /usr/lib -L /Users/tim/cargo/target/build/time-8bde1abd230448f0/out`
error: linking with `cc` failed: exit code: 1
note: cc '-m64' '-L' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib' '-o' '/Users/tim/cargo/target/cargo' '/Users/tim/cargo/target/cargo.o' '-Wl,-force_load,/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libmorestack.a' '-Wl,-dead_strip' '-nodefaultlibs' '/Users/tim/cargo/target/libcargo-f2e7efe4b1ea498a.rlib' '/Users/tim/cargo/target/deps/libglob-939306db26ee8d93.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libterm-4e7c5e5c.rlib' '/Users/tim/cargo/target/deps/libregistry-d86966ea9cace796.rlib' '/Users/tim/cargo/target/deps/libsemver-dd25c811d0e0fb2d.rlib' '/Users/tim/cargo/target/deps/libtoml-b7ada17e70343ce1.rlib' '/Users/tim/cargo/target/deps/libgit2-09174fe7d724f48b.rlib' '/Users/tim/cargo/target/deps/libtime-8bde1abd230448f0.rlib' '/Users/tim/cargo/target/deps/libflate2-5a9d56138eb3bd6b.rlib' '/Users/tim/cargo/target/deps/libminiz-sys-a684ca24728e1bf9.rlib' '/Users/tim/cargo/target/deps/liblibgit2-sys-ce6d220b35959092.rlib' '/Users/tim/cargo/target/deps/liblibssh2-sys-cf42cea080951617.rlib' '/Users/tim/cargo/target/deps/libcurl-0df19872d4b5399f.rlib' '/Users/tim/cargo/target/deps/libcurl-sys-151b74147b7ca2dd.rlib' '/Users/tim/cargo/target/deps/libopenssl-sys-c112af8ef9100c9f.rlib' '/Users/tim/cargo/target/deps/liblibz-sys-23b647d1eda82285.rlib' '/Users/tim/cargo/target/deps/libtar-36ff090b04fc02d0.rlib' '/Users/tim/cargo/target/deps/liburl-b469e1d44ac2e587.rlib' '/Users/tim/cargo/target/deps/libdocopt-64adc584d5ef7fee.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libserialize-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblog-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libregex-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libstd-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librustrt-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcollections-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liballoc-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/liblibc-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/librand-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libunicode-4e7c5e5c.rlib' '/usr/local/lib/rustlib/x86_64-apple-darwin/lib/libcore-4e7c5e5c.rlib' '-L' '/Users/tim/cargo/target' '-L' '/Users/tim/cargo/target/deps' '-L' '/Users/tim/cargo/target/build/libgit2-sys-ce6d220b35959092/out/lib' '-L' '/Users/tim/cargo/target/build/libssh2-sys-cf42cea080951617/out/lib' '-L' '/Users/tim/cargo/target/build/miniz-sys-a684ca24728e1bf9/out' '-L' '/usr/lib' '-L' '/Users/tim/cargo/target/build/time-8bde1abd230448f0/out' '-L' '/Users/tim/cargo/.rust' '-L' '/Users/tim/cargo' '-liconv' '-lcurl' '-lssl' '-lcrypto' '-lz' '-lz' '-lSystem' '-lpthread' '-lc' '-lm' '-lcompiler-rt'
note: ld: warning: directory not found for option '-L/Users/tim/cargo/.rust'
Undefined symbols for architecture x86_64:
  "_libiconv", referenced from:
      _git_path_iconv in liblibgit2-sys-ce6d220b35959092.rlib(r-git2-path.c.o)
  "_libiconv_close", referenced from:
      _git_path_iconv_clear in liblibgit2-sys-ce6d220b35959092.rlib(r-git2-path.c.o)
  "_libiconv_open", referenced from:
      _git_path_iconv_init_precompose in liblibgit2-sys-ce6d220b35959092.rlib(r-git2-path.c.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

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

Caused by:
  Process didn't exit successfully: `rustc src/bin/cargo.rs --crate-name cargo --crate-type bin -g --out-dir /Users/tim/cargo/target --dep-info /Users/tim/cargo/target/.fingerprint/cargo-f2e7efe4b1ea498a/dep-bin-cargo -L /Users/tim/cargo/target -L /Users/tim/cargo/target/deps --extern flate2=/Users/tim/cargo/target/deps/libflate2-5a9d56138eb3bd6b.rlib --extern tar=/Users/tim/cargo/target/deps/libtar-36ff090b04fc02d0.rlib --extern curl=/Users/tim/cargo/target/deps/libcurl-0df19872d4b5399f.rlib --extern registry=/Users/tim/cargo/target/deps/libregistry-d86966ea9cace796.rlib --extern glob=/Users/tim/cargo/target/deps/libglob-939306db26ee8d93.rlib --extern git2=/Users/tim/cargo/target/deps/libgit2-09174fe7d724f48b.rlib --extern docopt=/Users/tim/cargo/target/deps/libdocopt-64adc584d5ef7fee.rlib --extern toml=/Users/tim/cargo/target/deps/libtoml-b7ada17e70343ce1.rlib --extern semver=/Users/tim/cargo/target/deps/libsemver-dd25c811d0e0fb2d.rlib --extern time=/Users/tim/cargo/target/deps/libtime-8bde1abd230448f0.rlib --extern url=/Users/tim/cargo/target/deps/liburl-b469e1d44ac2e587.rlib --extern cargo=/Users/tim/cargo/target/libcargo-f2e7efe4b1ea498a.rlib -L /Users/tim/cargo/target/build/libgit2-sys-ce6d220b35959092/out/lib -L /Users/tim/cargo/target/build/libssh2-sys-cf42cea080951617/out/lib -L /Users/tim/cargo/target/build/miniz-sys-a684ca24728e1bf9/out -L /usr/lib -L /Users/tim/cargo/target/build/time-8bde1abd230448f0/out` (status=101)
@th0114nd
Copy link
Author

I should add that the version of gcc used is actually clang:

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix

@th0114nd
Copy link
Author

So libiconv is ultimately required by libgit2. In both git2-rs (on 056c7a7) and libgit2 (on 00e9ae5)
build/CMakeCache.txt is able to find /opt/local/lib/libiconv.dylib as the appropriate path. It is
still able to do so properly in ./target/build/libgit2-sys-ce6d220b35959092/out/build/CMakeCache.txt on 66cd61a in the cargo repository.

However, in the cc command in the error output above, there is a -liconv flag that includes libiconv but
only the following flag pairs specify where to look for libraries:

-L /usr/local/lib/rustlib/x86_64-apple-darwin-lib
-L /Users/tim/cargo/target
-L /Users/tim/cargo/target/deps
-L /Users/tim/cargo/target/build/libgit2-sys-ce6d220b35959092/out/lib
-L /Users/tim/cargo/target/build/libssh2-sys-cf42cea080951617/out/lib
-L /Users/tim/cargo/target/build/miniz-sys-a684ca24728e1bf9/out
-L /usr/lib
-L /Users/tim/cargo/target/build/time-8bde1abd230448f0/out
-L /Users/tim/cargo/.rust
-L /Users/tim/cargo

In particular, some part is not taking CMakeCache.txt's advice to include -L /opt/local/lib. However, it does find another version of libiconv in /usr/lib, and replacing -L /usr/lib with -L /opt/local/lib makes it work properly. Or instead of replacing it, inserting it before so that ld checks in /opt/local/lib first.

@alexcrichton
Copy link
Member

Ah yes it looks like this is a bug in libgit2-sys's build script!

@alexcrichton
Copy link
Member

cc #1029, another bug in libgit2's bulid script

@alexcrichton
Copy link
Member

Could you check out https://github.com/alexcrichton/cargo/tree/update-git2 and see if it works for you?

@th0114nd
Copy link
Author

So after further investigation, I found pretty much everything depends on /usr/lib/libiconv.dylib and removing it meant that sudo, the nightly cargo, Chrome, iTerm stop working and I had to boot into the recovery OS in order to move it back. I guess I should have realized that the version in /opt/local/lib was the less critical one. So then I'd guess that the CMakeCache.txt incorrectly chooses /opt/local/lib/libiconv.a over /usr/lib/libiconv.dylib. I can also see that the symbols that cargo is looking for in the /usr version are in the /opt version:

$ nm /usr/lib/libiconv.dylib | grep _close
0000000000002ea4 T _iconv_close
$ nm /opt/local/lib/libiconv.a | grep _close
0000000000002375 T _libiconv_close
00000000000f85f8 S _libiconv_close.eh

That is, in the /usr version they are prefixed with _iconv but in /opt with _libiconv

@th0114nd
Copy link
Author

update-git2 allows it to compile, but cargo test in the cargo/update-git2 does not pass with
a Library not loaded error from dyld:

   Compiling hamcrest v0.1.0 (https://github.com/carllerche/hamcrest-rust.git#2b9bd6cd)
   Compiling cargo v0.1.0 (file:///Users/tim/cargo)
     Running target/cargo-39287a133529d146
dyld: Library not loaded: /opt/local/lib/libcurl.4.dylib
  Referenced from: /Users/tim/cargo/target/cargo-39287a133529d146
  Reason: Incompatible library version: cargo-39287a133529d146 requires version 8.0.0 or later, but libcurl.4.dylib provides version 7.0.0
Process didn't exit successfully: `/Users/tim/cargo/target/cargo-39287a133529d146` (status=5)

I have both a /usr/lib/libcurl.4.dylib and an /opt/local/lib/libcurl.4.dylib if that helps.

Edit: Macports does not have a more recent version of curl & libcurl than 7.39.0 which is installed, and the curl download page does not list anything with a major version 8.

@alexcrichton
Copy link
Member

Aha! Ok, so getting past the linker was a definite goal of update-git2, so I think we're good on that front. Now dealing with DYLD_LIBRARY_PATH and friends may be a bit more difficult. It looks like we'd need to set that whenever running programs generated by cargo.

@th0114nd
Copy link
Author

Well yes, I agree it's a different error. But if I'm reading the message correctly, I think it's more that a version of libcurl is requested that doesn't exist.

@chrisxue815
Copy link

I had a similar problem while running cargo install cargo-update. It was eventually fixed by moving /Users/USER/anaconda/bin to the end of $PATH.

Here's how I found out the cause (please correct me if I am wrong):

  • Getting the raw cc command by running cargo install cargo-update -v.
  • Appending -Xlinker -v to the end and running the cc command shows me the "Library search paths". One of them is /usr/lib, which contains the libiconv.dylib that cc is trying to link.
  • nm -arch x86_64 -g /usr/lib/libiconv.dylib clearly shows that none of _libiconv, _libiconv_open, _libiconv_close is exposed by libiconv.dylib. It made me wonder whether the header file matches libiconv.dylib.
  • So I opened up the source code of libgit2-sys, found out that it's using cmake to locate the header file: FIND_PATH(ICONV_INCLUDE_DIR iconv.h).
  • After cargo build the source code, the cmake output can be found in target/debug/build/libgit2-sys-RANDOM/output. One line caught my eyes: -- Found Iconv: -L/Users/USER/anaconda/lib -liconv.
  • Here it is! Instead of /usr/include/iconv.h, cmake used /Users/USER/anaconda/lib/iconv.h, in which iconv is renamed to libiconv.
  • After some investigation, it turned out that cmake somehow found /Users/USER/anaconda/lib from /Users/USER/anaconda/bin in $PATH, even the last parts of the paths are different.
  • Removing /Users/USER/anaconda/bin from $PATH solved the problem. So did moving it to the end of $PATH (at least after /usr/lib)

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

Successfully merging a pull request may close this issue.

3 participants