Question about C dependencies

Hi, I have a question about how Dune works together with C libraries.
I am not super familiar with C compilation in general much less how C interfaces with OCaml.

I have two C libraries that I want to integrate as dependencies to my program, say X and Y.
In my file system I have two folders, X and Y, and they both have files X.c, X.ml, X.mli and Y.c, Y.ml, Y.mli. X and Y implement almost exactly the same functionality except that X is single-threaded C and Y is multithreaded C and needs to be compiled with -lpthreads and -lm. However, both of their header files are almost identical modulo some small changes in function signatures etc.

The dune file for my main executable is very simple. It is simply

(executable
 (public_name main)
 (name main)
 (libraries main_lib X Y)
 (modes exe))

It doesn’t contain anything about static linking or compilation flags because my understanding is that that would go in the dune files for the libraries, not the executables.

My concern is that the second library is “shadowing” the first one. If I simply write
(libraries main_lib Y X)
instead of
(libraries main_lib X Y)
then at runtime I get the error message
Fatal error: exception Dl.DL_error("_build/default/bin/test.exe: undefined symbol: futhark_context_config_set_num_threads")
The “set_num_threads” function is present in the Y code but not present in the X code. It would appear that the main program is not looking in library Y for the function. Somehow X “shadows it” or otherwise interferes with the process of finding the function, if I delete X it is fine.

For completeness, the dune files for the libraries are

(library
 (name scaling_unbalanced_single_core)
 (flags :standard -w -69-34)
 (libraries ctypes ctypes.foreign)
 (foreign_stubs (language c) (names scaling_unbalanced_single_core)))

and

(library
 (name scaling_unbalanced_multicore)
 (flags :standard -warn-error -69-34)
 (libraries ctypes ctypes.foreign)
 (library_flags -linkall -cclib -lpthread)
 (c_library_flags -lm)
 (foreign_stubs (language c) (names scaling_unbalanced_multicore)))

This is the repository for the code. I think that this branch does illustrate the problem, if you swap the order of libraries in the dune file for /bin/main.ml. If not I will upload appropriately buggy code.

1 Like