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"]);