我开发了一个应用程序(在Visual Studio 2008中),它与MySQL数据库进行通信并使用了很多wxWidgets。 问题是当我添加包含文件时,MySQL包含文件存在,我会收到大量错误。
这是主文件,其中包含Database.h。
#include <boost/shared_ptr.hpp>
#include <wx/app.h>
#include <ConcreteBatchDialogView.h>
#include <ConcreteLoginDialogView.h>
#include <ConcreteInitDialogView.h>
#include <ConcreteKeyDataView.h>
#include <ConcreteProductDataView.h>
#include <ConcreteView.h>
#include <Controller.h>
#include <DummyModel.h>
#include <Revision.h>
#include <Database.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
...
return 0;
}
Database.h具有以下代码:
#include <wx/string.h>
#include "DbTable.h"
class DbTable;
class Database
{
public:
Database();
~Database();
....
}
最后DbTable包含了MySQL:
#include <my_global.h>
#include <mysql.h>
class DbTable
{
public:
// CREATORS
~DbTable();
MYSQL_ROW currentRow;
MYSQL_ROW *row;
}
因此,应用程序编译正常,直到我包含Database.h。
请检查日志:
Compiling...
main.cpp
f:\build\mysql\5_6_12\x32\include\my_config.h(263) : warning C4005: 'HAVE_STRNLEN' : macro redefinition
f:\build\wxwidgets\2_9_1\include\wx\wxcrtbase.h(252) : see previous definition of 'HAVE_STRNLEN'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(91) : warning C4005: 'AF_IPX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(460) : see previous definition of 'AF_IPX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(127) : warning C4005: 'AF_MAX' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(479) : see previous definition of 'AF_MAX'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(163) : warning C4005: 'SO_DONTLINGER' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(402) : see previous definition of 'SO_DONTLINGER'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(206) : error C2011: 'sockaddr' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(485) : see declaration of 'sockaddr'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing '}' before 'constant'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2143: syntax error : missing ';' before 'constant'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(384) : error C2059: syntax error : 'constant'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C2143: syntax error : missing ';' before '}'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(437) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(518) : warning C4005: 'IN_CLASSA' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(287) : see previous definition of 'IN_CLASSA'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(524) : warning C4005: 'IN_CLASSB' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(293) : see previous definition of 'IN_CLASSB'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(530) : warning C4005: 'IN_CLASSC' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(299) : see previous definition of 'IN_CLASSC'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(541) : warning C4005: 'INADDR_ANY' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(304) : see previous definition of 'INADDR_ANY'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(543) : warning C4005: 'INADDR_BROADCAST' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(306) : see previous definition of 'INADDR_BROADCAST'
c:\program files\microsoft sdks\windows\v6.0a\include\ws2def.h(577) : error C2011: 'sockaddr_in' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(312) : see declaration of 'sockaddr_in'
c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(132) : error C2011: 'fd_set' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(68) : see declaration of 'fd_set'
c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(167) : warning C4005: 'FD_SET' : macro redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(102) : see previous definition of 'FD_SET'
c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(176) : error C2011: 'timeval' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(111) : see declaration of 'timeval'
c:\program files\microsoft sdks\windows\v6.0a\include\winsock2.h(232) : error C2011: 'hostent' : 'struct' type redefinition
c:\program files\microsoft sdks\windows\v6.0a\include\winsock.h(167) : see declaration of 'hostent'
我认为这已经足够了,但还有更多。
你有什么建议怎么办?
我检查过它可能是wxWidgets的问题 - MySQL编译。我试图将它们分开一些,但到目前为止没有帮助。
答案 0 :(得分:2)
答案 1 :(得分:0)
问题可能来自MySQL头文件,我也包含在一些头文件中。也许wxWidgets和MySQL彼此不喜欢。
实际上我使用的主要是MYSQL_ROW,实际上是
typedef char **MYSQL_ROW.
所以,我决定在所有标题和地点中将MYSQL_ROW替换为char **,我在其中使用它并添加标题
#include <my_global.h>
#include <mysql.h>
仅限于database.cpp文件。
有点讨厌的黑客,但有助于解决问题。
答案 2 :(得分:0)
你似乎有两个问题:
HAVE_STRNLEN
重新定义:wxWidgets在定义之前检查它是否尚未定义,因此如果首先包含MySQL标头,则应该解决该问题。wxUSE_IPV6
中启用wx/setup.h
来解决,因为这也会转换为使用WinSock2作为副作用。