1

STARTIG WITH MYSLQ CONNECTOR - here is the cmakelists.txt file that I use to generate build system using CMAKE GUI on windows (cause I actually have to make console application in c++ for windows).

cmake_minimum_required(VERSION 3.16) # Or whatever version you use
set(CMAKE_CXX_STANDARD 17)
# THIS HAS TO COME BEFORE THE PROJECT LINE

# THIS HAS TO COME BEFORE THE PROJECT LINE
project(test VERSION 0.0.0 LANGUAGES C CXX)
project(main)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:\\connector")
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}\\include)

link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}\\lib64\\v14)
add_executable(test main.cpp)
target_link_libraries(test C:/connector/lib64/vs14/mysqlcppconn.lib)

target_compile_features(test PRIVATE cxx_range_for)  
set(STDFS_LIB stdc++fs)

and here is the code I am trying to run (main.cpp) -

#include <stdio.h>  
 #include <stdint.h>
#include <cstdint>
#include <sys/types.h>

#include <jdbc/cppconn/driver.h>
#include <jdbc/cppconn/exception.h>
#include <jdbc/cppconn/resultset.h>
#include <jdbc/cppconn/statement.h>
#include <jdbc/cppconn/prepared_statement.h>
//#include <mysql/mysql.h>


int main()
{
    try
        {
            sql::Driver *driver;
            sql::Connection *con;
            //sql::Statement *stmt;
            sql::ResultSet *res;
            sql::PreparedStatement *pstmt;

            /* Create a connection */
            driver = get_driver_instance();
        
    } catch (sql::SQLException &e)
        {
            ///nav implementēts vairāk info
            //cout << "# ERR: SQLException in " << __FILE__;
            //cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
            /* what() (derived from std::runtime_error) fetches error message */
            //cout << "# ERR: " << e.what();
            //cout << " (MySQL error code: " << e.getErrorCode();
            //cout << "# ERR: SQLException in " << endl;
        }
    return 0;
}

I am using GNU make to compile it, and I get following error -

C:\Users\FL\Desktop\TEST2>make
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.obj
In file included from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/resultse
t.h:36:0,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:8:
C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/config.h:101:19: error: confli
cting declaration 'typedef long int int32_t'
 typedef __int32   int32_t;
                   ^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_trait
s.h:420,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_map.h:
63,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\map:61,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/connecti
on.h:36,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/driver.h
:36,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:6:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int3
2_t'
 typedef int   int32_t;
               ^~~~~~~
In file included from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/resultse
t.h:36:0,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:8:
C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/config.h:105:26: error: confli
cting declaration 'typedef long unsigned int uint32_t'
 typedef unsigned __int32 uint32_t;
                          ^~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_trait
s.h:420,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_map.h:
63,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\map:61,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/connecti
on.h:36,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/driver.h
:36,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:6:
c:\mingw\include\stdint.h:63:19: note: previous declaration as 'typedef unsigned
 int uint32_t'
 typedef unsigned  uint32_t;
                   ^~~~~~~~
In file included from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/statemen
t.h:36:0,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:9:
C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/config.h:101:19: error: confli
cting declaration 'typedef long int int32_t'
 typedef __int32   int32_t;
                   ^~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_trait
s.h:420,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_map.h:
63,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\map:61,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/connecti
on.h:36,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/driver.h
:36,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:6:
c:\mingw\include\stdint.h:62:15: note: previous declaration as 'typedef int int3
2_t'
 typedef int   int32_t;
               ^~~~~~~
In file included from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/statemen
t.h:36:0,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:9:
C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/config.h:105:26: error: confli
cting declaration 'typedef long unsigned int uint32_t'
 typedef unsigned __int32 uint32_t;
                          ^~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\stdint.h:9:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\cstdint:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\char_trait
s.h:420,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\stdexcept:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\array:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\tuple:39,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_map.h:
63,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\map:61,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/connecti
on.h:36,
                 from C:/PROGRA~1/MySQL/MYSQLC~1.0/include/jdbc/cppconn/driver.h
:36,
                 from C:\Users\FL\Desktop\TEST2\main.cpp:6:
c:\mingw\include\stdint.h:63:19: note: previous declaration as 'typedef unsigned
 int uint32_t'
 typedef unsigned  uint32_t;
                   ^~~~~~~~
make[2]: *** [CMakeFiles/test.dir/main.cpp.obj] Error 1
make[1]: *** [CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

This means something is wrong with ether gcc or g++, right? or should I be using make from mingw installation directory instead of make from other directory?

C:\Users\FL>make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-pc-mingw32

UPDATE: WITH ALL THE CHANGES ERROR NOW IS:

[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.obj
[100%] Linking CXX executable test.exe
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x22):
ndefined reference to `check(std::__cxx11::basic_string<char, std::char_traits<
har>, std::allocator<char> > const&)'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x76):
ndefined reference to `check(std::map<std::__cxx11::basic_string<char, std::cha
_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::ch
r_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<ch
r, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<s
d::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > c
nst, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<ch
r> > > > > const&)'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles\test.dir\build.make:100: test.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:90: all] Error 2```

SO MAYBE MINGW DOES NOT WORK WITH CONNECTOR PRECOMPILED BUT ONLY COMPILED WITH SAME COMPILER, SO:

SO WHAT I DID FIRST WAS downloading mysql connector 8.0 pre-compiled for windows from https://dev.mysql.com/downloads/connector/cpp/ installed it on C:\connector

and then I thought maybe it just don't work with MINGW "compiler" at all so I found this article https://forums.mysql.com/read.php?117,426293,426927

I downloaded source for it on https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-source-distribution.html

and compiling using cmake I got this error and did not get to makefiles that are needed to use "make" command -

The C compiler identification is GNU 9.2.0
The CXX compiler identification is GNU 9.2.0
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped
Detecting C compile features
Detecting C compile features - done
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped
Detecting CXX compile features
Detecting CXX compile features - done
BIG_ENDIAN: 0
Building version 8.0.24
Building on system: Windows-6.1.7601 (AMD64)
Using cmake generator: MinGW Makefiles
Using toolset: 
Building 64bit code
Building shared connector library
Configuring CDK as part of MySQL_CONCPP project
Looking for SSL library.
CMake Error at cdk/cmake/DepFindSSL.cmake:79 (message):
  Cannot find appropriate system libraries for SSL.  Make sure you've
  specified a supported SSL version.  Consult the documentation for WITH_SSL
  alternatives
Call Stack (most recent call first):
  cdk/cmake/DepFindSSL.cmake:354 (main)
  cdk/cmake/dependency.cmake:42 (include)
  cdk/CMakeLists.txt:96 (find_dependency)


Setting up Protobuf.
==== Configuring Protobuf build using cmake generator: MinGW Makefiles   -DCMAKE_SYSTEM_NAME=Windows;-DCMAKE_SYSTEM_VERSION=6.1.7601
CMake Deprecation Warning at CMakeLists.txt:44 (cmake_minimum_required):

  Compatibility with CMake < 2.8.12 will be removed from a future version of

  CMake.



  Update the VERSION argument <min> value or use a ...<max> suffix to tell

  CMake that the project does not need compatibility with older versions.





-- The C compiler identification is GNU 9.2.0

-- The CXX compiler identification is GNU 9.2.0

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

-- Check for working C compiler: C:/MinGW/bin/gcc.exe - skipped

-- Detecting C compile features

-- Detecting C compile features - done

-- Detecting CXX compiler ABI info

-- Detecting CXX compiler ABI info - done

-- Check for working CXX compiler: C:/MinGW/bin/g++.exe - skipped

-- Detecting CXX compile features

-- Detecting CXX compile features - done

-- BIG_ENDIAN: 0

-- Looking for pthread.h

-- Looking for pthread.h - found

-- Performing Test CMAKE_HAVE_LIBC_PTHREAD

-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success

-- Found Threads: TRUE  

Using pthreads for protobuf code


-- Configuring done

-- Generating done

-- Build files have been written to: C:/Users/FL/Desktop/mysql-connector-cpp/cdk/protobuf

==== Protobuf build configured.
Processor Count: 1
Setting up RapidJSON.
Skipping second declaration of config option: THROW_AS_ASSERT (found in: C:/Users/FL/Desktop/mysql-connector-cpp/cdk/CMakeLists.txt)
Performing Test HAVE_STATIC_ASSERT
Performing Test HAVE_STATIC_ASSERT - Success
Performing Test HAVE_IS_SAME
Performing Test HAVE_IS_SAME - Failed
CMake Deprecation Warning at cdk/extra/zlib/CMakeLists.txt:24 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Deprecation Warning at cdk/extra/zlib/CMakeLists.txt:37 (CMAKE_POLICY):
  The OLD behavior for policy CMP0075 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.


Looking for sys/types.h
Looking for sys/types.h - found
Looking for stdint.h
Looking for stdint.h - found
Looking for stddef.h
Looking for stddef.h - found
Check size of off64_t
Check size of off64_t - done
Looking for fseeko
Looking for fseeko - found
Looking for unistd.h
Looking for unistd.h - found
CMake Deprecation Warning at cdk/extra/zstd/CMakeLists.txt:11 (CMAKE_MINIMUM_REQUIRED):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


ZSTD_LEGACY_SUPPORT not defined!
Performing Test HAVE_SHARED_PTR
Performing Test HAVE_SHARED_PTR - Success
Performing Test HAVE_SYSTEM_ERROR
Performing Test HAVE_SYSTEM_ERROR - Success
Check size of wchar_t
Check size of wchar_t - done
Looking for sys/endian.h
Looking for sys/endian.h - not found
Looking for sys/byteorder.h
Looking for sys/byteorder.h - not found
CMake Deprecation Warning at cdk/core/CMakeLists.txt:30 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


Wrote configuration header: C:/Users/FL/Desktop/mysql-connector-cpp/cdk/include/mysql/cdk/config.h
Preparing to merge SHARED library: connector (xapi;devapi)
Connector library name: mysqlcppconn8-2
Building version 8.0.24
Generating INFO_SRC
Generating INFO_BIN
Install location: C:/Users/FL/MySQL/MySQL Connector C++ 
Connector libraries will be installed at: lib64

Project configuration options:

: BUILD_STATIC: OFF
Build static version of connector library

: WITH_SSL: system
Either 'system' to use system-wide OpenSSL library, or custom OpenSSL location. (default : system)

: WITH_JDBC: OFF
Whether to build a variant of connector library which implements legacy JDBC API

Configuring incomplete, errors occurred!
See also "C:/Users/FL/Desktop/mysql-connector-cpp/CMakeFiles/CMakeOutput.log".
See also "C:/Users/FL/Desktop/mysql-connector-cpp/CMakeFiles/CMakeError.log".

** BIT LATEEERRR ****

OKAAAAAAY SO I'M TRYING TO COMPILE MARIADB https://mariadb.com/docs/clients/connector-cpp/ using MINGW with following cmakelists.txt config

cmake_minimum_required(VERSION 3.16) # Or whatever version you use
set(CMAKE_CXX_STANDARD 17)
# THIS HAS TO COME BEFORE THE PROJECT LINE
project(test VERSION 0.0.0 LANGUAGES C CXX)
project(main)
set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:\\mariaconnector")
include_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}\\include)
add_executable(test main.cpp)
target_compile_features(test PRIVATE cxx_range_for)  
set(STDFS_LIB stdc++fs)

my code is:

#include <stdio.h>  
#include <stdint.h>
#include <cstdint>
#include <sys/types.h>
#include <iostream>
#include <mariadb/conncpp.hpp>

//#include <mysql/mysql.h>
using std::uint32_t;

// Function to print Contacts
void printContacts(std::shared_ptr<sql::Statement> &stmnt)
{
   try
   {
      // Execute SELECT Statement
      std::unique_ptr<sql::ResultSet> res(
            stmnt->executeQuery("SELECT first_name, last_name, email FROM test.contacts")
         );

      // Loop over Result-set
      while (res->next())
      {
         // Retrieve Values and Print Contacts
         std::cout << "- "
            << res->getString("first_name")
            << " "
            << res->getString("last_name")
            << " <"
            << res->getString("email")
            << ">"
            << std::endl;
      }
   }

   // Catch Exception
   catch (sql::SQLException& e)
   {
      std::cerr << "Error printing contacts: "
         << e.what() << std::endl;
   }
}

// Main Process
int main(int argc, char **argv)
{
   try
   {
      // Instantiate Driver
      sql::Driver* driver = sql::mariadb::get_driver_instance();

      // Configure Connection
      // The URL or TCP connection string format is
      // ``jdbc:mariadb://host:port/database``.
      sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");

      // Use a properties map for the user name and password
      sql::Properties properties({
            {"user", "db_user"},
            {"password", "db_user_password"}
         });

      // Establish Connection
      // Use a smart pointer for extra safety
      std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));

      // Create a Statement
      // Use a smart pointer for extra safety
      std::shared_ptr<sql::Statement> stmnt(conn->createStatement());

      printContacts(stmnt);

      // Close Connection
      conn->close();
   }

   // Catch Exceptions
   catch (sql::SQLException &e)
   {
      std::cerr << "Error Connecting to MariaDB Platform: "
         << e.what() << std::endl;

      // Exit (Failed)
      return 1;
   }

   // Exit (Success)
   return 0;
}

I get this error-

C:\Users\FL\Desktop\testMARIA>make
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.obj
[100%] Linking CXX executable test.exe
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xd4):
ndefined reference to `__imp__ZN3sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xff):
ndefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x16a):
undefined reference to `__imp__ZN3sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x190):
undefined reference to `__imp__ZN3sqllsERSoRKNS_9SQLStringE'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1d4):
undefined reference to `__imp__ZN3sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1fa):
undefined reference to `__imp__ZN3sqllsERSoRKNS_9SQLStringE'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x23e):
undefined reference to `__imp__ZN3sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x264):
undefined reference to `__imp__ZN3sqllsERSoRKNS_9SQLStringE'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x292):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2a2):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2b2):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2c2):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2d2):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x2e2):
more undefined references to `__imp__ZN3sql9SQLStringD1Ev' follow
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x470):
undefined reference to `__imp__ZN3sql7mariadb19get_driver_instanceEv'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x48a):
undefined reference to `__imp__ZN3sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x5e3):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x67f):
undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_ED1Ev[_ZNSt4pairIKN3sql9SQLStringES1_ED1Ev]+0x1a): undef
ned reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_ED1Ev[_ZNSt4pairIKN3sql9SQLStringES1_ED1Ev]+0x2a): undef
ned reference to `__imp__ZN3sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStri
gES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_]+0x33): undefined reference to `__imp__ZN3s
l9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStri
gES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_]+0x56): undefined reference to `__imp__ZN3s
l9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStri
gES1_EC1IRA5_KcRA8_S5_Lb1EEEOT_OT0_]+0x6b): undefined reference to `__imp__ZN3s
l9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStr
ngES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_]+0x33): undefined reference to `__imp__ZN
sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStr
ngES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_]+0x56): undefined reference to `__imp__ZN
sql9SQLStringC1EPKc'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_[_ZNSt4pairIKN3sql9SQLStr
ngES1_EC1IRA9_KcRA17_S5_Lb1EEEOT_OT0_]+0x6b): undefined reference to `__imp__ZN
sql9SQLStringD1Ev'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNKSt4
essIN3sql9SQLStringEEclERKS1_S4_[_ZNKSt4lessIN3sql9SQLStringEEclERKS1_S4_]+0x22
: undefined reference to `__imp__ZNK3sql9SQLStringltERKS0_'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC1ERKS3_]+0x24
: undefined reference to `__imp__ZN3sql9SQLStringC1ERKS0_'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC1ERKS3_]+0x40
: undefined reference to `__imp__ZN3sql9SQLStringC1ERKS0_'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32
bin/ld.exe: CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text$_ZNSt4p
irIKN3sql9SQLStringES1_EC1ERKS3_[_ZNSt4pairIKN3sql9SQLStringES1_EC1ERKS3_]+0x55
: undefined reference to `__imp__ZN3sql9SQLStringD1Ev'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles\test.dir\build.make:99: test.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:90: all] Error 2

:>

Ronalds Mazītis
  • 323
  • 2
  • 5
  • 18

4 Answers4

1

Both connectors MySQL as well as MariaDB (which share the same heritage) are intended to be compiled and used with Visual Studio on Windows only. You will find a lot of previous questions on StackOverflow regarding it. The problem with them is that they define a lot of structures that are already defined in the standard library and then link to the standard library as well.

I suggest you to either switch to Visual Studio or to a Linux system. If you have to use GCC under Windows then look for another connector. These problems will not be solved easily. If so the solutions are unlikely to be portable and might not work with future versions of the two connectors. You can have a look at the alternatives SQLite and SQLAPI++.


First problem: fixed-width integers

The first problem you mention is actually related to the fixed-width integer types and 32-bit operating systems defined in the header-files. There are the traditional integer types like char, short, int, long and long long but additionally the aforementioned fixed-width integers.

The MySql Connector defines the int32_t data type in config.h and also the standard C++ library defines them: MySql defines the int32_t with the compiler data type __int32

typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;

which surprisingly turns out to be the long int data types

typedef long int int32_t;
typedef long unsigned int uint32_t;

while the standard library is defining them as regular int

typedef int int32_t;
typedef unsigned int uint32_t;

long integer data types are guaranteed to be at least 32-bit: On a 32-bit architecture a long int is 32-bit (just like an int) while for 64-bit they have different lengths - a long int is 64-bit and an int is only 32-bit (see here). This means actually for a 32-bit system these definitions should be identical but yet the compiler thinks they are conflicting.

The MySql header is wrapped by various defines (I put an explanation next to them so you can understand why the proposed solutions given below actually work) that decide if the corresponding data types should be defined or not

// Only define for 32-bit compilation
#if defined(_WIN32)
// Don't define if this custom flag is activated
#ifndef CPPCONN_DONT_TYPEDEF_MS_TYPES_TO_C99_TYPES
// Do not define for Visual Studio 2010 and later (but use C++ standard library instead)
#if _MSC_VER >= 1600
#include <stdint.h>
#else
// Only define if HAVE_MS_INT32 (another custom flag) is set to true (1)
#ifdef HAVE_MS_INT32
typedef __int32 int32_t;
#endif
// Some more data type defines...
#endif
#endif
#endif

Solutions

Based on the structure of the header file given above there are a couple of solutions for this. Some might be more viable while others less.

  • Clearly you could not include any the type definitions in cstdint and stdint.h and live with the MySql defines. This would actually be quite limiting as sooner or later likely another standard library header will include it and it might result in forcing you to not use the standard library at all which might be very limiting.

  • You could abandon the 32-bit build tool-chain you are using altogether, switch to a **64-bit compiler and compile for 64-bit. In this case this should not happen as the header config.h in MySql is only included for 32-bit systems as stated above! If there is no good reason that your project should be 32-bit that is what I would actually do. Talking about your compiler: You seem to be using GCC 6.3.0 which was released back in 2016 and actually does not fully support the C++17 language standard you are telling it to compile with CMAKE_CXX_STANDARD 17 in your CMake-file. You might want to use another newer compiler in case you want use C++17 features extensively. Otherwise C++14 is not too bad either.

  • You could use Visual Studio 2010 (version 1600) or later for compilation as in this case the header will automatically include the definitions from the standard instead of defining its own ones.

  • You could define the pre-processor flag #define CPPCONN_DONT_TYPEDEF_MS_TYPES_TO_C99_TYPES on top of your code (or inside the IDE you are using for your project) as if this flag is set the config.h file won't define any data types.

  • Similarly you could also solve it by opening MYSQLC~1.0/include/jdbc/cppconn/config.h and modify the pre-processor directives from

    #define HAVE_MS_INT32  1
    #define HAVE_MS_UINT32 1
    

    to

    #define HAVE_MS_INT32  0
    #define HAVE_MS_UINT32 0
    

    This will de-activate the corresponding defines for all programs you are also writing in the future that include this header.


Second problem: Linking to libraries compiled with Visual Studio

The second error message you get is actually related to linking the library. On Windows libraries compiled with different compilers are generally not compatible. This means a program compiled with GCC can't include libraries compiled with Visual Studio. In your case the DLL was compiled with Visual Studio and therefore the linking to your GCC program fails.

As also mentioned here you can force CMake to use MinGW instead of Visual Studio with cmake -G "MinGW Makefiles" but I have tried it and it neither works with MariaDB nor MySQL.

Using MSYS2 in MySQL I get a cryptic error related to OpenSSL while on MariaDB following the offical guide and then using

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -G "MinGW Makefiles" -DCONC_WITH_UNIT_TESTS=Off -DCONC_WITH_MSI=OFF -DWITH_SSL=SCHANNEL .
cmake --build . --config RelWithDebInfo

I have to do a couple of manual modifications, such as modifying /src/CArrayImp.h and change line 59 to 63 from

#ifndef _WIN32
# define ZEROI64 0LL
#else
# define ZEROI64 0I64
#endif

to

#define ZEROI64 0LL

as 0I64 is only defined by Visual Studio. Furthermore one has to remove the template instantiation in CArray.cpp but I still end up with a The system cannot find the path specified. error message. Similarly I wasn't able to get it to compile in Cygwin.


SQL C++ connector alternatives

I have no solution for the last problem but you might want to have a look at alternatives. You could download SQLite from source and compile it. According to the installation guide from source it is compatible with MinGW but it is only lightweight. So should be the Shareware SQLAPI++. According to their "Order" page the trial version for Windows is fully functional

You can use trial version for evaluation purposes only. The evaluation version has no limits or functional differences from registered version. However, evaluation version displays a registration message every time an application performs its first database connection (Windows version).

After registration you will:

  • receive a non-trial version of the library, including its full source code
  • continue to receive free lifetime email support
  • get one year of free bug fixing and new version upgrades

Both should support MySql: e.g. see here.


tl;dr: Use the MySQL and MariaDB connectors on Windows in Visual Studio only. If you can't use Visual Studio have a look at the alternative C++ SQL connectors such SQLite and SQLAPI++ instead.

2b-t
  • 2,414
  • 1
  • 10
  • 18
  • Okay, but Google says - "C++17 features are available since GCC 5. This mode is the default in GCC 11; it can be explicitly selected with the -std=c++17 command-line flag, or -std=gnu++17 to enable GNU extensions as well." – Ronalds Mazītis May 02 '21 at 08:25
  • I upgraded my GCC version, by downloading mingw from https://nuwen.net/mingw.html#install it came with GCC 9.2.0 and now I got this error if I remove "target_link_libraries(test mysqlcppconn)", - https://paste.ubuntu.com/p/mTGH8cdbmj/ cause otherwise I get "cannot find -lmysqlcppconn." – Ronalds Mazītis May 02 '21 at 09:32
  • @RonaldsMazītis Some C++17 features but not all of them as can be seen on the list I sent you. The final C++17 standard was released in [December 2017](https://en.wikipedia.org/wiki/C%2B%2B17) while the different versions of [GCC 5 were released between 2015 and 2017](https://gcc.gnu.org/gcc-5/), and [GCC 6.3 in December 2016](https://gcc.gnu.org/gcc-6/) – 2b-t May 02 '21 at 10:00
  • Do not remove `target_link_libraries` it will combine your code with the MySql library code. Can you check where your MySql library is located? You put `C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0` inside your Makefile. Is that really the correct path where MySql is located? If yes, what files are in there? Only the headers or also `.lib` and `.dll` files? – 2b-t May 02 '21 at 10:10
  • Downloaded it from somewhere here - https://dev.mysql.com/doc/connector-j/8.0/en/ and that is default installation location. – Ronalds Mazītis May 02 '21 at 17:19
  • @RonaldsMazītis Check the contents of the folder and what is in it. – 2b-t May 02 '21 at 17:36
  • https://paste.ubuntu.com/p/Bv5J9mXWhC/ - the error how C:\\Program Files\\MySQL\\MySQL Connector C++ 8.0 looks - https://ibb.co/y49wYZs also I changed config.h in include/jdbc/cppconn/config.h – Ronalds Mazītis May 03 '21 at 11:40
  • What is more interesting is what is inside the `/lib64` and inside its subfolders. The error message states that the linking to the MySql library fails ( `mysqlcppconn`). So make sure a file named `mysqlcppconn.lib` exists somewhere in that folder. Potentially in a subfolder `v14` or so. Then correct your CMake file to point to the correct location. So something like `link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/v14)`... – 2b-t May 03 '21 at 19:07
  • hmm it's there, I also tried adding the line and replacing old one (link_directories(${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64) - https://ibb.co/wpCztrW – Ronalds Mazītis May 03 '21 at 19:44
  • Weird, you could try modifying `target_link_libraries(test mysqlcppconn)` to `target_link_libraries(test ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn.lib)` and if that does not work to `target_link_libraries(test ${FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR}/lib64/vs14/mysqlcppconn-static.lib)`. – 2b-t May 03 '21 at 20:11
  • If all this does not work you will have to download Visual Studio. This is what it is actually intended to be used with. If the DLL is build with it (You can see that inside the `INFO_BIN` files under `compiler`. Open it with a regular text editor.) you should also build your code with it. – 2b-t May 03 '21 at 20:15
  • You can find a guide for it [here](https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-apps-windows-notes.html) under `Building Connector/C++ Applications with Microsoft Visual Studio`. This is how I am actually using it. – 2b-t May 03 '21 at 20:19
  • https://paste.ubuntu.com/p/PnRghSDvWj/ - I'm trying this cmakelist. – Ronalds Mazītis May 03 '21 at 21:01
  • Try again with "C:/Program Files/MySQL/MySQL Connector C++ 8.0" as the path. As you can see it has problems with the double backslash: Somehow it is looking for "C:Program FilesMySQLMySQL Connector C++ 8.0/lib64/vs14/mysqlcppconn.lib" – 2b-t May 03 '21 at 21:06
  • https://paste.ubuntu.com/p/x6p3RbsffF/ - I copied connector to c drive, named it connector and tried to use following versions of includes. Also this seem to be only right set(FULL_PATH_TO_MYSQL_CONNECTOR_CPP_DIR "C:\\connector") – Ronalds Mazītis May 03 '21 at 21:59
  • Did you put the includes into the CMake-file? That is not where they are supposed to go... Give it a try with Visual Studio as said. I sadly do not have the same configuration as you have to test it out. I am using it in Visual Studio and it is straight forward to use there. Is there a particular reason you want to use GCC with Mingw? – 2b-t May 03 '21 at 22:04
  • No I did not. It's just what main.cpp is inside. I don't have machine that would be able to run Visual Studio due to resources. – Ronalds Mazītis May 03 '21 at 22:09
  • Hmmm, ok, I see... Maybe I have time to try and set-up the same tool-chain on my computer and play around with it and then let you know. Right now I have no other clue what you could try... Sorry... As an alternative you could give [MariaDB](https://github.com/mariadb-corporation/mariadb-connector-cpp) a go... The interface is the same as MySql JDBC. – 2b-t May 03 '21 at 22:25
  • I need this for a server that stores some medical data, and it uses mysql for some reason. (BTW I updated the article when I got to point where it started linking .exe.) – Ronalds Mazītis May 03 '21 at 22:30
  • I am not sure if you are confusing two things here. At least it was confusing to me at first... While connector and data base have to be compatible you do not necessarily need to use the MySql connector with a MySql data base! See the compatibility of MariaDB [here](https://mariadb.com/kb/en/mariadb-vs-mysql-compatibility/). – 2b-t May 03 '21 at 23:12
  • So You are telling me "mariadb connector" will work with mysql database? – Ronalds Mazītis May 04 '21 at 09:50
  • Yes, see e.g. [here](https://dba.stackexchange.com/a/224189). Furthermore it has a different license making it favourable for commercial projects. – 2b-t May 04 '21 at 11:53
  • So https://mariadb.com/docs/clients/connector-cpp/ cmakelists.txt for this has to be written, could You please help me make one? – Ronalds Mazītis May 05 '21 at 09:48
  • I can give it a go tomorrow. But I can't guarantee that I will get it working. Windows libraries can be a pain in the ass... – 2b-t May 05 '21 at 21:26
  • @RonaldsMazītis I have tried the entire evening as well... I think you are out of luck. Either you install Visual Studio or you are unlikely to get it working... – 2b-t May 06 '21 at 22:59
  • @RonaldsMazītis Added some alternatives to my post that you could have a look at namely SQLite and SQLAPI++. Good luck. – 2b-t May 07 '21 at 07:57
1

This is how I build the MariaDB Client Library with MinGW-w64 (I use the compiler from http://winlibs.com/) under MSYS2 shell (https://www.msys2.org/):

wget https://downloads.mariadb.org/interstitial/connector-c-3.1.11/mariadb-connector-c-3.1.11-src.tar.gz
tar xfz mariadb-connector-c-3.1.11-src.tar.gz
cd mariadb-connector-c-3.1.11-src

# fix cmake/ConnectorName.cmake (version >= 3.1.11)
patch -ulbf cmake/ConnectorName.cmake << EOF
@@ -24,3 +24,3 @@
     SET(MACHINE_NAME "32")
-  END()
+  ENDIF()
 ENDIF()
EOF

# set install location
INSTALLPREFIX=D:/Prog/mariadb-connector

# configure
cmake.exe -GNinja -DCMAKE_INSTALL_PREFIX:PATH=$INSTALLPREFIX -DCMAKE_BUILD_TYPE:STRING=Release -DWITH_CURL:BOOL=OFF -DWITH_DYNCOL:BOOL=ON -DWITH_EXTERNAL_ZLIB:BOOL=ON -DWITH_MYSQLCOMPAT:BOOL=ON -DWITH_SSL:STRING=GNUTLS -DWITH_UNIT_TESTS:BOOL=OFF -DCMAKE_SHARED_LINKER_FLAGS=-Wl,--enable-stdcall-fixup -S. -Bbuild_win

# build and install
ninja -Cbuild_win install/strip

# move library files out of mariadb folder
mv -f $INSTALLPREFIX/lib/mariadb/*.dll $INSTALLPREFIX/bin/ &&
mv -f $INSTALLPREFIX/lib/mariadb/liblibmariadb.dll.a $INSTALLPREFIX/lib/libmariadb.dll.a &&
mv -f $INSTALLPREFIX/lib/mariadb/*.a $INSTALLPREFIX/lib/

# make copies for MySQL compatibility
cp -f $INSTALLPREFIX/lib/libmariadb.dll.a $INSTALLPREFIX/lib/libmariadbclient.dll.a &&
cp -f $INSTALLPREFIX/lib/libmariadbclient.dll.a $INSTALLPREFIX/lib/libmysqlclient.dll.a &&
cp -f $INSTALLPREFIX/lib/libmariadbclient.dll.a $INSTALLPREFIX/lib/libmysqlclient_r.dll.a &&
cp -f $INSTALLPREFIX/lib/libmariadbclient.a $INSTALLPREFIX/lib/libmysqlclient.a &&
cp -f $INSTALLPREFIX/lib/libmariadbclient.a $INSTALLPREFIX/lib/libmysqlclient_r.a &&
cp -f $INSTALLPREFIX/lib/libmariadbclient.a $INSTALLPREFIX/lib/libmariadb.a &&
cp -f $INSTALLPREFIX/bin/mariadb_config.exe $INSTALLPREFIX/bin/mysql_config.exe

Of course before building you need to have the prerequisites already installed (zlib and gnutls).

If you don't want to use Ninja for some reason you could replace -GNinja with -G"MSYS Makefiles" and ninja -Cbuild_win install/strip with make -Cbuild_win install-strip.

Then to build libraries that depend on MySQL client I add -I$INSTALLPREFIX/include/mariadb to the compiler flags.

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40
  • I think currently the libmariadbclient library can be directly installed from the pacman command in the msys2, see here: [Package: mingw-w64-x86_64-libmariadbclient - MSYS2 Packages](https://packages.msys2.org/package/mingw-w64-x86_64-libmariadbclient). – ollydbg23 Jun 23 '23 at 23:32
1

This is not answer for your problems, but too long for a comment...

A few words from the maintainer of MariaDB Connector/C (and former maintainer of MySQL Connector/C):

MinGW was never a supported platform/environment, however we tried our best to support it (like many other not officially unsupported platforms). The main problem was (or still is), that MinGW is always behind the latest/stable Windows release. We tried to add a custom include file where some things could be added, e.g. missing definitions, but it didn't work anymore after we decided to use Schannel in favor of OpenSSL for secure (tls) connections on Windows platforms.

If someone has a good solution, e.g. by adding optional include files I'm open to merge your pull request. Modifying C code with MinGW conditional defines is not an option.

Your question was tagged with Connector/C++ - however both C++ conncetors from MariaDB and MySQL are using Connector/C.

about previous answer: Building with GnuTLS works, however there are some limitations. E.g MySQL authentication plugins like caching_sha2_password will not work unless you will use a secure (tls) connection.

Georg Richter
  • 5,970
  • 2
  • 9
  • 15
  • I also had issues when building with MinGW but this explains the reasons. Thanks for the detailed explanation. – Nuclear Nov 02 '22 at 05:50
0

There is a library named SOCI - The C++ Database Access Library , which has many back-ends support such as: SQLite3, MySQL and others. If you use this library under Windows + MSYS2, you can easily installed it by the command pacman -S mingw-w64-x86_64-soci (see here: Package: mingw-w64-x86_64-soci - MSYS2 Packages).

This is a sample code to demonstrate the usage:

#include <soci/soci.h>
#include <soci/mysql/soci-mysql.h>
#include <iostream>

int main()
{
    try
    {
        soci::session sql(soci::mysql, "dbname=testdb user=root password=123456");

        int count;
        sql << "SELECT COUNT(*) FROM testtable", soci::into(count);

        std::cout << "There are " << count << " rows in testtable" << std::endl;


        soci::rowset<soci::row> rows = (sql.prepare << "SELECT * FROM testtable");

        for(soci::row& row : rows)
        {
            std::cout << "id = " << row.get<int>("id")
                      << ", name = " << row.get<std::string>("name")
                      << ", age = " << row.get<int>("age")
                      << std::endl;
        }

    }
    catch(const soci::soci_error& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

Hope that can help you and others.

ollydbg23
  • 1,124
  • 1
  • 12
  • 38