2013-08-27 73 views
2

我一直在嘗試使用Windows 7(x64)上的Visual Studio 2010工作MySQL示例。我在Linux上沒有問題,但是,我在Windows上遇到了很多錯誤。這是我的代碼(我從一些網站複製到測試,如果它真的將無法正常工作)C++ mySQL連接器LINKER錯誤(Windows)

/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. 

This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; version 2 of the License. 

There are special exceptions to the terms and conditions of the GPL 
as it is applied to this software. View the full text of the 
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this 
software distribution. 

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details. 

You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
*/ 

/* Standard C++ includes */ 
#include <stdlib.h> 
#include <iostream> 

    /* 
Include directly the different 
headers from cppconn/ and mysql_driver.h + mysql_util.h 
    (and mysql_connection.h). This will reduce your build time! 
    */ 
#include <mysql_connection.h> 

#include <driver.h> 
#include <exception.h> 
#include <resultset.h> 
#include <statement.h> 

using namespace std; 

int main(void) 
    { 
    cout << endl; 
    cout << "Running 'SELECT 'Hello World!' » AS _message'..." << endl; 

try { 
sql::Driver *driver; 
sql::Connection *con; 
sql::Statement *stmt; 
sql::ResultSet *res; 

/* Create a connection */ 
driver = get_driver_instance(); 
con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); 
/* Connect to the MySQL test database */ 
con->setSchema("test"); 

stmt = con->createStatement(); 
res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); 
while (res->next()) { 
    cout << "\t... MySQL replies: "; 
    /* Access column data by alias or column name */ 
    cout << res->getString("_message") << endl; 
cout << "\t... MySQL says it again: "; 
/* Access column fata by numeric offset, 1 is the first column */ 
cout << res->getString(1) << endl; 
} 
delete res; 
delete stmt; 
delete con; 

} catch (sql::SQLException &e) { 
cout << "# ERR: SQLException in " << __FILE__; 
// cout << "(" << __FUNCTION__ << ") on line " » << __LINE__ << endl; 
cout << "# ERR: " << e.what(); 
cout << " (MySQL error code: " << e.getErrorCode(); 
cout << ", SQLState: " << e.getSQLState() << ")" << endl; 
} 

cout << endl; 

return EXIT_SUCCESS; 
} 

母雞我試圖鏈接到靜態庫mysqlcppconn-static.lib。這會在下面產生未解決的錯誤。 :

1>xedownload.obj : error LNK2019: unresolved external symbol  
"__declspec(dllimport)  public: class std::basic_string<char,struct  
std::char_traits<char>,class std::allocator<char> > const & __thiscall  
sql::SQLException::getSQLState(void)const "([email protected]@[email protected]@[email protected][email protected]@[email protected]@V?$alloc [email protected]@[email protected]@[email protected]@XZ) referenced in function __catch$_main$0 
1>xedownload.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) 
public: int __thiscall sql::SQLException::getErrorCode(void)const "([email protected]@[email protected]@QBEHXZ) referenced in function __catch$_main$0 
1>xedownload.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) 
public: __thiscall sql::SQLString::~SQLString(void)" ([email protected]@@[email protected])  referenced in function _main 
1>xedownload.obj : error LNK2019: unresolved external symbol "__declspec(dllimport)  public: __thiscall sql::SQLString::SQLString(char const * const)" ([email protected]@@[email protected]@Z) referenced in function _main 
1>xedownload.obj : error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main 
1>xedownload.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class  std::allocator<char> > const & __thiscall sql::SQLString::asStdString(void)const " ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@XZ) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class sql::SQLString const &)" ([email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected]@@@Z) 
1>c:\users\avi\documents\visual studio 2010\Projects\xedownload\Debug\xedownload.exe : fatal error LNK1120: 6 unresolved externals 

回答

0

我認爲你正在運行的Win32,但實際上你的庫是64位的,所以你需要看你的系統安裝了MySQL連接器C++ 1.1.6中的下一個,到底是64位或32位。運行32位程序需要安裝32位庫,這需要64位庫和頭文件才能運行64位程序。 VSxxxx特別關注開發過程