MySQL和MariaDB库';s在C++;使用cmake,mingw > StigTig与MySLQ连接器——这里是CMAKISST.TXT文件,我使用它来在Windows上使用CsAuto GUI生成构建系统(因为我实际上必须在Windows中用C++编写控制台应用程序)。< /强> < /P> 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)MySQL和MariaDB库';s在C++;使用cmake,mingw > StigTig与MySLQ连接器——这里是CMAKISST.TXT文件,我使用它来在Windows上使用CsAuto GUI生成构建系统(因为我实际上必须在Windows中用C++编写控制台应用程序)。< /强> < /P> 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),c++,mysql,cmake,mariadb,mingw,C++,Mysql,Cmake,Mariadb,Mingw,下面是我试图运行的代码(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 <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;
}
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++有问题,对吗?或者我应该使用make from mingw安装目录而不是make from其他目录
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```
我的代码是:
#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;
}
:>两个连接器MySQL和MariaDB(它们)都是仅在Windows上与Visual Studio一起编译和使用的。你会发现很多关于StackOverflow的问题。它们的问题是,它们定义了许多已经在标准库中定义的结构,然后链接到标准库 我建议您切换到Visual Studio或Linux系统。如果必须在Windows下使用GCC,请寻找其他连接器。这些问题不容易解决。如果是这样,解决方案不太可能是可移植的,并且可能无法与两个连接器的未来版本一起使用。您可以查看备选方案和
第一个问题:固定宽度整数 您提到的第一个问题实际上与头文件中定义的和32位操作系统有关。有传统的整数类型,如
char
、short
、int
、long
和long
,但还有前面提到的固定宽度整数
MySQL连接器在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位的long int
是32位的(就像int
),而对于64位,它们有不同的长度-long int
是64位的,int
只有32位(请参阅)。这意味着实际上对于32位系统,这些定义应该相同,但编译器认为它们相互冲突
MySql头由各种定义(我在它们旁边放了一个解释,这样您就可以理解为什么下面给出的建议的解决方案实际有效)包装,这些定义决定是否应该定义相应的数据类型
// 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
到
这将为您将来编写的包含此标头的所有程序取消激活相应的定义
第二个问题:链接到使用Visual Studio编译的库 您得到的第二条错误消息实际上与链接库有关。在Windows上,使用不同编译器编译的库通常不兼容。这意味着使用GCC编译的程序不能包含使用Visual Studio编译的库在您的情况下,DLL是用Visual Studio编译的,因此链接到GCC程序失败。
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)
如前所述,您可以通过CMake-G“MinGW Makefiles”
强制CMake使用MinGW而不是visualstudio,但我已经尝试过了,它既不适用于MariaDB,也不适用于MySQL
在MySQL中使用时,我在MariaDB上遇到了一个与OpenSSL相关的神秘错误
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行从
#ifndef _WIN32
# define ZEROI64 0LL
#else
# define ZEROI64 0I64
#endif
到
as0I64
仅由Visual Studio定义。此外,必须删除CArray.cpp
中的模板实例化,但最终还是出现了系统无法找到指定的路径。
错误消息。同样地,我也无法在Cygwin中编译它
<强> SQLC++连接器备选软件<强/> /P> 我没有解决最后一个问题的办法,但你可能想看看其他的办法。您可以从源代码下载并编译它。根据标准,它与MinGW兼容,但仅限于。共享软件也应该如此。据介绍,Windows的试用版功能齐全
试用版只能用于评估目的。评价 版本与注册版本没有限制或功能差异 版本但是,评估版本会显示一条注册消息 每次应用程序执行其第一个数据库连接时 (Windows版本) 注册后,您将:- 接收库的非试用版本,包括其完整源代码
- 继续获得终身免费电子邮件支持
- 获得一年的免费bug修复和新版本升级
tl;dr:仅在Visual Studio中的窗口上使用MySQL和MariaDB连接器。如果您不能使用VisualStudio,请查看<强>可选的C++ SQL连接器,例如“和/强”> .< /P> < P>这是我如何用MysWshell(MyS2 shell)(Mys2 shell)(以下是使用编译器)构建MiRADB客户端库的方法:
wgethttps://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-ADB-connector-c-3.1.11-src
#修复cmake/ConnectorName.cmake(版本>=3.1.11)
补丁-ulbf cmake/ConnectorName.cmake你能删除#include
和#include
?好的,但谷歌说:“从GCC 5开始,C++17功能可用。这种模式是
#define HAVE_MS_INT32 1
#define HAVE_MS_UINT32 1
#define HAVE_MS_INT32 0
#define HAVE_MS_UINT32 0
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -G "MinGW Makefiles" -DCONC_WITH_UNIT_TESTS=Off -DCONC_WITH_MSI=OFF -DWITH_SSL=SCHANNEL .
cmake --build . --config RelWithDebInfo
#ifndef _WIN32
# define ZEROI64 0LL
#else
# define ZEROI64 0I64
#endif
#define ZEROI64 0LL