Order of linked libraries in ocamlbuild
Asked Answered
A

1

6

I'm having an issue with the order in which the libraries are added to the linker. Previously built libraries by ocamlbuild are linked in after the list of libraries I included by the flag rule. And, I don't see any way to define this type of dependency in myocamlbuild.ml either.

Specifically, the issue comes in linking with a previously built library (gzcaml) that requires a library itself (z). Because of the added strictness in newer versions of gcc the argument -lz must appear after libgzcaml.a.

I am including all these libraries with,

flag ["ocaml"; "link"]
    (S (process "-cclib" clibs))

where process creates a list alternating the library and A"-cclib", appropriately.

Also, additional libraries are appended (from the verbose output, -lm and -ldl) but I have no idea how I can modify/append these? (this would instantly solve my problem).

My myocamlbuild.ml is quite long else I would have included it here. I have tried moving the above code around to the bottom of the After_rules, to the top. And it does change the order, but never after the built libraries (c and otherwise) that ocamlbuild created previously.


EDIT Below are code snippets I've used in my configure script and ocamlbuild to solve the issue above. Cheers!

in configure.ac

oCFLAGS="$CFLAGS"
CFLAGS="$FLAGS -Wl,--no-as-needed"
AC_MSG_CHECKING([whether we need to add --no-as-needed linking option])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], 
                                     [[ 
                                        int main(){ return 0; } 
                                    ]])], 
                  [AC_MSG_RESULT([yes]); CC_NOASNEEDED="true"],
                  [AC_MSG_RESULT([no]); CC_NOASNEEDED="false"]) 
CFLAGS=$oCFLAGS

in myocamlbuild.ml.in

 if @CC_NOASNEEDED@ then
     flag ["ocaml"; "link"]
         (S [A"-cclib";A"-Wl,--no-as-needed"]);
Apopemptic answered 28/11, 2011 at 19:43 Comment(0)
B
4

This is not an answer, but a workaround - disable this new linker behaviour with -cclib -Wl,--no-as-needed.

I guess this should be reported as a bug to mantis. Specifically, ocamlbuild should guarantee that options from flags are inserted into command-line in the same order as they are encountered in the source (this is the case now afair), and ocamlopt should preserve the order of -cclib and -ccopt arguments wrt other entries on the command-line (this is NOT the case now).

Bifarious answered 29/11, 2011 at 13:16 Comment(1)
Thanks this worked great. For anyone else with the problem, just make sure that these flags are before all the libraries.Apopemptic

© 2022 - 2024 — McMap. All rights reserved.