c++cmakemariadbpoco-libraries

How do I use MariaDB with POCO (https://pocoproject.org/), using C++ on Windows. Like how do I link in the MariaDB libraries etc. in CMake


this is the first time I've tried to link MariaDB to a C++ program in CMake

MariaDB MySQL version: 10.5.21 (what server uses)

The CMakeLists.txt that I have currently, that doesn't work looks like this:


`
cmake_minimum_required(VERSION 3.20)

project(Dance_Syllabuses_Backend VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(MARIADB_DIR "C:/Program Files/MariaDB 10.5")
set(MARIADB_INCLUDE_DIR "${MARIADB_DIR}/include")
set(MARIADB_LIB_DIR "${MARIADB_DIR}/lib")

find_library(MARIADB_LIB NAMES mariadb PATHS ${MARIADB_LIB_DIR})

if (MARIADB_LIB)
    message(STATUS "Found MariaDB: ${MARIADB_LIB}")
else()
    message(FATAL_ERROR "MariaDB not found")
endif()

set(ENABLE_NET OFF CACHE BOOL "Enable Net" FORCE)
set(ENABLE_ENCODINGS OFF CACHE BOOL "Enable Encodings" FORCE)
set(ENABLE_XML OFF CACHE BOOL "Enable XML" FORCE)
set(ENABLE_JSON OFF CACHE BOOL "Enable JSON" FORCE)
set(ENABLE_MONGODB OFF CACHE BOOL "Enable MongoDB" FORCE)
set(ENABLE_DATA_SQLITE OFF CACHE BOOL "Enable Data SQlite" FORCE)
set(ENABLE_REDIS OFF CACHE BOOL "Enable Redis" FORCE)
set(ENABLE_ZIP OFF CACHE BOOL "Enable Zip" FORCE)
set(ENABLE_PAGECOMPILER OFF CACHE BOOL "Enable PageCompiler" FORCE)
set(ENABLE_PAGECOMPILER_FILE2PAGE OFF CACHE BOOL "Enable File2Page" FORCE)
set(ENABLE_ACTIVERECORD OFF CACHE BOOL "Enable ActiveRecord" FORCE)
set(ENABLE_ACTIVERECORD_COMPILER OFF CACHE BOOL "Enable ActiveRecord Compiler" FORCE)
set(ENABLE_DATA_MYSQL ON CACHE BOOL "Enable MySQL" FORCE)

add_subdirectory(external/POCO)

add_executable(application
    include/customer_dao.hpp
    include/customer.hpp
    include/scope_guard.hpp
    src/customer_dao.cpp
    src/customer.cpp
    src/main.cpp)
target_include_directories(application PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(application PRIVATE Poco::Data Poco::DataMySQL)
`

Tried to build it and expected it to find MariaDB and work and it didn't

Edit: To add more clarity to post

Receive error message "MariaDB not found", which is something we outputted. Assumption was that POCO will care about those variables we declared and then know to use MariaDB and where to find MariaDB.

MYSQL_ROOT_DIR - Set root installation path where to find include path and libraries of MySQL or MariaDB

MYSQL_ROOT_INCLUDE_DIRS - Set include paths where to find MySQL or MariaDB headers

MYSQL_ROOT_LIBRARY_DIRS - Set library paths where to find MySQL or MariaDB libraries

These are the paths that POCO supposedly looks at. https://docs.pocoproject.org/current/00200-GettingStarted.html If we can set those correctly then it might be able to find MariaDB.


Solution

  • It looks like that you installed MariaDB Server without development component (which contains header files and libraries). The .dll is not enough, for linking you will need the symbols from .lib files.

    Also note that include files are not stored in ${MARIADB_DIR}/include but in ${MARIADB_DIR}/include/mysql