MariaDB C++ 连接器编译
MariaDB C++ Connector compile
我正在尝试编译一个假设使用 mariadbc++ 连接器的简单 c++ 文件。出于某种原因,我 运行 遇到了很多麻烦。我收到 st运行ge 错误消息。我既不理解它们,也不知道如何修复它们。
我做了很多研究,但要么没有其他人 运行 研究这些问题,要么我就是找不到它们。
我安装了以下软件包:
mariadb-client
mariadb-client-5.5
mariadb-server
libmariadbclient18
libmysqlclient18
libmysqlcppconn7
libmysqlcppconn-dev
libmariadbclient-dev
libmariadb-dev
我使用的是 x64 Ubuntu 14.04 系统(好吧 Pinguy OS,但我想这并不重要)
我尝试构建以下程序:
#include <my_global.h>
#include <mysql.h>
#include "mysql_driver.h"
int main(int argc, char **argv)
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306","root", "herein");
if (!con->isValid()) exit(1);
stmt = con->createStatement();
stmt->execute("use testtb");
stmt->execute("INSERT INTO Testtable(id, label) VALUES (1, 1)");
delete stmt;
delete con;
return 1;
}
我试过这个命令:
g++ -lmysqlcppconn -g src/test_mariadb.cpp -o bin/test_mariadb $(mysql_config --cflags) $(mysql_config --libs)
我的源文件在 src/ 下,在 bin/ 我的二进制文件中。
最后我得到了编译错误:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /usr/include/boost/assert.hpp:82,
from /usr/include/boost/smart_ptr/scoped_ptr.hpp:15,
from /usr/include/boost/scoped_ptr.hpp:14,
from /usr/include/mysql_driver.h:30,
from src/test_mariadb.cpp:3:
/usr/include/c++/4.8/bits/stl_algobase.h:239:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:56: error: macro "max" passed 3 arguments, but takes just 2
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
In file included from /usr/include/boost/mpl/aux_/begin_end_impl.hpp:21:0,
from /usr/include/boost/mpl/begin_end.hpp:18,
from /usr/include/boost/mpl/iter_fold.hpp:18,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_begin.hpp:20:57: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_begin, begin, true)
^
In file included from /usr/include/boost/mpl/sequence_tag.hpp:18:0,
from /usr/include/boost/mpl/begin_end.hpp:19,
from /usr/include/boost/mpl/iter_fold.hpp:18,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_tag.hpp:20:54: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_tag, tag, false)
^
In file included from /usr/include/boost/mpl/aux_/O1_size_impl.hpp:20:0,
from /usr/include/boost/mpl/O1_size.hpp:19,
from /usr/include/boost/mpl/iter_fold.hpp:19,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_size.hpp:20:1: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_DEF(size)
^
In file included from /usr/include/boost/mpl/apply_wrap.hpp:23:0,
from /usr/include/boost/mpl/bind.hpp:27,
from /usr/include/boost/mpl/lambda.hpp:18,
from /usr/include/boost/mpl/iter_fold.hpp:20,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_apply.hpp:22:58: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_apply, apply, false)
^
In file included from /usr/include/boost/mpl/quote.hpp:23:0,
from /usr/include/boost/mpl/aux_/full_lambda.hpp:25,
from /usr/include/boost/mpl/lambda.hpp:22,
from /usr/include/boost/mpl/iter_fold.hpp:20,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_type.hpp:20:55: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_type, type, true)
^
In file included from src/test_mariadb.cpp:1:0:
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected unqualified-id before ‘const’
min(const _Tp& __a, const _Tp& __b)
^
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected initializer before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected unqualified-id before ‘const’
max(const _Tp& __a, const _Tp& __b)
^
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected initializer before ‘const’
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /usr/include/boost/assert.hpp:82,
from /usr/include/boost/smart_ptr/scoped_ptr.hpp:15,
from /usr/include/boost/scoped_ptr.hpp:14,
from /usr/include/mysql_driver.h:30,
from src/test_mariadb.cpp:3:
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: error: ‘std::min’ declared as an ‘inline’ variable
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.8/bits/stl_algobase.h:242:7: error: expected primary-expression before ‘if’
if (__comp(__b, __a))
^
/usr/include/c++/4.8/bits/stl_algobase.h:242:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.8/bits/stl_algobase.h:244:7: error: expected unqualified-id before ‘return’
return __a;
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:5: error: ‘max’ declared as an ‘inline’ variable
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:5: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.8/bits/stl_algobase.h:263:7: error: expected primary-expression before ‘if’
if (__comp(__a, __b))
^
/usr/include/c++/4.8/bits/stl_algobase.h:263:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.8/bits/stl_algobase.h:265:7: error: expected unqualified-id before ‘return’
return __a;
^
/usr/include/c++/4.8/bits/stl_algobase.h:266:5: error: expected declaration before ‘}’ token
}
您包含的 header 定义了名为 min
、max
和 test
的预处理器宏。这些与您包含的其他 header 文件中使用的标识符(例如方法名称)发生冲突。
假设这些宏已在这些 header 之一中定义:
#include <my_global.h>
#include <mysql.h>
您可以在包含 mysql_driver.h
之前添加 #undef
,例如:
#include <my_global.h>
#include <mysql.h>
#undef min
#undef max
#undef test
#include "mysql_driver.h"
您还可以考虑 re-arranging 包含文件的顺序,或者查看 header 定义这些文件的内容,看看是否可以禁用这些宏的定义。
我正在尝试编译一个假设使用 mariadbc++ 连接器的简单 c++ 文件。出于某种原因,我 运行 遇到了很多麻烦。我收到 st运行ge 错误消息。我既不理解它们,也不知道如何修复它们。
我做了很多研究,但要么没有其他人 运行 研究这些问题,要么我就是找不到它们。
我安装了以下软件包:
mariadb-client
mariadb-client-5.5
mariadb-server
libmariadbclient18
libmysqlclient18
libmysqlcppconn7
libmysqlcppconn-dev
libmariadbclient-dev
libmariadb-dev
我使用的是 x64 Ubuntu 14.04 系统(好吧 Pinguy OS,但我想这并不重要)
我尝试构建以下程序:
#include <my_global.h>
#include <mysql.h>
#include "mysql_driver.h"
int main(int argc, char **argv)
{
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
con = driver->connect("tcp://localhost:3306","root", "herein");
if (!con->isValid()) exit(1);
stmt = con->createStatement();
stmt->execute("use testtb");
stmt->execute("INSERT INTO Testtable(id, label) VALUES (1, 1)");
delete stmt;
delete con;
return 1;
}
我试过这个命令:
g++ -lmysqlcppconn -g src/test_mariadb.cpp -o bin/test_mariadb $(mysql_config --cflags) $(mysql_config --libs)
我的源文件在 src/ 下,在 bin/ 我的二进制文件中。
最后我得到了编译错误:
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /usr/include/boost/assert.hpp:82,
from /usr/include/boost/smart_ptr/scoped_ptr.hpp:15,
from /usr/include/boost/scoped_ptr.hpp:14,
from /usr/include/mysql_driver.h:30,
from src/test_mariadb.cpp:3:
/usr/include/c++/4.8/bits/stl_algobase.h:239:56: error: macro "min" passed 3 arguments, but takes just 2
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:56: error: macro "max" passed 3 arguments, but takes just 2
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
In file included from /usr/include/boost/mpl/aux_/begin_end_impl.hpp:21:0,
from /usr/include/boost/mpl/begin_end.hpp:18,
from /usr/include/boost/mpl/iter_fold.hpp:18,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_begin.hpp:20:57: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_begin, begin, true)
^
In file included from /usr/include/boost/mpl/sequence_tag.hpp:18:0,
from /usr/include/boost/mpl/begin_end.hpp:19,
from /usr/include/boost/mpl/iter_fold.hpp:18,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_tag.hpp:20:54: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_tag, tag, false)
^
In file included from /usr/include/boost/mpl/aux_/O1_size_impl.hpp:20:0,
from /usr/include/boost/mpl/O1_size.hpp:19,
from /usr/include/boost/mpl/iter_fold.hpp:19,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_size.hpp:20:1: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_DEF(size)
^
In file included from /usr/include/boost/mpl/apply_wrap.hpp:23:0,
from /usr/include/boost/mpl/bind.hpp:27,
from /usr/include/boost/mpl/lambda.hpp:18,
from /usr/include/boost/mpl/iter_fold.hpp:20,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_apply.hpp:22:58: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_apply, apply, false)
^
In file included from /usr/include/boost/mpl/quote.hpp:23:0,
from /usr/include/boost/mpl/aux_/full_lambda.hpp:25,
from /usr/include/boost/mpl/lambda.hpp:22,
from /usr/include/boost/mpl/iter_fold.hpp:20,
from /usr/include/boost/variant/detail/initializer.hpp:28,
from /usr/include/boost/variant/variant.hpp:31,
from /usr/include/boost/variant.hpp:17,
from /usr/include/cppconn/connection.h:31,
from /usr/include/cppconn/driver.h:30,
from /usr/include/mysql_driver.h:32,
from src/test_mariadb.cpp:3:
/usr/include/boost/mpl/aux_/has_type.hpp:20:55: error: macro "test" passed 2 arguments, but takes just 1
BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_type, type, true)
^
In file included from src/test_mariadb.cpp:1:0:
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected unqualified-id before ‘const’
min(const _Tp& __a, const _Tp& __b)
^
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:193:5: error: expected initializer before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected unqualified-id before ‘const’
max(const _Tp& __a, const _Tp& __b)
^
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected ‘)’ before ‘const’
/usr/include/c++/4.8/bits/stl_algobase.h:216:5: error: expected initializer before ‘const’
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from /usr/include/boost/assert.hpp:82,
from /usr/include/boost/smart_ptr/scoped_ptr.hpp:15,
from /usr/include/boost/scoped_ptr.hpp:14,
from /usr/include/mysql_driver.h:30,
from src/test_mariadb.cpp:3:
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: error: ‘std::min’ declared as an ‘inline’ variable
min(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:239:5: error: template declaration of ‘const _Tp& std::min’
/usr/include/c++/4.8/bits/stl_algobase.h:242:7: error: expected primary-expression before ‘if’
if (__comp(__b, __a))
^
/usr/include/c++/4.8/bits/stl_algobase.h:242:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.8/bits/stl_algobase.h:244:7: error: expected unqualified-id before ‘return’
return __a;
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:5: error: ‘max’ declared as an ‘inline’ variable
max(const _Tp& __a, const _Tp& __b, _Compare __comp)
^
/usr/include/c++/4.8/bits/stl_algobase.h:260:5: error: template declaration of ‘const _Tp& max’
/usr/include/c++/4.8/bits/stl_algobase.h:263:7: error: expected primary-expression before ‘if’
if (__comp(__a, __b))
^
/usr/include/c++/4.8/bits/stl_algobase.h:263:7: error: expected ‘}’ before ‘if’
/usr/include/c++/4.8/bits/stl_algobase.h:265:7: error: expected unqualified-id before ‘return’
return __a;
^
/usr/include/c++/4.8/bits/stl_algobase.h:266:5: error: expected declaration before ‘}’ token
}
您包含的 header 定义了名为 min
、max
和 test
的预处理器宏。这些与您包含的其他 header 文件中使用的标识符(例如方法名称)发生冲突。
假设这些宏已在这些 header 之一中定义:
#include <my_global.h>
#include <mysql.h>
您可以在包含 mysql_driver.h
之前添加 #undef
,例如:
#include <my_global.h>
#include <mysql.h>
#undef min
#undef max
#undef test
#include "mysql_driver.h"
您还可以考虑 re-arranging 包含文件的顺序,或者查看 header 定义这些文件的内容,看看是否可以禁用这些宏的定义。