How to compile with gcc on Mac OS using mariadb-connector-c

87 Views Asked by At

I have successfully installed mariadb and mariadb-connector-c on Mac OS Monterey using Homebrew but cannot figure out how to compile a simple c source file with gcc. If I try:

gcc -o test main.c
# main.c:1:10: fatal error: 'mysql.h' file not found

Trying something different, if I adapt the MySQL instructions for using its own connector-c here, I've tried:

gcc -o main main.c /usr/local/bin/mariadb_config
# ld: can't link with a main executable file '/usr/local/bin/mariadb_config' for architecture x86_64

Another thing I've tried is adapting the answer here for mariadb:

gcc -o test main.c $(mariadb_config --libs)
# main.c:1:10: fatal error: 'mysql.h' file not found

How can I compile a c program that uses mariadb-connector-c on Mac?

FYI, here is main.c:

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  MYSQL *con = mysql_init(NULL);

  if (con == NULL)
  {
    fprintf(stderr, "%s\n", mysql_error(con));
    exit(1);
  }

  if (mysql_real_connect(con, "localhost", "(...)", "(...)", NULL, 0, NULL, 0) == NULL)
  {
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
  }

  if (mysql_query(con, "CREATE DATABASE testdb"))
  {
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
  }

  mysql_close(con);

  printf("MySQL client version: %s\n", mysql_get_client_info());

  exit(0);
}
1

There are 1 best solutions below

0
Georg Richter On BEST ANSWER

gcc needs to know where to find both MariaDB Connector/C include files (in most cases mysql.h) and the location and names of the MariaDB C/C libraries.

You can either specify them with -I/include_path -L/library_path -lmariadb or you can use mariadb_config:

gcc -o test main.c $(mariadb_config --include --libs)

Source: https://mariadb.com/docs/skysql-previous-release/connect/programming-languages/c/development/