如何将 MySQL 连接到 m1 Mac 上的 C++?

How to connect MySQL to c++ on m1 Mac?

我正在使用 Apple M1 MacBook Air。 我想将 MySQL 连接到 C++。我尝试了 2 种变体:'c++ connector' 从官方 MySQL website(使用 jdbc.h)安装,'mysql-8.0.26-macos11-arm64' 使用 'brew install mysql' 安装。 我可以将它们都包括在内:

#include </usr/local/mysql-connector-c++-8.0.27/include/mysql/jdbc.h>

#include </usr/local/mysql-8.0.26-macos11-arm64/include/mysql.h>

编译成功。

但是当我尝试编写任何使用这些库的代码时,它会出错:

JDBC的代码:

#include </usr/local/mysql-connector-c++-8.0.27/include/mysql/jdbc.h>

using namespace std;

int main() {
        try {
                sql::mysql::MySQL_Driver *driver;
                sql::Connection *con;
        
                driver = sql::mysql::get_mysql_driver_instance();
                con = driver->connect("tcp://127.0.0.1:3306", "root", "rootpass");
        
                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;
        }
        return 0;
}

错误:

Undefined symbols for architecture arm64:
  "check(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
      check_lib() in sql-c9ebd3.o
  "check(std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > > const&)", referenced from:
      check_lib() in sql-c9ebd3.o
  "sql::mysql::_get_driver_instance_by_name(char const*)", referenced from:
      sql::mysql::get_driver_instance_by_name(char const*) in sql-c9ebd3.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

来自 'brew' 的 mysql-arm64 代码:

#include </usr/local/mysql-8.0.26-macos11-arm64/include/mysql.h>
#include <iostream>
#include <stdlib.h>

using namespace std;
int main(void)
{
  MYSQL_RES *result;
  MYSQL_ROW row;
  MYSQL *connection, mysql;

  int state;

  mysql_init(&mysql);

  connection = mysql_real_connect(&mysql,"127.0.0.1","root","rootpass","mysql",0,0,0);

  if (connection == NULL)
  {
    std::cout << mysql_error(&mysql) << std::endl;
  }
state = mysql_query(connection, "SHOW TABLES");
  if (state !=0)
  {
    std::cout << mysql_error(connection) << std::endl;
  }

  result = mysql_store_result(connection);

  std::cout << "tables: " << mysql_num_rows(result) << std::endl;
  while ( ( row=mysql_fetch_row(result)) != NULL )
  {
    cout << row[0] << std::endl;
  }

  mysql_free_result(result);
  mysql_close(connection);
  return 0;
}

错误:

Undefined symbols for architecture arm64:
    "_mysql_close", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_error", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_fetch_row", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_free_result", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_init", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_num_rows", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_query", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_real_connect", referenced from:
      _main in sql_another-80b5eb.o
  "_mysql_store_result", referenced from:
      _main in sql_another-80b5eb.o
   ld: symbol(s) not found for architecture arm64
   clang: error: linker command failed with exit code 1 (use -v to see invocation)

或者 MySQL 用于 C++ 的连接器在 M1 MacBook 上不可用?

请帮帮我)))

是的,你是对的。尚无 MySQL M1 的 c++ 连接器。

我希望您在此期间找到了解决方案,否则这里有一个可行的方法。 我使用的是 Macbook Air M1、MacOs 12.2 Monterey,MySQL 通过 Homebrew (brew install mysql) 安装。 Openssl 也是通过自制软件安装的。

编译我使用: gcc -o filename.out filename.c `mysql_config --libs`

请注意最后一条命令中使用的反引号

我第一次使用此命令时,ld 出现了未找到的其他库(zstd、ssl 等):只需通过此命令方案将正确的路径添加到 LIBRARY_PATH: export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix NAME_OF_LIBRARY)/lib/

例如,如果缺少的库是 lssl,则 NAME_OF_LIBRARY 将是 ssl,或者如果错误是针对 lzstd,则 NAME_OF_LIBRARY 将是 zstd