How to use mysql-arm64 installed with ‘brew’ to connect to database with c++ on m1 Mac?

  apple-m1, c++, mysql

I am using Apple M1 MacBook Air.
I want to connect MySQL to c++. I’ve tried 2 variants: ‘c++ connector’ installed from official MySQL website(with jdbc.h) and ‘mysql-8.0.26-macos11-arm64’ installed with ‘brew install mysql’.
I can include both of them with:

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

OR

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

And it successfully compiles.

But when I try to write any code that uses any of those libraries, it gives errors:

Code for 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;
}

Error:

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)

Code for mysql-arm64 from ‘brew’:

#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;
}

Error:

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)

Or maybe MySQL connector for c++ is not available on M1 MacBooks?

Help me, please)))

Source: Windows Questions C++

LEAVE A COMMENT