2017-01-02 5 views
0

シンプルなMySQLテストプログラムのコンパイルに問題があります。Vala:MySQLプログラムがコンパイルされない

私はこのチュートリアルのコードを使用しました:fromdual.chしかし、私はautomakeでコンパイルしています。私はgnome-builderでこの簡単なテストを作成し、ちょうどsrc/Makefile.amのvalaフラグに--pkg mysqlを追加し、configure.acにmysqslチェックを追加しました。私はまた、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 

答えて

2

エラーが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コンパイラとリンカのフラグを手動で設定します。

+1

実際、リンカからエラーが発生しています。 CCはヘッダーを見つけていますが、リンカーはライブラリについて語られていません。 IIRCの右のフラグは '-lmysql'です。 – nemequ

+0

@ AIThomas:コネクタをインストールしようとしましたが、同じエラーでビルドに失敗しました。 –

+0

@nemequ:ldに '-lmysql'フラグを追加しようとしましたが、'/usr/bin/ld:-lmysql'を見つけることができませんでした。 –

関連する問題