简体   繁体   English

无法在Visual Studio 2015的C ++程序中链接MySQL

[英]Can't Link MySQL in a C++ Program in Visual Studio 2015

I am trying to compile a very simple program to just test my MySQL installation with a simple DB Table. 我正在尝试编译一个非常简单的程序,以使用一个简单的数据库表测试我的MySQL安装。 I am new to MySQL and relatively inexperienced programmer so it may be a very simple set up/linking problem: 我是MySQL的新手,并且是相对没有经验的程序员,因此它可能是一个非常简单的设置/链接问题:

#include <iostream>
#include <memory>
#include <string> 

#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>

using namespace std;
using namespace sql;

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "root"
#define PASSWORD ""
#define DATABASE "mydb"

int main()
{
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;

std::string user(USER);
std::string dbhost(DBHOST);
std::string password(PASSWORD);

driver = get_driver_instance();
//con = driver->connect(dbhost, user, password);
//con->setSchema(DATABASE);

return 0;
}

I have installed the latest MySQL package and have the include and library files at the following locations: C:\\Program Files\\MySQL\\Connector.C++ 1.1\\include C:\\Program Files\\MySQL\\Connector.C++ 1.1\\lib\\opt 我已经安装了最新的MySQL软件包,并在以下位置包含了包含文件和库文件:C:\\ Program Files \\ MySQL \\ Connector.C ++ 1.1 \\ include C:\\ Program Files \\ MySQL \\ Connector.C ++ 1.1 \\ lib \\ opt

I have added the above include path to the "Include Directories" under the VC+= Directories menu in the Configuration properties as well as the above library path in the "Library Directories" on the same tab. 我已将上述包含路径添加到“配置属性”中VC + =目录菜单下的“包含目录”中,并将上述库路径添加到了同一选项卡上“库目录”中的上述库路径中。

Additionally I have added mysqlcppconn.lib as an "Additional Dependency in the Linker-Input page. 另外,我在“链接器输入”页面中将mysqlcppconn.lib添加为“其他依赖项”。

When I compile I get a LNK 2019 "unresolved external symbol __imp__get_driver_instance referenced in function _main" error...... 编译时出现LNK 2019“函数_main中引用的未解析的外部符号__imp__get_driver_instance”错误...

I suspect it is something fundamental that I am unaware of !!! 我怀疑这是我不知道的根本问题!

OK. 好。 So after another day of playing around I moved the header files and libraries for both of the places where MySQL was installed (the default Program Files directory and the Program Files (x86) directory) into a MySQL/include and MySQL/lib as well as MySQL(x86)/include and MySQL(x86)/lib in my projects directory. 因此,经过一天的闲逛,我将安装了MySQL的两个位置(默认的Program Files目录和Program Files(x86)目录)的头文件和库都移到了MySQL / include和MySQL / lib以及我的项目目录中的MySQL(x86)/ include和MySQL(x86)/ lib。

I tried linking to these in succession in the respective places in Visual Studio 2015. The program compiled linking to the x86 versions. 我尝试在Visual Studio 2015中的各个位置依次链接到这些文件。该程序已编译链接到x86版本。

The only thing I didn't realise was that I required the mysqlcppconn.dll in the run directory of the project to load correctly. 我唯一没有意识到的是,我需要项目的运行目录中的mysqlcppconn.dll才能正确加载。 Once I had Googled this the program loaded and ran successfully !! 一旦我搜索了这个程序,程序就会加载并成功运行!

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

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