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

rustc cannot statically link to C library #27142

Closed
vks opened this issue Jul 20, 2015 · 4 comments
Closed

rustc cannot statically link to C library #27142

vks opened this issue Jul 20, 2015 · 4 comments

Comments

Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
@vks
Copy link
Contributor

@vks vks commented Jul 20, 2015

When trying to link to a static C library called md

rustc src/lib.rs [...] -L native=/path/to/md -l static=md

it fails:

error: linking with `cc` failed: exit code: 1
[...]
note: /usr/bin/ld: /path/to/md/libmd.a(acosh.o): relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
/path/to/md/libmd.a(acosh.o): error adding symbols: Bad value
collect2: error: ld returned 1 exit status

error: aborting due to previous error

I don't understand why rustc tries making a shared object. Changing the argument to -l dylib does not make any difference.

@vks
Copy link
Contributor Author

@vks vks commented Jul 20, 2015

How to reproduce:

$ git clone https://github.com/vks/special-fun.git
$ cd special-fun
$ cargo build  # works
[...]
$ cargo test  # fails
[...]

Maybe it is related to rustc --test?

@alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented Jul 20, 2015

This is actually working as intended because rustc produces position-independent output by default (even for executables, not just for dynamic libraries). You'll need to add -fPIC to your makefiles for the native library or use a build dependency like gcc to build native code.

@vks
Copy link
Contributor Author

@vks vks commented Jul 20, 2015

Is it possible to make it produce non position-independent output?

@alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented Jul 20, 2015

Yes rustc -C relocation-model=static should lift this requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment