c++ - 在 Visual Studio 2013 中尝试使用 C++ 连接到 mysql 时出现未解析的外部问题

标签 c++ mysql visual-studio

我正在尝试在 Visual Studio 2013 中使用 C++ 连接到 mysql。我正在使用发布配置。我已经把目录放在

C:\mysql-5.7.11-winx64\lib C:\Program Files\MySQL\Connector.C++ 1.1\include C:\boost_1_60_0

in C/C++ general-->Additional Dependencies, VC++ Directories and Linker general->Additional Dependencies

我还在 Linker-->Input 上添加了 mysqlcppconn-static.lib 和 libmysql.lib。

现在我得到一个错误 错误 LNK1120:2 个 Unresolved external 问题
错误 LNK2001:无法解析的外部符号 _get_driver_instance
错误 LNK2001:无法解析的外部符号 _WinMain@16

我该如何解决这个问题?谢谢!

/* 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 <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(void)
{
cout << endl;
cout << "Let's have MySQL count from 10 to 1..." << endl;

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

  /* 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();
  stmt->execute("DROP TABLE IF EXISTS test");
  stmt->execute("CREATE TABLE test(id INT)");
  delete stmt;

  /* '?' is the supported placeholder syntax */
  pstmt = con->prepareStatement("INSERT INTO test(id) VALUES (?)");
  for (int i = 1; i <= 10; i++) {
    pstmt->setInt(1, i);
    pstmt->executeUpdate();
  }
  delete pstmt;

  /* Select in ascending order */
  pstmt = con->prepareStatement("SELECT id FROM test ORDER BY id ASC");
  res = pstmt->executeQuery();

  /* Fetch in reverse = descending order! */
  res->afterLast();
  while (res->previous())
    cout << "\t... MySQL counts: " << res->getInt("id") << endl;
  delete res;

  delete pstmt;
  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;
}

最佳答案

您遇到了一些问题。

  1. 首先,您的项目设置似乎需要 Windows 应用程序,而不是控制台应用程序,这可以通过尝试找到符号 _WinMain@16 而不是 main 来证明。仔细检查您的项目设置。

  2. 第二个是您尝试静态链接,但至少在某些版本的 mysql 中,您需要定义预处理器指令,以便符号对其余代码可见。具体来说,_get_driver_instance(),因为这是为那些动态链接到 mysql 库的人添加的函数。尝试在项目设置中定义 mysqlcppconn_EXPORTS 并重新构建。

关于c++ - 在 Visual Studio 2013 中尝试使用 C++ 连接到 mysql 时出现未解析的外部问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36313958/

相关文章:

c++ - POCO如何发送XML数据?

c++ - 我应该在哪个源文件中定义一个函数

mysql - 在 Yii2 中访问具有多个(结果集)的存储过程的所有数据集

php - 在 PHP 中同时执行多个 MySQL 数据库事件

windows - 将 PowerShell 用于 Visual Studio 命令提示符

c++ - IDE 使用 Visual Studio,但跨平台配置使用 CMake

c++ - 模板多重定义问题

c++ - 使用命名空间而不是单例

php - 防止恶意外部脚本

visual-studio - Microsoft Expression Studio 究竟是什么以及它如何与 Visual Studio 集成?