ygrek / ocaml-mysql

OCaml bindings to libmysqlclient
https://ygrek.org.ua/p/ocaml-mysql
GNU Lesser General Public License v2.1
23 stars 11 forks source link

Possible to compile with mysql statically linked #9

Closed jmmk closed 7 years ago

jmmk commented 7 years ago

Is it possible that I can compile my application with libmysqlclient included in the build artifact?

ygrek commented 7 years ago

Try linking final binary with -noautolink.

ygrek commented 7 years ago

Or you mean to make ocaml-mysql link in libmysqlclient statically? Then you are better off adding -Wl,-Bstatic to final link command line (but be careful to not statically link in glibc). Either way I think there is nothing specific to ocaml-mysql. Let me know if it is not the case.

jmmk commented 7 years ago

You're right that this is not specific to ocaml-mysql. More an issue of statically linking C dependencies, specifically on a Mac. Thanks for pointing me in the direction of linker flags.

After some experimentation, it seems like this does not work well on Mac (it looks for .dylib files before .a files, so I have to rename the .dylibs to be able to statically link). I think it might work if I could exclude some flags like -llibmysqlclient and -L/usr/local/opt/mysql/lib when I include the path to /usr/local/opt/mysql/lib/libmysqlclient.a, but ocamlbuild is adding these flags and I don't know of an easy way to modify them.

The solution I'm using is to instead include the .dylib and call install_name_tool -change /usr/local/opt/mysql/lib/libmysqlclient.20.dylib @executable_path/libmysqlclient.dylib <my_compiled_executable>

ygrek commented 7 years ago

I think it might work if I could exclude some flags like -llibmysqlclient and -L/usr/local/opt/mysql/lib when I include the path to /usr/local/opt/mysql/lib/libmysqlclient.a, but ocamlbuild is adding these flags and I don't know of an easy way to modify them.

This is what -noautolink is for.

But the idea of -Bstatic is exactly to avoid the need to specify .a directly, but influence the preference of linker instead (to resolve -llib to lib.a not lib.dylib). I don't know if this applies to Mac though

jmmk commented 7 years ago

@ygrek I read in several places that -Bstatic is a no-op on Mac because of the way the build toolchain is configured.

I could probably use something like -noautolink, but I don't want to have to specify all the includes for every package, I just want to exclude mysql.

ygrek commented 7 years ago

Got it, Mac OS strikes again.