Embedded MariaDB C/C++ API Embedded MariaDB C/C++ API mysql mysql

Embedded MariaDB C/C++ API


Ok first a little explanation about your example. The user is using gcc to compile and I can see you are using cmake.First what does -lz and mysql_config --include --libmysqld-libs means. The first is link zlib to link zlib in cmake you can refer to this answer, but long story short:

find_package( ZLIB REQUIRED )if ( ZLIB_FOUND )    include_directories( ${ZLIB_INCLUDE_DIRS} )    target_link_libraries( sql_fn ${ZLIB_LIBRARIES} )endif( ZLIB_FOUND )

Then you need the mariaDB library and that is the second part. mysql_config --include --libmysqld-libs this means execute the command mysql_config --include --libmysqld-libs which will return a string with the link options so execute the command:

$mysql_config --include --libmysqld-libs-I/usr/local/mysql/include-L/usr/local/mysql/lib  -lmysqld

And you should get an output like the one above. The -I is to look for headers in a given directory and the -L is to search a library in a directory, the -l is to link a given library it serves the same purpose as -lz only you are adding -lmysqld.

Well now that all is explained you need only include the -I -L and -l options with mysql however this is not such a standard library so you need to include directories and libraries through a script as explained in this anwer. So again long story short there is no bullet proof for this for example my library is in /usr/local/mysql/lib and yours is in /usr/local/lib. Since that is the case it will be easier to use the second method.

execute_process(COMMAND mysql_config --include    OUTPUT_VARIABLE MYSQL_INCLUDE)execute_process(COMMAND mysql_config --libmysqld-libs    OUTPUT_VARIABLE MYSQL_LIBS)target_compile_options(sql_fn PUBLIC ${MYSQL_INCLUDE})target_link_libraries(sql_fn ${MYSQL_LIBS})

And that is all the required information you need. Now we are glad we have Cmake to make things easier for us don't we. ;)Here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.6)project(embedded_mysql)set(CMAKE_CXX_STANDARD 14)set(SOURCE_FILES main.cpp)add_executable(embedded_mysql ${SOURCE_FILES})find_package( ZLIB REQUIRED )if ( ZLIB_FOUND )    include_directories( ${ZLIB_INCLUDE_DIRS} )    target_link_libraries( embedded_mysql ${ZLIB_LIBRARIES} )endif( ZLIB_FOUND )execute_process(COMMAND mysql_config --include        OUTPUT_VARIABLE MYSQL_INCLUDE)execute_process(COMMAND mysql_config --libmysqld-libs        OUTPUT_VARIABLE MYSQL_LIBS)string(STRIP ${MYSQL_LIBS} MYSQL_LIBS)target_compile_options(embedded_mysql PUBLIC ${MYSQL_INCLUDE})target_link_libraries(embedded_mysql ${MYSQL_LIBS})

You can see the code in github here