Always pass the libraries to link as last argument to the linker. Should fix build failures with as-needed such as: https://bugs.gentoo.org/show_bug.cgi?id=331075 https://bugs.gentoo.org/show_bug.cgi?id=331377 Index: ocaml-3.12.0/utils/ccomp.ml =================================================================== --- ocaml-3.12.0.orig/utils/ccomp.ml +++ ocaml-3.12.0/utils/ccomp.ml @@ -98,8 +98,18 @@ type link_mode = | MainDll | Partial +let is_lib name = String.length name >= 2 && String.sub name 0 2 = "-l" + +let rec link_order init libs = function + [] -> List.rev_append init (List.rev libs) + | t::q -> if is_lib t then link_order init (t::libs) q else link_order (t::init) libs q + +let rec split_libs libs others = function + [] -> (libs, others) + | t::q -> if is_lib t then split_libs (t::libs) others q else split_libs libs (t::others) q + let call_linker mode output_name files extra = - let files = quote_files files in + let files = quote_files (link_order [] [] files) in let cmd = if mode = Partial then Printf.sprintf "%s%s %s %s" @@ -108,7 +118,8 @@ let call_linker mode output_name files e files extra else - Printf.sprintf "%s -o %s %s %s %s %s %s %s" + let (cclibs,ccopts) = split_libs [] [] !Clflags.ccopts in + Printf.sprintf "%s -o %s %s %s %s %s %s %s %s" (match !Clflags.c_compiler, mode with | Some cc, _ -> cc | None, Exe -> Config.mkexe @@ -120,8 +131,9 @@ let call_linker mode output_name files e (if !Clflags.gprofile then Config.cc_profile else "") "" (*(Clflags.std_include_flag "-I")*) (quote_prefixed "-L" !Config.load_path) - (String.concat " " (List.rev !Clflags.ccopts)) + (String.concat " " ccopts) files + (String.concat " " cclibs) extra in command cmd = 0