How to build MySQL 8.0 C API (libmysqlclient)

If you write C applications that need to connect to MySQL, you can use the MySQL C API aka libmysqlclient. The MySQL C API replaces the outdated MySQL-Connector-C.

If you want to use MySQL 8.0 as a Document Store with the X Protocol, you need then to use MySQL Connector/C++ 8.0.

Some have asked how to compile only the MySQL C API.

Compiling only libmysqlclient

As the FAQ stipulates it, it’s not possible to only build the library. However, as mentioned in the documentation, it’s possible to reduce the amount of compiled products with some cmake options.

You still need to get the full source tree (from GitHub for example) and bypass the compilation of the server.

~ $ mkdir workspace
~ $ cd workspace
~workspace $ git clone https://github.com/mysql/mysql-server.git
~workspace $ mkdir mysql-server-bin-debug
~workspace $ cd mysql-server-bin-debug
~workspace/mysql-server-bin-debug $ cmake -DWITHOUT_SERVER=ON \
                                    -DDOWNLOAD_BOOST=1 \
                                    -DWITH_BOOST=../mysql-server/downloads/ \
                                    -DWITH_UNIT_TESTS=OFF ../mysql-server
~workspace/mysql-server-bin-debug $ make -j 8                        

On my laptop, this command took 2m24sec

Now you have built the client line tools and also libmysqlclient:

Conclusion

Following the process outlined in this blog, it takes me 2min24sec to build the client line tools and also libmysqlclient. You are required to download the complete source, and build some extra command line tools, but the process is fast.

Update !

Joro added extra information to this bug including how to bypass boost. I recommend reading those comments especially if you are building this on Windows.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

4 Comments

  1. Hi,
    How do you build the Debug version f the library?
    Do I have to provide the Boost option(s)? Will it be compiled without it?

    Thank you.

    • Hi Igor,

      Adding -DWITH_DEBUG=1 to the cmake command line will provide the debug version of the library.

      You can compare the output with readelf -w libmysqlclient.so.21.2.29 for example 😉

      Cheers,

  2. Try to build libmysqlclient on msys2 on window 10 with the follow command:
    “`bash
    cmake -DWITHOUT_SERVER=ON \
    -DFORCE_INSOURCE_BUILD=1 \
    -DDOWNLOAD_BOOST=1 \
    -DWITH_BOOST=../mysql-server-mysql-8.0.32/downloads/ \
    -DWITH_UNIT_TESTS=OFF ../mysql-server-mysql-8.0.32
    “`

    Then it output the error:
    “`
    D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-2.1.11-stable/ipv6-int
    ernal.h:53:8: error: redefinition of ‘struct in6_addr’
    53 | struct in6_addr {
    | ^~~~~~~~
    In file included from C:/msys/ucrt64/include/ws2ipdef.h:17,
    from C:/msys/ucrt64/include/ws2tcpip.h:18,
    from D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-
    2.1.11-stable/include/event2/util.h:68,
    from D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-
    2.1.11-stable/bufferevent_filter.c:51:
    C:/msys/ucrt64/include/in6addr.h:17:16: note: originally defined here
    17 | typedef struct in6_addr {
    | ^~~~~~~~
    D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-2.1.11-stable/ipv6-int
    ernal.h:54:20: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘.’ token
    54 | ev_uint8_t s6_addr[16];
    | ^~~~~~~
    D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-2.1.11-stable/ipv6-int
    ernal.h:63:8: error: redefinition of ‘struct sockaddr_in6’
    63 | struct sockaddr_in6 {
    | ^~~~~~~~~~~~
    C:/msys/ucrt64/include/ws2ipdef.h:140:8: note: originally defined here
    140 | struct sockaddr_in6 {
    | ^~~~~~~~~~~~
    D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-2.1.11-stable/util-int
    ernal.h:389:8: error: redefinition of ‘struct sockaddr_storage’
    389 | struct sockaddr_storage {
    | ^~~~~~~~~~~~~~~~
    In file included from D:/Codelabs/GitHub/msys/src/mysql-server-mysql-8.0.32/extra/libevent/libevent-
    2.1.11-stable/bufferevent_filter.c:48:
    C:/msys/ucrt64/include/winsock2.h:269:10: note: originally defined here
    269 | struct sockaddr_storage {
    | ^~~~~~~~~~~~~~~~
    [57/523] Building C object extra/zstd/CMakeFiles…jlib.dir/zstd-1.5.0/lib/compress/zstd_lazy.c.obj
    ninja: build stopped: subcommand failed.
    “`

Leave a Reply

Your email address will not be published. Required fields are marked *

As MySQL Community Manager, I am an employee of Oracle and the views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

You can find articles I wrote on Oracle’s blog.