I try to access a function from an archive file (.a) with Ctypes-foreign but the linker doesn’t include it in the exe file because the function is not statically linked.
If you want to make an object visible to the dynamic loader (so that dlsym that ctypes-foreign uses), you can link it with --export-dynamic. Try passing -Wl,--export-dynamic in your C flags.
(* Hack needed to make symbols available, see constfun's comment here
* https://github.com/ocamllabs/ocaml-ctypes/issues/541 *)
external _force_link_ : unit -> unit = "pg_query_free_parse_result"
However, this works on Linux, not on Windows, even if the symbols are exported (I mean listed by nm).
A (link_flags -ccopt -Wl,--export-dynamic) stanza fails on Windows.
# x86_64-w64-mingw32-gcc: error: unrecognized command-line option ‘--export-dynamic’; did you mean ‘-export-dynamic’?
EDIT: I progress… objdump _path_of_pg_check gives:
/cygdrive/c/Users/frede/AppData/Local/opam/default/bin/pg_check.exe: file format pei-x86-64
SYMBOL TABLE:
***plenty of symbols***
DYNAMIC SYMBOL TABLE:
no symbols
Then, I have to find how to export dynamic symbols.
Finally, the problem is solved, but not in a satisfactory way.
I have to compile my main file (executable stanza) with the following dune stanza:
(link_flags -cclib -Wl,-Wl,--export-all-symbols)
-cclib is to tell Ocamlopt to pass the following to the linker. Gcc is used as a linker, then -Wl, must be used to indicate something has to be passed to the linker. On opam2.2.0~beta1/Windows/MinGW, there is something which deals with flexlink and gcc is called once again… a second -Wl is required. Then the --export-all-symbols which is the way to export all symbols in the dynamic table. (--export-dynamic doesn’t work with PE executables, i.e. Windows EXE).
I guess that a Dune/Ocaml should have make things simpler. Typically a stanza which would avoid the need of -Wl,-Wl. Shouldn’t -cclib escape its argument and pass it really to the linker. (The documentation is only about -cclib -l... then the use without a -l is unspecified.) If one want to pass something to gcc, there is -ccopt.
Is there a way to make this stanza condionnal ? (Only in a MinGW environment)