Vala:MySQL 程序未编译

Vala: MySQLprogram not compiling

我在编译一个简单的 MySQL 测试程序时遇到问题。

我使用了本教程中的代码: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

更新 我只是通过添加一些 automake 的东西来做对了。正如所建议的那样,链接器缺少 mysql 标志。 我接受 AlThomas 的回答,因为我缺少连接器而且我不想写我自己的答案。

/* 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;
}

和编译器输出:

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

错误来自 C 编译器。它找不到 MySQL 符号的定义。这表明未安装 C 头文件。

从您添加到问题的标签来看,您似乎正在使用 Fedora 和 MariaDB。希望发行:

dnf install mariadb-devel

将解决您的问题。

未定义的mysql_个符号来自MariaDB Connector/C API Functions。对于 Fedora,这是一个单独的包,因此您可能还需要:

dnf install mariadb-connector-c-devel

我在这两个包中都找不到 pkg-config.pc 文件,因此您可能还必须为 C 编译器和链接器手动添加包含目录和链接器标志。