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

ocamlopt reorders linker options #7150

Closed
vicuna opened this issue Feb 16, 2016 · 5 comments
Closed

ocamlopt reorders linker options #7150

vicuna opened this issue Feb 16, 2016 · 5 comments

Comments

@vicuna
Copy link

vicuna commented Feb 16, 2016

Original bug ID: 7150
Reporter: @yallop
Assigned to: @chambart
Status: assigned (set by @chambart on 2017-06-02T10:13:45Z)
Resolution: reopened
Priority: normal
Severity: minor
Target version: 4.03.1+dev
Category: compiler driver
Related to: #5441
Monitored by: @Drup @diml @ygrek @hcarty

Bug description

Here's a demonstration of unhelpful reordering behaviour involving options passed through by ocamlopt to the linker.

First, let's compile a couple of modules:

$ ocamlopt -c a.ml b.ml

Next, we'll build a library from one of the modules with some linker options, passed via -cclib:

$ ocamlopt -a -o liba.cmxa a.cmx -cclib -L/usr/adir/lib -cclib -L/usr/adir2/lib

Finally, we'll build an executable from the library and the other module, passing some more linker options:

$ ocamlopt -verbose -o b.native b.cmx -cclib -L/usr/lib/bdir/ -cclib -v liba.cmxa -cclib -L/usr/bdir2/lib 

Here's the linker invocation from the last step:

gcc -o 'b.native'   '-L/home/jeremy/.opam/4.02.3/lib/ocaml'  '/tmp/camlstartup7c51ee.o' '/home/jeremy/.opam/4.02.3/lib/ocaml/std_exit.o' 'liba.a' 'b.o' '/home/jeremy/.opam/4.02.3/lib/ocaml/stdlib.a' '-L/usr/adir/lib' '-L/usr/adir2/lib' '-L/usr/lib/bdir/' '-v' '-L/usr/bdir2/lib' '/home/jeremy/.opam/4.02.3/lib/ocaml/libasmrun.a' -lm  -ldl

In particular, note the order of the linker options

'-L/usr/adir/lib' '-L/usr/adir2/lib' '-L/usr/lib/bdir/' '-v' '-L/usr/bdir2/lib'

Although liba.cmxa appeared between -L/usr/lib/bdir and -L/usr/bdir2/lib in the ocamlopt command line, in the gcc command line the options embedded in liba.cmxa have all been moved to the beginning of the line.

Moving the options in this way is a problem because the order of -L options is significant. Moving cmxa-embedded options to the beginning of the line results in those options taking precedence over whatever's specified on the command-line. On my system, for example, the lwt-unix.cmxa library embeds -L/usr/lib, which then appears at the beginning of the library search path. As a result system libraries are chosen before user-installed libraries, which is not usually the desired behaviour.

@vicuna
Copy link
Author

vicuna commented Feb 24, 2016

Comment author: @damiendoligez

Please read the discussion under #5441.

@vicuna
Copy link
Author

vicuna commented Feb 24, 2016

Comment author: @yallop

Thanks for the reference. Perhaps I can fix the immediate issue by using -ccopt rather than -cclib to pass -L options.

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

Given that #5441 has been closed I believe this one can be too.

@vicuna
Copy link
Author

vicuna commented May 18, 2017

Comment author: @yallop

I'm reopening this because 0005441 doesn't solve the problem.

I'd like the command line options

-cclib -L/usr/bdir/lib liba.cmxa -cclib -L/usr/bdir2/lib

to embed the linker options from liba.cmxa between the linker option -L/usr/bdir/lib and -L/usr/bdir2/lib and there doesn't appear to be any way to achieve that with any combination of '-ccopt -Wl,' and '-cclib'.

@github-actions
Copy link

github-actions bot commented May 9, 2020

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

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

No branches or pull requests

2 participants