Search code examples
mysqlmariadbfedoravala

Vala: MySQLprogram not compiling


I have a problem with compiling a simple MySQL test program.

I used the code from this tutorial: fromdual.ch but I'm compiling with automake. I created this simple test in gnome-builder and just added --pkg mysql to the vala flags in src/Makefile.am and I added a mysqsl check to configure.ac. I also tried to compile this with the compiler commands from 1.

Update I just got it right by adding some automake stuff. As suggested the linker was missing the mysql flags. I'm accepting AlThomas's answer, because I was missing the connector and I didn't want to write my own answer.

/* main.vala
 *
 * Copyright (C) 2017 Gerald Zehetner
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Gtk;
using Mysql;

int main (string[] args)
{

  int rc = 0;

  ClientFlag cflag    = 0;
  string     host     = "127.0.0.1";
  string     user     = "root";
  string     password = "";
  string     database = "test";
  int        port     = 3306;
  string     socket   = null;

  Database mysql = new Mysql.Database ();

  var isConnected = mysql.real_connect(host, user, password, database, port, socket, cflag);

  if ( ! isConnected ) {

    rc = 1;
    stdout.printf("ERROR %u: Connection failed: %s\n", mysql.errno(), mysql.error());
    return rc;
  }

  stdout.printf("Connected to MySQL server version: %s (%lu)\n"
              , mysql.get_server_info()
              , (ulong) mysql.get_server_version());

  string sql = "SELECT * FROM test LIMIT 10";
  rc = mysql.query(sql);
  if ( rc != 0 ) {

    stdout.printf("ERROR %u: Query failed: %s\n", mysql.errno(), mysql.error());
    return rc;
  }

  Result ResultSet = mysql.use_result();

  string[] MyRow;

  while ( (MyRow = ResultSet.fetch_row()) != null ) {

    stdout.printf("id: %s | data: %s | ts: %s\n", MyRow[0], MyRow[1], MyRow[2]);
  }
  // free_result is called automatically

  // mysql_close is called automatically
  return rc;
}

and the compiler output:

gmake 'all' '-j5'
gmake  all-recursive
gmake[1]: Entering directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu'
Making all in data
gmake[2]: Entering directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu/data'
gmake[2]: Leaving directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu/data'
Making all in src
gmake[2]: Entering directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu/src'
 cd /home/zege/Projects/test_mysql && /bin/sh /home/zege/Projects/test_mysql/build-aux/missing automake-1.15 --foreign src/Makefile
 cd .. && /bin/sh ./config.status src/Makefile depfiles
config.status: creating src/Makefile
config.status: executing depfiles commands
git.mk: Generating /home/zege/Projects/test_mysql/src/.gitignore
  CCLD     test_mysql
test_mysql-main.o: In function `_vala_mysql_fetch_row':
main.c:(.text+0x8c): undefined reference to `mysql_fetch_row'
main.c:(.text+0xb1): undefined reference to `mysql_num_fields'
test_mysql-main.o: In function `_vala_main':
main.c:(.text+0x3b6): undefined reference to `mysql_init'
main.c:(.text+0x45c): undefined reference to `mysql_real_connect'
main.c:(.text+0x4f0): undefined reference to `mysql_errno'
main.c:(.text+0x510): undefined reference to `mysql_error'
main.c:(.text+0x556): undefined reference to `mysql_close'
main.c:(.text+0x5f2): undefined reference to `mysql_get_server_info'
main.c:(.text+0x613): undefined reference to `mysql_get_server_version'
main.c:(.text+0x692): undefined reference to `mysql_query'
main.c:(.text+0x715): undefined reference to `mysql_errno'
main.c:(.text+0x735): undefined reference to `mysql_error'
main.c:(.text+0x795): undefined reference to `mysql_close'
main.c:(.text+0x823): undefined reference to `mysql_use_result'
main.c:(.text+0xaac): undefined reference to `mysql_free_result'
main.c:(.text+0xae4): undefined reference to `mysql_close'
collect2: error: ld returned 1 exit status
Makefile:460: recipe for target 'test_mysql' failed
gmake[2]: *** [test_mysql] Error 1
gmake[2]: Leaving directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu/src'
gmake[1]: *** [all-recursive] Error 1
Makefile:485: recipe for target 'all-recursive' failed
gmake: *** [all] Error 2
gmake[1]: Leaving directory '/home/zege/.cache/gnome-builder/builds/test_mysql/local/x86_64-linux-gnu'
Makefile:417: recipe for target 'all' failed

Solution

  • The errors are coming from the C compiler. It can't find definitions for the MySQL symbols. This suggests the C headers aren't installed.

    From the tags you've added to the question it looks like you are using Fedora and MariaDB. Hopefully issuing:

    dnf install mariadb-devel

    will solve your problem.

    The mysql_ symbols that are undefined are from the MariaDB Connector/C API Functions. With Fedora this is a separate package so you may also need to:

    dnf install mariadb-connector-c-devel

    I can't find a .pc file for pkg-config in either of those packages so you may also have to add the include directories and linker flags manually for the C compiler and linker.