简体   繁体   English

C++ MySQL Linker 错误

[英]C++ MySQL Linker Error

I have been trying to fetch some values from a database, so I downloaded & installed this.我一直在尝试从数据库中获取一些值,所以我下载并安装了它。 I included the needed headers and faced this linker errors.我包含了所需的标题并遇到了这个 linker 错误。 (I'm also using boost.) (我也在使用boost。)

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 " (__imp_?getSQLState@SQLException@sql@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function __catch$_main$0
error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QBEHXZ) referenced in function __catch$_main$0
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QAE@XZ) referenced in function _main
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QAE@QBD@Z) referenced in function _main
error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main
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 " (__imp_?asStdString@SQLString@sql@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@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 &)" (??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@ABVSQLString@sql@@@Z)
fatal error LNK1120: 6 unresolved externals

I also got a couple of warnings like this:我也收到了一些这样的警告:

warning C4251: 'sql::mysql::MySQL_Connection::proxy': class 'boost::shared_ptr<T>' needs to have dll-interface to be used by clients of class 'sql::mysql::MySQL_Connection'

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include "mysql_connection.h"

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

using namespace std;

int main(void)
{
    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("trinity");

        stmt = con->createStatement();
        res = stmt->executeQuery("SELECT * FROM test WHERE test='Tester'"); // replace with your statement
        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;
}

Does anyone know what may happened there?有谁知道那里可能发生了什么?

Have you also add correct library?您是否还添加了正确的库? It seems (you write, you have added headers), that you are running Visual Studio, so either do #pragma comment(lib, <mysql lib>) or add that lib in projects settings under Linker看起来(你写,你已经添加了标题),你正在运行 Visual Studio,所以要么做#pragma comment(lib, <mysql lib>)要么在 Linker 下的项目设置中添加该 lib

#pragma comment(lib, "libmysql.lib") #pragma 注释(lib,“libmysql.lib”)

Properties->Linker->Input->Additional-> mysqlcppconn.lib属性->链接器->输入->附加-> mysqlcppconn.lib

And your project folder (Project folder name ->Debug or Release) paste the mysqlcppconn.dll并且您的项目文件夹(项目文件夹名称 -> 调试或发布)粘贴 mysqlcppconn.dll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM