English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Options order for ocamlc/opt/opt.opt
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2007-11-24 (03:09)
From: Julien Moutinho <julien.moutinho@g...>
Subject: Re: [Caml-list] Re: Options order for ocamlc/opt/opt.opt
On Fri, Nov 23, 2007 at 05:40:27PM +0100, Romain Beauxis wrote:
> Le Friday 23 November 2007 02:43:34, vous avez écrit :
> > The fix in this case is to simply fix the linking order so that the
> > libraries given to the linker are all after the object files and the static
> > archives."
> (...)
> > There's a good documentation where the above statement is taken there:
> >   http://www.gentoo.org/proj/en/qa/asneeded.xml
> >
> > What do you think ?
> Well, I'm quite confused with this...
> After spending some time finding out how to change those arguments order, 
> I ended up with a patch that orders them just perfectly, 
> so that the following is passed:
>   gcc <other options> <-I options> -Wl,--as-needed <base_file.o> <project_files.o> <static_libraries.a> <dynamic list of -lfoo> -o object
> This is exactly what's refered as a good practice in the above link, but still I got my linking wrong...
> Ha, and of course this option works on a simple hello_world with the same gcc...
Options passed to -cclib are in !Clflags.ccobjs to which -l<foo> flags are appended,
hence your patch:
          Printf.sprintf "%s %s %s %s %s %s %s %s %s %s -o %s"
            (if !Clflags.gprofile then Config.cc_profile else "")
            (Clflags.std_include_flag "-I")
              (List.map (fun dir -> if dir = "" then "" else "-L" ^ dir)
            (String.concat " " !Clflags.ccopts)
            (Ccomp.quote_files (List.rev !Clflags.ccobjs))
            (Filename.quote startup_file)
            (Ccomp.quote_files (List.rev file_list))
            (Ccomp.quote_optfile runtime_lib)
            (Filename.quote output_name)

gives (it can easily be seen by wrapping /usr/bin/gcc with a shell
script /usr/local/bin/gcc echoing its arguments):

  gcc -I/usr/local/lib/ocaml -L/usr/local/lib/ocaml /tmp/camlstartupf0fb45.o \
    /usr/local/lib/ocaml/std_exit.o test.o /usr/local/lib/ocaml/stdlib.a \
    /usr/local/lib/ocaml/libasmrun.a -Wl,--as-needed -lm -ldl -o test

Which is not what you want, isn't it.

But now, what exactly is wrong for you in:
  ocamlopt -o test -ccopt '-Wl,--as-needed' -cclib '-lunix' test.ml

which gives (with a vanilla release310 branch):

  gcc -o test -I/usr/local/lib/ocaml -Wl,--as-needed \
    /tmp/camlstartupd18cfa.o /usr/local/lib/ocaml/std_exit.o test.o \
    /usr/local/lib/ocaml/stdlib.a -L/usr/local/lib/ocaml -lunix \
    /usr/local/lib/ocaml/libasmrun.a -lm -ldl