如何修复由于MySql连接器C ++未解决的外部符号? [英] How to fix unresolved external symbol due to MySql Connector C++?

查看:119
本文介绍了如何修复由于MySql连接器C ++未解决的外部符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照本教程 http://blog.ulf-wendel.de/? p = 215#hello 。我试过在Visual C ++ 2008和Visual C ++ 2010。无论是静态还是动态,编译器给了我相同的确切的错误消息:

  error LNK2001:未解析的外部符号_get_driver_instance 



更新:

+其他依赖项:mysqlcppconn.lib

+其他包含目录:C:\Program Files\\ \\ MySQL \MySQL Connector C ++ 1.0.5 \include

+其他库目录:C:\Program Files\MySQL\MySQL Connector C ++ 1.0.5 \lib \opt

另一个更新:
点击get_driver_instance()上的F12链接到:

  class CPPCONN_PUBLIC_FUNC驱动程序
{
protected:
virtual〜Driver(){}
public:
//尝试使数据库连接到给定的URL 。

virtual Connection * connect(const std :: string& hostName,const std :: string& userName,const std :: string& password)= 0;

virtual Connection * connect(std :: map< std :: string,ConnectPropertyVal>& options)= 0;

virtual int getMajorVersion()= 0;

virtual int getMinorVersion()= 0;

virtual int getPatchVersion()= 0;

virtual const std :: string& getName()= 0;
};

} / *命名空间sql * /

externC
{
CPPCONN_PUBLIC_FUNC sql :: Driver * get_driver_instance
}

显然,函数存在,但是链接器找不到它。 p>

代码段:

  #include< iostream& 
#include< sstream>
#include< memory>
#include< string>
#include< stdexcept>

using namespace std;

#includemysql_connection.h
#includemysql_driver.h

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

int main(){

try {
sql :: Driver * driver;
sql :: Connection * conn;
sql :: Statement * stmt;
sql :: ResultSet * res;
driver = get_driver_instance();
conn = driver-> connect(http:// localhost / chandb,root,chan);


stmt = conn-> createStatement();
res = stmt-> executeQuery(select * from another);
while(res-> next()){
cout< id =< res-> getInt(Id);
cout<< id =< res-> getInt(GoldValue);
cout<< id =< res-> getString(Model);
}

delete conn;
delete stmt;
delete res;
std :: cout<< 就是这个;
}
catch(sql :: SQLException e){
cout<< e.what();
}
}

感谢

Chan

解决方案

根据 MySQL 5.1参考手册如果您使用的版本1.1的MySQL连接器C ++:

get_driver_instance()是现在只能在动态库构建 - 静态构建不
有这个符号这是为了适应加载DLL与LoadLibrary或dlopen如果你不使用CMake构建源代码,你需要定义mysqlcppconn_EXPORTS如果你正在动态加载并想要使用get_driver_instance()入口点。

如果我正确理解了前面的注释,你必须使用动态构建并定义 mysqlcppconn_EXPORTS


I followed this tutorial http://blog.ulf-wendel.de/?p=215#hello. I tried both on Visual C++ 2008 and Visual C++ 2010. Either static or dynamic, the compiler gave me the same exact error messages:

error LNK2001: unresolved external symbol _get_driver_instance

Has anyone experience this issue before?

Update:
+ Additional Dependencies: mysqlcppconn.lib
+ Additional Include Directories: C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\include
+ Additional Libraries Directories: C:\Program Files\MySQL\MySQL Connector C++ 1.0.5\lib\opt

Another Update: Click F12 on get_driver_instance() linked to:

class CPPCONN_PUBLIC_FUNC Driver
{
protected:
    virtual ~Driver() {}
public:
    // Attempts to make a database connection to the given URL.

    virtual Connection * connect(const std::string& hostName, const std::string& userName, const std::string& password) = 0;

    virtual Connection * connect(std::map< std::string, ConnectPropertyVal > & options) = 0;

    virtual int getMajorVersion() = 0;

    virtual int getMinorVersion() = 0;

    virtual int getPatchVersion() = 0;

    virtual const std::string & getName() = 0;
};

} /* namespace sql */

extern "C"
{
  CPPCONN_PUBLIC_FUNC sql::Driver *get_driver_instance();
}

Apparently, the function existed, but the linker could not find it.

Code snippet:

#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>

using namespace std;

#include "mysql_connection.h"
#include "mysql_driver.h" 

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

int main() {

    try {
        sql::Driver *driver;
        sql::Connection *conn;
        sql::Statement *stmt;
        sql::ResultSet *res;
        driver = get_driver_instance();
        conn = driver->connect( "http://localhost/chandb", "root", "chan" );


        stmt = conn->createStatement();
        res = stmt->executeQuery( "select * from another" );
        while( res->next() ) {
            cout << "id = " << res->getInt( "Id" );
            cout << "id = " << res->getInt( "GoldValue" );
            cout << "id = " << res->getString( "Model" );
        }

        delete conn;
        delete stmt;
        delete res;
        std::cout << "This is it";
    }
    catch( sql::SQLException e ) {
        cout << e.what();
    }
}

Thanks,
Chan

解决方案

According to MySQL 5.1 Reference Manual if you are using the Version 1.1 of the MySQL Connector C++:
"get_driver_instance() is now only available in dynamic library builds - static builds do not have this symbol. This was done to accommodate loading the DLL with LoadLibrary or dlopen. If you do not use CMake for building the source code you will need to define mysqlcppconn_EXPORTS if you are loading dynamically and want to use the get_driver_instance() entry point."
If I understand the previous note correctly, you have to use the dynamic build and define mysqlcppconn_EXPORTS.

这篇关于如何修复由于MySql连接器C ++未解决的外部符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆