程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++代碼中用MySQL頭文件報錯解決方法講解

C++代碼中用MySQL頭文件報錯解決方法講解

編輯:C++入門知識

當在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,否則你可能遇到很多莫名其妙的編譯錯誤,如果不想到這一點,即使花一天時間也未必能找到錯誤原因。

補充: MySQL4.x和MySQL5.x頭文件不兼容的,最好使用5.x版本
補充: 有些版本只能引用#include <mysql.h>,包含其它會報C 標准庫中某文件錯誤。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved