MySQL和MariaDB库在C++中使用cmake,mingw

i86rm4rw  于 2023-06-25  发布在  Mysql
关注(0)|答案(4)|浏览(187)

STARTIG WITH MYSLQ CONNECTOR -这里是cmakelists.txt文件,我使用它在Windows上使用CMAKE GUI生成构建系统(因为我实际上必须在c++中为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)

这里是我尝试运行的代码(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;
}

我使用GNU make编译它,我得到以下错误-

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

这意味着以太网gcc或g++有问题,对吗?或者我应该使用mingw安装目录中的make而不是其他目录中的make?

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

更新:所有更改错误现在是:

[ 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```

因此,可能MINGW无法使用预先编译的连接器,但仅使用相同的编译器编译,因此:

所以我首先做的是从https://dev.mysql.com/downloads/connector/cpp/下载mysql connector 8.0预编译的windows安装在C:\connector上
然后我想也许它只是不工作与MINGW“编译器”在所有所以我找到了这篇文章https://forums.mysql.com/read.php?117,426293,426927
我在https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-source-distribution.html上下载了源代码
和编译使用cmake我得到了这个错误,并没有得到的makefile是需要使用“make”命令-

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".

迟一点**
OK,所以我正在尝试使用MINGW编译MARIADB https://mariadb.com/docs/clients/connector-cpp/,并使用以下cmakelists.txt配置

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)

我的代码是:

#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;
}

我得到这个错误-

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

:>

mo49yndu

mo49yndu1#

连接器MySQL和MariaDB(其中share the same heritage)都是旨在仅在Windows上与Visual Studio一起编译和使用。你会在StackOverflow上找到很多关于它的问题。它们的问题在于它们定义了很多已经在标准库中定义的结构,然后也链接到标准库。
我建议你要么切换到Visual Studio或Linux系统。如果你必须在Windows下使用GCC,然后寻找另一个连接器。这些问题不会轻易解决。如果是这样,解决方案不太可能是可移植的,并且可能不适用于两个连接器的未来版本。您可以查看替代品SQLiteSQLAPI++

    • 第一个问题:固定宽度整数**

您提到的第一个问题实际上与头文件中定义的fixed-width integer types和32位操作系统有关。有传统的整数类型,如charshortintlonglong long,但另外还有前面提到的固定宽度整数。
MySql Connector在config.h中定义了int32_t数据类型,标准C++库也定义了它们:MySql使用编译器数据类型__int32定义int32_t

typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;

令人惊讶的是long int数据类型

typedef long int int32_t;
typedef long unsigned int uint32_t;

而标准库将它们定义为常规int

typedef int int32_t;
typedef unsigned int uint32_t;
    • long整数数据类型****保证至少为32位**:在32位架构中,long int是32位(就像int一样),而对于64位,它们有不同的长度-long int是64位,而int只有32位(参见here)。这意味着实际上对于32位系统,这些定义应该是相同的,但编译器认为它们是冲突的。

MySql头由各种定义 Package (我在它们旁边放了一个解释,这样您就可以理解为什么下面给出的建议的解决方案实际上有效),这些定义决定是否应该定义相应的数据类型

// 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
    • 解决方案**

基于上面给出的头文件的结构,有几个解决方案。有些人可能更可行,而另一些人则更不可行。

  • 很明显,你可以不包括cstdintstdint.h中的任何类型定义,而使用MySql定义。这实际上是相当有限的,因为迟早有一天,另一个标准库头可能会包含它,这可能会导致你根本不使用标准库,这可能是非常有限的。
  • 您可以完全放弃正在使用的32位构建工具链,切换到64位编译器,然后编译为64位**。在这种情况下,不应该发生这种情况,因为MySql中的头config.h仅包含在32位系统中,如上所述!如果没有很好的理由,你的项目应该是32位,这是我实际上会做的。关于你的编译器:你似乎正在使用GCC 6.3.0,它是在2016年发布的,实际上是does not fully support the C++17 language standard,你告诉它在CMake文件中使用CMAKE_CXX_STANDARD 17编译。如果你想广泛使用C17的特性,你可能想使用另一个更新的编译器。C14也不算太坏。
  • 您可以使用Visual Studio 2010(版本1600或更高版本进行编译,因为在这种情况下,头将自动包含来自标准的定义,而不是定义自己的定义。
  • 您可以在代码顶部(或在项目中使用的IDE内部)定义预处理器标志#define CPPCONN_DONT_TYPEDEF_MS_TYPES_TO_C99_TYPES,就好像设置了此标志,config.h文件不会定义任何数据类型一样。
  • 同样,您也可以通过打开MYSQLC~1.0/include/jdbc/cppconn/config.h修改预处理器指令
#define HAVE_MS_INT32  1
#define HAVE_MS_UINT32 1

#define HAVE_MS_INT32  0
#define HAVE_MS_UINT32 0

这将取消激活您将来编写的包含此头文件的所有程序的相应定义。

    • 第二个问题:链接到用Visual Studio编译的库**

您得到的第二条错误消息实际上与链接库有关。在Windows上,使用不同编译器编译的库通常不兼容。这意味着使用GCC编译的程序不能包含使用Visual Studio编译的库。* * 在您的示例中,DLL是使用Visual Studio编译的,因此链接到GCC程序失败。**
正如前面提到的here,你可以强制CMake使用MinGW而不是Visual Studio与cmake -G "MinGW Makefiles",但我已经尝试过了,它既不适用于MariaDB也不适用于MySQL。
在MySQL中使用MSYS2时,我在MariaDB上遵循offical guide然后使用

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

我必须进行一些手动修改,例如修改/src/CArrayImp.h,并将第59行改为第63行

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

#define ZEROI64 0LL

因为0I64仅由Visual Studio定义。此外,我们必须删除CArray.cpp中的模板示例化,但我仍然得到了The system cannot find the path specified.错误消息。同样,我也无法在Cygwin中编译它。

    • SQL C++连接器替代方案**

我没有解决最后一个问题的办法,但你可能想看看替代方案。你可以从源代码下载SQLite并编译它。根据installation guide from source,它与MinGW兼容,但它只是lightweight。所以应该是Shareware SQLAPI++。根据their "Order" page,Windows试用版功能齐全
您只能使用试用版进行评估。评估版本与注册版本没有限制或功能差异。但是,评估版在应用程序每次执行第一次数据库连接时都会显示一条注册消息(Windows版)。
注册后,您将:

  • 收到库的非试用版,包括其完整源代码
  • 继续获得免费的终身电子邮件支持
  • 获得一年的免费bug修复和新版本升级

两者都支持MySql:例如参见here

tl;dr:仅在Visual Studio中的Windows上使用MySQL和MariaDB连接器。如果您不能使用Visual Studio,请查看替代C++ SQL连接器,例如SQLiteSQLAPI++**。

kmpatx3s

kmpatx3s2#

这就是我如何在MSYS 2 shell(https://www.msys2.org/)下使用MinGW-w 64构建MariaDB客户端库(我使用http://winlibs.com/编译器):

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

当然,在构建之前,您需要已经安装了先决条件(zlib和gnutls)。
如果你不想使用忍者因为某种原因,你可以用-G"MSYS Makefiles"替换-GNinja,用make -Cbuild_win install-strip替换ninja -Cbuild_win install/strip
然后,为了构建依赖于MySQL客户端的库,我将-I$INSTALLPREFIX/include/mariadb添加到编译器标志中。

gojuced7

gojuced73#

这不是你的问题的答案,但太长的评论...
MariaDB Connector/C的维护者(也是MySQL Connector/C的前维护者)的几句话:
MinGW从来都不是一个受支持的平台/环境,但是我们尽力支持它(就像许多其他不受官方支持的平台一样)。主要的问题是(或仍然是),MinGW总是落后于最新/稳定的Windows版本。我们尝试添加一个自定义的包含文件,其中可以添加一些内容,例如:缺少定义,但在我们决定使用Schannel支持OpenSSL用于Windows平台上的安全(tls)连接后,它不再起作用。
如果有人有一个好的解决方案,例如:通过添加可选的包含文件我打开合并您的拉取请求。使用MinGW条件定义修改C代码不是一个选项。
您的问题被标记为Connector/C++ -但是MariaDB和MySQL的C++连接器都使用Connector/C。
关于先前的回答:使用GnuTLS进行构建是可行的,但是也有一些限制。例如,除非使用安全(tls)连接,否则像caching_sha2_password这样的MySQL身份验证插件将无法工作。

cygmwpex

cygmwpex4#

有一个名为SOCI - The C++ Database Access Library的库,它有许多后端支持,例如:SQLite3、MySQL和其他。如果您在Windows + MSYS2下使用此库,您可以通过命令pacman -S mingw-w64-x86_64-soci轻松地安装它(参见此处:Package: mingw-w64-x86_64-soci - MSYS2 Packages)。
这是一个示例代码来演示它的用法:

#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;
}

希望能帮助到你和其他人。

相关问题