2017-01-02 10 views
0

У меня возникла проблема с компиляцией простой тестовой программы MySQL.Vala: MySQLprogram не компилируется

Я использовал код из этого урока: fromdual.ch, но я компилирую с automake. Я создал этот простой тест в gnome-builder и только добавил --pkg mysql к флагам vala в src/Makefile.am, и я добавил проверку mysqsl на configure.ac. Я также попытался скомпилировать это с помощью команд компилятора от 1.

Обновление Я просто понял это, добавив некоторые вещи для автомикширования. Как и предполагалось, компоновщику не хватало флаги 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

Я не могу найти .pc файл pkg-config в любой из этих пакетов, так что вы можете также добавить включают каталоги и линкер флаги для компилятора и компоновщика C.

+1

На самом деле ошибки исходят от компоновщика. ЦК находит заголовки, но линкеру не рассказывают о библиотеке. IIRC - правый флаг '-lmysql'. – nemequ

+0

@ AIThomas: Я попытался установить соединитель, но сбой с ошибкой здания не изменился. –

+0

@nemequ: Я также попытался добавить флаг '-lmysql' в ld, но он терпит неудачу с'/usr/bin/ld: не может найти -lmysql' –