簡體   English   中英

MySQL 和 MariaDB 庫在 C++ 中使用 Z272CEADB8458515B2AE4B5630A6029wCCZ

[英]MySQL and MariaDB library's in C++ using cmake, mingw

STARTIG WITH MYSLQ CONNECTOR - 這是我用來在 windows 上使用 CMAKE GUI 生成構建系統的 cmakelists.txt 文件(因為我實際上必須在 Z6CE8094900BA1225B4 窗口中創建控制台應用程序)。

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 不適用於預編譯的連接器,但只能使用相同的編譯器編譯,所以:

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

然后我想也許它根本不適用於 MINGW“編譯器”所以我發現這篇文章https://forums.mysql.com/read.ZE1BFD762321E409CEE3,4AC0B6E8262961,4AC0B6E826196

我在https://dev.mysql.com/doc/connector-cpp/8.0/en/connector-cpp-installation-source-distribution.ZFC35FDC70D5FC69D269883A82上下載了它的源代碼

並使用 cmake 進行編譯我得到了這個錯誤,沒有得到使用“make”命令所需的 makefile -

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

** 有點晚了 ****

OKAAAAAAY 所以我正在嘗試使用 MINGW 和以下 cmakelists.txt 配置編譯 MARIADB https://mariadb.com/docs/clients/connector-cpp/

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

:>

兩個連接器 MySQL 和 MariaDB(它們具有相同的傳統旨在僅在 Windows 上與 Visual Studio 一起編譯和使用 你會在 StackOverflow 上找到很多關於它的先前問題。 它們的問題是它們定義了許多已經在標准庫中定義的結構,然后也鏈接到標准庫。

我建議您切換到 Visual Studio 或 Linux 系統 如果您必須在 Windows 下使用 GCC,請尋找另一個連接器。 這些問題不會輕易解決。 如果是這樣,解決方案不太可能是可移植的,並且可能不適用於兩個連接器的未來版本。 您可以查看替代方案SQLiteSQLAPI++


第一個問題:固定寬度的整數

您提到的第一個問題實際上與頭文件中定義的固定寬度 integer 類型和 32 位操作系統有關。 有傳統的 integer 類型,如charshortintlonglong long ,還有前面提到的固定寬度整數。

MySql 連接器在config.h定義了int32_t數據類型,標准 C++ 庫也定義了它們: __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 integer 數據類型保證至少為 32 位:在 32 位架構上, long int是 32 位(就像int一樣),而對於 64 位,它們具有不同的長度 - long int是 64 位並且int只有 32 位(請參見此處)。 這意味着實際上對於 32 位系統,這些定義應該是相同的,但編譯器認為它們是沖突的。

MySql header 由各種定義包裝(我在它們旁邊做了解釋,以便您了解為什么下面給出的建議解決方案實際上有效)決定是否應該定義相應的數據類型

// 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

解決方案

基於上面給出的 header 文件的結構,有幾個解決方案。 有些可能更可行,而另一些則不那么可行。

  • 顯然,您不能cstdintstdint.h中包含任何類型定義,並與 MySql 定義一起使用。 這實際上是非常有限的,因為遲早可能另一個標准庫 header 將包含它,它可能會導致您根本不使用標准庫,這可能是非常有限的。

  • 您可以完全放棄正在使用的 32 位構建工具鏈,切換到 **64 位編譯器並編譯為 64 位 在這種情況下,這不應該發生,因為 MySql 中的 header config.h僅包含在 32 位系統中,如上所述。 如果沒有充分的理由認為你的項目應該是 32 位的,那我實際上會這樣做:談論你的編譯器。 您似乎正在使用 2016 年發布的 GCC 6.3.0,實際上並不完全C++17您告訴它在 CMake 文件中使用CMAKE_CXX_STANDARD 17編譯的 C++17 語言標准。 如果您想廣泛使用 C++17 功能,您可能需要使用另一個更新的編譯器。 否則 C++14 也不錯。

  • 您可以使用Visual Studio 2010 (版本1600或更高版本進行編譯,因為在這種情況下 header 將自動包含標准中的定義,而不是定義自己的定義。

  • 您可以在代碼頂部(或在您用於項目的 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

    這將停用您將來編寫的所有程序的相應定義,包括此 header。


第二個問題:鏈接到用 Visual Studio 編譯的庫

您收到的第二條錯誤消息實際上與鏈接庫有關。 在 Windows 上,使用不同編譯器編譯的庫通常不兼容。 這意味着使用 GCC 編譯的程序不能包含使用 Visual Studio 編譯的庫。 在您的情況下,DLL 是使用 Visual Studio 編譯的,因此鏈接到您的 GCC 程序失敗。

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.

在 MySQL 中使用MSYS2我得到一個與 OpenSSL 相關的神秘錯誤,而在 MariaDB 上遵循官方指南然后使用

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並編譯它。 根據源代碼的安裝指南,它與 MinGW 兼容,但它只是輕量級的。 共享軟件SQLAPI++也應該如此。 根據他們的“訂單”頁面,Windows 的試用版功能齊全

您只能將試用版用於評估目的。 評估版與注冊版沒有限制或功能差異。 但是,每次應用程序執行其第一次數據庫連接(Windows 版本)時,評估版都會顯示注冊消息。

注冊后,您將:

  • 接收庫的非試用版,包括其完整源代碼
  • 繼續獲得終身免費 email 支持
  • 獲得一年的免費錯誤修復和新版本升級

兩者都應該支持 MySql:例如看這里


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

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

當然,在構建之前,您需要已經安裝了先決條件(zlib 和 gnutls)。

如果您出於某種原因不想使用 Ninja,可以將-GNinja替換為-G"MSYS Makefiles"並將ninja -Cbuild_win install/strip替換為make -Cbuild_win install-strip

然后構建依賴於 MySQL 客戶端的庫,我將-I$INSTALLPREFIX/include/mariadb到編譯器標志。

這不是您的問題的答案,但評論太長了......

MariaDB Connector/C 的維護者(以及 MySQL Connector/C 的前維護者)的幾句話:

MinGW 從來都不是受支持的平台/環境,但是我們盡最大努力支持它(就像許多其他非官方不受支持的平台一樣)。 主要問題是(或仍然是),MinGW 總是落后於最新/穩定的 Windows 版本。 我們嘗試添加一個自定義包含文件,其中可以添加一些內容,例如缺少定義,但是在我們決定使用 Schannel 支持 OpenSSL 在 Windows 平台上進行安全 (tls) 連接后,它不再起作用。

如果有人有一個好的解決方案,例如通過添加可選的包含文件,我願意合並您的拉取請求。 使用 MinGW 條件定義修改 C 代碼不是一個選項。

您的問題被標記為連接器/C++ - 但是來自 MariaDB 和 MySQL 的 C++ 連接器都使用連接器/C。

關於之前的答案:使用 GnuTLS 構建是可行的,但是有一些限制。 例如,MySQL 身份驗證插件(如caching_sha2_password)將不起作用,除非您使用安全(tls)連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM