test_libmysqld.c
Код: Выделить всё
#include <mysql.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
mysql_server_init(0, NULL, NULL);
MYSQL* db = mysql_init(NULL);
mysql_real_connect(db, "localhost", "root", "password", "db", 0, NULL, 0);
// …здесь делаем с базой данных все, что понадобится
mysql_query(db, "SELECT * FROM tab");
MYSQL_RES* result = mysql_store_result(db);
MYSQL_ROW row = mysql_fetch_row(result);
//row - массив, содержащий значения полей записи
printf("1st row, 1st field: %s",row[0]);
mysql_close(db);
mysql_server_end();
return EXIT_SUCCESS;
}по моему проще некуда.
Makefile
Код: Выделить всё
# Предполагается, что программное обеспечение MySQL установлено в
#/usr/local/mysql
inc := /usr/include/mysql
lib := /usr/lib/mysql
CC := gcc
CPPFLAGS := -I$(inc) -D_THREAD_SAFE -D_REENTRANT
CFLAGS := -g -W -Wall
LDFLAGS := -static
# Можно изменить -lmysqld на -lmysqlclient для того, чтобы использовать
LDLIBS = -L$(lib) -lz -lm -lcrypt -lpthread
#LDLIBS += -lmysqld
LDLIBS += -lmysqlclient
# Это работает для простых однофайловых тестовых программ
sources := $(wildcard *.c)
objects := $(patsubst %c,%o,$(sources))
targets := $(basename $(sources))
all: $(targets)
clean:
rm -f $(targets) $(objects) *.coreтут мне с большего все понятно
Результат
Код: Выделить всё
jb@f-s:~/work/test_libmysqld$ make
gcc -g -W -Wall -I/usr/include/mysql -D_THREAD_SAFE -D_REENTRANT -static test_libmysqld.c -L/usr/lib/mysql -lz -lm -lcrypt -lpthread -lmysqlclient -o test_libmysqld
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(mf_pack.o): In function `unpack_dirname':
(.text+0x60d): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libmysql.o): In function `read_user_name':
(.text+0x60d1): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(mf_pack.o): In function `unpack_dirname':
(.text+0x622): warning: Using 'endpwent' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(my_gethostbyname.o): In function `my_gethostbyname_r':
(.text+0x3c): warning: Using 'gethostbyname_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libmysql.o): In function `mysql_server_init':
(.text+0x6b12): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libmysql.o): In function `fetch_float_with_conversion':
(.text+0x3f15): undefined reference to `floor'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libmysql.o): In function `.L738':
(.text+0x3fb4): undefined reference to `floor'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(password.o): In function `check_scramble_323':
(.text+0x73a): undefined reference to `floor'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(password.o): In function `check_scramble_323':
(.text+0x79d): undefined reference to `floor'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(password.o): In function `scramble_323':
(.text+0x887): undefined reference to `floor'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(password.o):(.text+0x8d0): more undefined references to `floor' follow
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(yassl_int.o): In function `yaSSL::yassl_int_cpp_local2::GetSelf()':
(.text+0x2293): undefined reference to `pthread_self'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(my_compress.o): In function `my_uncompress':
(.text+0x6b): undefined reference to `uncompress'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(my_compress.o): In function `my_compress_alloc':
(.text+0x120): undefined reference to `compress'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libtaocrypt_la-dh.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x4f6): undefined reference to `pow'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libtaocrypt_la-dh.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x504): undefined reference to `log'
/usr/lib/gcc/i486-linux-gnu/4.3.2/../../../../lib/libmysqlclient.a(libtaocrypt_la-dh.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x516): undefined reference to `pow'
collect2: ld returned 1 exit status
make: *** [test_libmysqld] Ошибка 1А вот тут уже ничего не понятно
Буду рад любой помощи, а так же ссылки на хорошее описание MySQL по C(c++)
За ранее спасибо.