C++与MySQL的冲突
时间:2010-09-01 来源:Aquester
当在C++代码中,直接引用MySQL头文件时,可能会遇到如下错误:
In file included from /usr/include/c++/4.1.0/bits/char_traits.h:46,
from /usr/include/c++/4.1.0/string:46,
/usr/include/c++/4.1.0/bits/stl_algobase.h:92:28: error: macro "swap" requires 3 arguments, but only 2 given
/usr/include/c++/4.1.0/bits/stl_algobase.h:127:26: error: macro "swap" requires 3 arguments, but only 2 given
/usr/include/c++/4.1.0/bits/vector.tcc:176:20: error: macro "swap" requires 3 arguments, but only 1 given
/usr/include/c++/4.1.0/cctype:70: error: '::isalnum' has not been declared
/usr/include/c++/4.1.0/cctype:71: error: '::isalpha' has not been declared
/usr/include/c++/4.1.0/cctype:72: error: '::iscntrl' has not been declared
/usr/include/c++/4.1.0/cctype:73: error: '::isdigit' has not been declared
/usr/include/c++/4.1.0/cctype:74: error: '::isgraph' has not been declared
/usr/include/c++/4.1.0/cctype:75: error: '::islower' has not been declared
/usr/include/c++/4.1.0/cctype:76: error: '::isprint' has not been declared
/usr/include/c++/4.1.0/cctype:77: error: '::ispunct' has not been declared
/usr/include/c++/4.1.0/cctype:78: error: '::isspace' has not been declared
/usr/include/c++/4.1.0/cctype:79: error: '::isupper' has not been declared
/usr/include/c++/4.1.0/cctype:80: error: '::isxdigit' has not been declared
/usr/include/c++/4.1.0/cctype:81: error: '::tolower' has not been declared
/usr/include/c++/4.1.0/cctype:82: error: '::toupper' has not been declared
解决办法:
尽量对MySQL进行二次包装,让调用者看不到MySQL头文件,如在CPP中包含:
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
在头文件中只进行引用声明:
struct st_mysql;
struct st_mysql_res;
typedef long num_t;
typedef char ** MYSQL_ROW; /** return data as array of strings */
不要在头文件直接include到MySQL的头文件,而且保证只在一个CPP文件中有对MySQL文件的include,否则你可能遇到很多莫名其妙的编译错误,如果不想到这一点,即使花一天时间也未必能找到错误原因。
http://bbs.hadoopor.com/thread-1564-1-1.html
In file included from /usr/include/c++/4.1.0/bits/char_traits.h:46,
from /usr/include/c++/4.1.0/string:46,
/usr/include/c++/4.1.0/bits/stl_algobase.h:92:28: error: macro "swap" requires 3 arguments, but only 2 given
/usr/include/c++/4.1.0/bits/stl_algobase.h:127:26: error: macro "swap" requires 3 arguments, but only 2 given
/usr/include/c++/4.1.0/bits/vector.tcc:176:20: error: macro "swap" requires 3 arguments, but only 1 given
/usr/include/c++/4.1.0/cctype:70: error: '::isalnum' has not been declared
/usr/include/c++/4.1.0/cctype:71: error: '::isalpha' has not been declared
/usr/include/c++/4.1.0/cctype:72: error: '::iscntrl' has not been declared
/usr/include/c++/4.1.0/cctype:73: error: '::isdigit' has not been declared
/usr/include/c++/4.1.0/cctype:74: error: '::isgraph' has not been declared
/usr/include/c++/4.1.0/cctype:75: error: '::islower' has not been declared
/usr/include/c++/4.1.0/cctype:76: error: '::isprint' has not been declared
/usr/include/c++/4.1.0/cctype:77: error: '::ispunct' has not been declared
/usr/include/c++/4.1.0/cctype:78: error: '::isspace' has not been declared
/usr/include/c++/4.1.0/cctype:79: error: '::isupper' has not been declared
/usr/include/c++/4.1.0/cctype:80: error: '::isxdigit' has not been declared
/usr/include/c++/4.1.0/cctype:81: error: '::tolower' has not been declared
/usr/include/c++/4.1.0/cctype:82: error: '::toupper' has not been declared
解决办法:
尽量对MySQL进行二次包装,让调用者看不到MySQL头文件,如在CPP中包含:
#include <my_global.h>
#include <my_sys.h>
#include <mysql.h>
在头文件中只进行引用声明:
struct st_mysql;
struct st_mysql_res;
typedef long num_t;
typedef char ** MYSQL_ROW; /** return data as array of strings */
不要在头文件直接include到MySQL的头文件,而且保证只在一个CPP文件中有对MySQL文件的include,否则你可能遇到很多莫名其妙的编译错误,如果不想到这一点,即使花一天时间也未必能找到错误原因。
补充: MySQL4.x和MySQL5.x头文件不兼容的,最好使用5.x版本 补充: 有些版本只能引用#include <mysql.h>,包含其它会报C++标准库中某文件错误。 原帖发在我的论坛: |
相关阅读 更多 +