How should I use libdrizzle?

Asked by Akihiro Seki on 2013-02-03

Hi!
I am making mysql tunnel application.
Apache(PHP)--(mysql protocol)-->My application(c++)--(mysql protocol)-->Mysql
My application receives the request of the Web server and takes a certain step and transfers it to Mysql.
So I think that I should use libdrizzle.
When I compile it, my source code fails.

I make and install drizzle-7.1.36-stable.
My server is "Red Hat Enterprise Linux Server release 6.3 (Santiago)"

Could you give me a advice for this?

<Drizzle Install>
wget https://launchpad.net/drizzle/7.1/7.1.36/+download/drizzle-7.1.36-stable.tar.gz
tar zxvf drizzle-7.1.36-stable.tar.gz
cd drizzle-7.1.36-stable
./config/autorun.sh
./configure
make
make install
cp -r libdrizzle-2.0 /usr/local/include

<Result>
[root]# make
gcc -c -I/usr/include/mysql/ server.cc
gcc -o server -L/usr/lib64/mysql server.o -L/root/drizzle-7.1.36-stable/libdrizzle-2.0/.libs
server.o: In function `main':
server.cc:(.text+0x17e): undefined reference to `drizzle_create'
server.cc:(.text+0x1cc): undefined reference to `drizzle_set_option'
server.cc:(.text+0x1dd): undefined reference to `drizzle_set_verbose'
server.cc:(.text+0x1e9): undefined reference to `drizzle_con_create'
server.cc:(.text+0x232): undefined reference to `drizzle_con_add_options'
server.cc:(.text+0x249): undefined reference to `drizzle_con_set_tcp'
server.cc:(.text+0x260): undefined reference to `drizzle_con_add_options'
server.cc:(.text+0x26c): undefined reference to `drizzle_con_listen'
server.cc:(.text+0x281): undefined reference to `drizzle_error'
server.cc:(.text+0x2be): undefined reference to `drizzle_con_accept'
server.cc:(.text+0x2d5): undefined reference to `drizzle_error'
server.cc:(.text+0x31e): undefined reference to `drizzle_con_free'
server.cc:(.text+0x33f): undefined reference to `drizzle_con_free'
server.cc:(.text+0x34b): undefined reference to `drizzle_free'
server.o: In function `server(drizzle_st*, drizzle_con_st*, drizzle_column_st*)':
server.cc:(.text+0x3a2): undefined reference to `drizzle_con_set_protocol_version'
server.cc:(.text+0x3b6): undefined reference to `drizzle_con_set_server_version'
server.cc:(.text+0x3ca): undefined reference to `drizzle_con_set_thread_id'
server.cc:(.text+0x3e1): undefined reference to `drizzle_con_set_scramble'
server.cc:(.text+0x3f5): undefined reference to `drizzle_con_set_capabilities'
server.cc:(.text+0x409): undefined reference to `drizzle_con_set_charset'
server.cc:(.text+0x41d): undefined reference to `drizzle_con_set_status'
server.cc:(.text+0x431): undefined reference to `drizzle_con_set_max_packet_size'
server.cc:(.text+0x440): undefined reference to `drizzle_handshake_server_write'
server.cc:(.text+0x459): undefined reference to `drizzle_error'
server.cc:(.text+0x47f): undefined reference to `drizzle_handshake_client_read'
server.cc:(.text+0x498): undefined reference to `drizzle_error'
server.cc:(.text+0x4be): undefined reference to `drizzle_result_create'
server.cc:(.text+0x4dd): undefined reference to `drizzle_error'
server.cc:(.text+0x50f): undefined reference to `drizzle_result_write'
server.cc:(.text+0x528): undefined reference to `drizzle_error'
server.cc:(.text+0x54c): undefined reference to `drizzle_result_free'
server.cc:(.text+0x576): undefined reference to `drizzle_con_command_buffer'
server.cc:(.text+0x5b8): undefined reference to `drizzle_error'
server.cc:(.text+0x5de): undefined reference to `drizzle_result_create'
server.cc:(.text+0x5fd): undefined reference to `drizzle_error'
server.cc:(.text+0x637): undefined reference to `drizzle_result_write'
server.cc:(.text+0x650): undefined reference to `drizzle_error'
server.cc:(.text+0x67e): undefined reference to `drizzle_result_set_column_count'
server.cc:(.text+0x699): undefined reference to `drizzle_result_write'
server.cc:(.text+0x6b2): undefined reference to `drizzle_error'
server.cc:(.text+0x6df): undefined reference to `drizzle_column_create'
server.cc:(.text+0x6f8): undefined reference to `drizzle_error'
server.cc:(.text+0x723): undefined reference to `drizzle_column_set_catalog'
server.cc:(.text+0x737): undefined reference to `drizzle_column_set_db'
server.cc:(.text+0x74b): undefined reference to `drizzle_column_set_table'
server.cc:(.text+0x75f): undefined reference to `drizzle_column_set_orig_table'
server.cc:(.text+0x773): undefined reference to `drizzle_column_set_name'
server.cc:(.text+0x787): undefined reference to `drizzle_column_set_orig_name'
server.cc:(.text+0x79b): undefined reference to `drizzle_column_set_charset'
server.cc:(.text+0x7af): undefined reference to `drizzle_column_set_size'
server.cc:(.text+0x7c3): undefined reference to `drizzle_column_set_type'
server.cc:(.text+0x7d9): undefined reference to `drizzle_column_write'
server.cc:(.text+0x7f2): undefined reference to `drizzle_error'
server.cc:(.text+0x81d): undefined reference to `drizzle_column_set_name'
server.cc:(.text+0x831): undefined reference to `drizzle_column_set_orig_name'
server.cc:(.text+0x847): undefined reference to `drizzle_column_write'
server.cc:(.text+0x860): undefined reference to `drizzle_error'
server.cc:(.text+0x886): undefined reference to `drizzle_column_free'
server.cc:(.text+0x897): undefined reference to `drizzle_result_set_eof'
server.cc:(.text+0x8b2): undefined reference to `drizzle_result_write'
server.cc:(.text+0x8cb): undefined reference to `drizzle_error'
server.cc:(.text+0x97a): undefined reference to `drizzle_result_calc_row_size'
server.cc:(.text+0x986): undefined reference to `drizzle_row_write'
server.cc:(.text+0x99f): undefined reference to `drizzle_error'
server.cc:(.text+0x9d1): undefined reference to `drizzle_field_write'
server.cc:(.text+0x9ea): undefined reference to `drizzle_error'
server.cc:(.text+0xa1c): undefined reference to `drizzle_field_write'
server.cc:(.text+0xa35): undefined reference to `drizzle_error'
server.cc:(.text+0xa79): undefined reference to `drizzle_result_write'
server.cc:(.text+0xa96): undefined reference to `drizzle_error'
server.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [server] Error 1

<Makefile>
server : server.o
        gcc -o server -L/usr/lib64/mysql server.o -L/root/drizzle-7.1.36-stable/libdrizzle-2.0/.libs

server.o : server.cc
        gcc -c -I/usr/include/mysql/ server.cc

<server.cc>
(This is examples of Drizzle)
/*
 * Drizzle Client & Protocol Library
 *
 * Copyright (C) 2008 Eric Day (<email address hidden>)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *
 * * The names of its contributors may not be used to endorse or
 * promote products derived from this software without specific prior
 * written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

//#include "config.h"

#define __STDC_LIMIT_MACROS
#include <stdint.h>

#define __STDC_FORMAT_MACROS
#include <inttypes.h>

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <libdrizzle-2.0/drizzle_server.h>

#define DRIZZLE_FIELD_MAX 32
#define DRIZZLE_RESULT_ROWS 20

#define DRIZZLE_RETURN_CHECK(__ret, __function, __drizzle) \
{ \
  if ((__ret) != DRIZZLE_RETURN_OK) \
    DRIZZLE_RETURN_ERROR(__function, __drizzle) \
}

#define DRIZZLE_RETURN_ERROR(__function, __drizzle) \
{ \
  printf(__function ":%s\n", drizzle_error(__drizzle)); \
  return; \
}

static void server(drizzle_st *drizzle, drizzle_con_st *con, drizzle_column_st *column);

int main(int argc, char *argv[])
{
  int c;
  uint32_t count= 0;
  const char *host= NULL;
  bool mysql= false;
  in_port_t port= 0;
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
  drizzle_return_t ret;
  drizzle_st *drizzle;
  drizzle_column_st column;

  while((c = getopt(argc, argv, "c:h:mp:v")) != -1)
  {
    switch(c)
    {
    case 'c':
      count= (uint32_t)atoi(optarg);
      break;

    case 'h':
      host= optarg;
      break;

    case 'm':
      mysql= true;
      break;

    case 'p':
      port= (in_port_t)atoi(optarg);
      break;

    case 'v':
      switch(verbose)
      {
      case DRIZZLE_VERBOSE_NEVER:
        verbose= DRIZZLE_VERBOSE_FATAL;
        break;
      case DRIZZLE_VERBOSE_FATAL:
        verbose= DRIZZLE_VERBOSE_ERROR;
        break;
      case DRIZZLE_VERBOSE_ERROR:
        verbose= DRIZZLE_VERBOSE_INFO;
        break;
      case DRIZZLE_VERBOSE_INFO:
        verbose= DRIZZLE_VERBOSE_DEBUG;
        break;
      case DRIZZLE_VERBOSE_DEBUG:
        verbose= DRIZZLE_VERBOSE_CRAZY;
        break;
      case DRIZZLE_VERBOSE_CRAZY:
      case DRIZZLE_VERBOSE_MAX:
        break;
      }
      break;

    default:
      printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n",
             argv[0]);
      printf("\t-c <count> - Number of connections to accept before exiting\n");
      printf("\t-h <host> - Host to listen on\n");
      printf("\t-m - Use the MySQL protocol\n");
      printf("\t-p <port> - Port to listen on\n");
      printf("\t-v - Increase verbosity level\n");
      return 1;
    }
  }

  if ((drizzle= drizzle_create()) == NULL)
  {
    fprintf(stderr, "drizzle_create:NULL\n");
    return 1;
  }

  drizzle_set_option(drizzle, DRIZZLE_FREE_OBJECTS, true);
  drizzle_set_verbose(drizzle, verbose);

  drizzle_con_st* con_listen;
  if ((con_listen= drizzle_con_create(drizzle)) == NULL)
  {
    fprintf(stderr, "drizzle_con_create:NULL\n");
    return 1;
  }

  drizzle_con_add_options(con_listen, DRIZZLE_CON_LISTEN);
  drizzle_con_set_tcp(con_listen, host, port);

  if (mysql)
  {
    drizzle_con_add_options(con_listen, DRIZZLE_CON_MYSQL);
  }

  if (drizzle_con_listen(con_listen) != DRIZZLE_RETURN_OK)
  {
    fprintf(stderr, "drizzle_con_listen:%s\n", drizzle_error(drizzle));
    return 1;
  }

  while (1)
  {
    drizzle_con_st *con= drizzle_con_accept(drizzle, &ret);
    if (ret != DRIZZLE_RETURN_OK)
    {
      fprintf(stderr, "drizzle_con_accept:%s\n", drizzle_error(drizzle));
      return 1;
    }

    server(drizzle, con, &column);

    drizzle_con_free(con);

    if (count > 0)
    {
      count--;

      if (count == 0)
        break;
    }
  }

  drizzle_con_free(con_listen);
  drizzle_free(drizzle);

  return 0;
}

static void server(drizzle_st *drizzle, drizzle_con_st *con,
                   drizzle_column_st *column)
{
  drizzle_return_t ret;
  drizzle_command_t command;
  uint8_t *data= NULL;
  size_t total;
  char *field[2];
  char field1[DRIZZLE_FIELD_MAX];
  char field2[DRIZZLE_FIELD_MAX];
  size_t size[2];
  uint64_t x;

  field[0]= field1;
  field[1]= field2;

  /* Handshake packets. */
  drizzle_con_set_protocol_version(con, 10);
  drizzle_con_set_server_version(con, "libdrizzle example 1.2.3");
  drizzle_con_set_thread_id(con, 1);
  drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
  drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
  drizzle_con_set_charset(con, 8);
  drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
  drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);

  ret= drizzle_handshake_server_write(con);
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_server_write", drizzle)

  ret= drizzle_handshake_client_read(con);
  DRIZZLE_RETURN_CHECK(ret, "drizzle_handshake_client_read", drizzle);

  drizzle_result_st *result;
  if ((result= drizzle_result_create(con)) == NULL)
  {
    DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
  }

  ret= drizzle_result_write(con, result, true);
  DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

  /* Command loop. */
  while (1)
  {
    drizzle_result_free(result);
    free(data);

    data= (uint8_t *)drizzle_con_command_buffer(con, &command, &total, &ret);
    if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
        (ret == DRIZZLE_RETURN_OK && command == DRIZZLE_COMMAND_QUIT))
    {
      free(data);
      return;
    }
    DRIZZLE_RETURN_CHECK(ret, "drizzle_con_command_buffer", drizzle)

    if ((result= drizzle_result_create(con)) == NULL)
    {
      DRIZZLE_RETURN_ERROR("drizzle_result_create", drizzle)
    }

    if (command != DRIZZLE_COMMAND_QUERY)
    {
      ret= drizzle_result_write(con, result, true);
      DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
      continue;
    }

    drizzle_result_set_column_count(result, 2);

    ret= drizzle_result_write(con, result, false);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

    /* Columns. */
    if (drizzle_column_create(result, column) == NULL)
    {
      DRIZZLE_RETURN_ERROR("drizzle_column_create", drizzle)
    }

    drizzle_column_set_catalog(column, "default");
    drizzle_column_set_db(column, "drizzle_test_db");
    drizzle_column_set_table(column, "drizzle_test_table");
    drizzle_column_set_orig_table(column, "drizzle_test_table");
    drizzle_column_set_name(column, "test_column_1");
    drizzle_column_set_orig_name(column, "test_column_1");
    drizzle_column_set_charset(column, 8);
    drizzle_column_set_size(column, DRIZZLE_FIELD_MAX);
    drizzle_column_set_type(column, DRIZZLE_COLUMN_TYPE_VARCHAR);

    ret= drizzle_column_write(result, column);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)

    drizzle_column_set_name(column, "test_column_2");
    drizzle_column_set_orig_name(column, "test_column_2");

    ret= drizzle_column_write(result, column);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_column_write", drizzle)

    drizzle_column_free(column);

    drizzle_result_set_eof(result, true);

    ret= drizzle_result_write(con, result, false);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)

    /* Rows. */
    for (x= 0; x < DRIZZLE_RESULT_ROWS; x++)
    {
      size[0]= (size_t)snprintf(field[0], DRIZZLE_FIELD_MAX,
                                "field %" PRIu64 "-1", x);
      if (size[0] >= DRIZZLE_FIELD_MAX)
        size[0]= DRIZZLE_FIELD_MAX - 1;

      size[1]= (size_t)snprintf(field[1], DRIZZLE_FIELD_MAX,
                                "field %" PRIu64 "-2", x);
      if (size[1] >= DRIZZLE_FIELD_MAX)
        size[1]= DRIZZLE_FIELD_MAX - 1;

      /* This is needed for MySQL and old Drizzle protocol. */
      drizzle_result_calc_row_size(result, (drizzle_field_t *)field, size);

      ret= drizzle_row_write(result);
      DRIZZLE_RETURN_CHECK(ret, "drizzle_row_write", drizzle)

      /* Fields. */
      ret= drizzle_field_write(result, (drizzle_field_t)field[0], size[0],
                               size[0]);
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)

      ret= drizzle_field_write(result, (drizzle_field_t)field[1], size[1],
                               size[1]);
      DRIZZLE_RETURN_CHECK(ret, "drizzle_field_write", drizzle)
    }

    ret= drizzle_result_write(con, result, true);
    DRIZZLE_RETURN_CHECK(ret, "drizzle_result_write", drizzle)
  }
}

Question information

Language:
English Edit question
Status:
Answered
For:
Drizzle Edit question
Assignee:
No assignee Edit question
Last query:
2013-02-03
Last reply:
2013-02-04
Andrew Hutchings (linuxjedi) said : #1

The biggest problem here is you have told gcc where the link files are, but not what files to link to. You should use -ldrizzle (note that is a lowercase L).

I should also note that if you are just using libdrizzle you should check out https://launchpad.net/libdrizzle which has a much simpler and more developed API (and the 5.1 API will be stable in a few days time).

Can you help with this problem?

Provide an answer of your own, or ask Akihiro Seki for more information if necessary.

To post a message you must log in.