0

i am trying to develop a server application that get English word from client side than translate it to Turkish word. Server should use litesql during translation process. During this process, database should have at least following key value pairs:

  • Key: Value

  • one: bir

  • two: iki

  • three: uc

  • four: dort

  • five: bes

  • six: alti

  • seven: yedi

  • eight: sekiz

  • nine: dokuz

  • ten: on

i get this error code below whenever i try to compile this code:

21701047@db:~$ gcc threadv1.c -o threadv1 -lpthread
/tmp/ccMnTKvk.o: In function `Child':
threadv1.c:(.text+0x54): undefined reference to `sqlite3_open'
threadv1.c:(.text+0x6c): undefined reference to `sqlite3_errmsg'
collect2: error: ld returned 1 exit status

Code

#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <sqlite3.h>

/* Definations */
#define DEFAULT_BUFLEN 1024
#define PORT 1888

void PANIC(char* msg);
#define PANIC(msg)  { perror(msg); exit(-1); }

/*--------------------------------------------------------------------*/
/*--- Child - echo server                                         ---*/
/*--------------------------------------------------------------------*/
void* Child(void* arg)
{   char line[DEFAULT_BUFLEN];
    int bytes_read;
    int client = *(int *)arg;
    char* f_name = "translator.db";
     sqlite3 *db;
     char *zErrMsg = 0;
     int rc;
     char *sql;

            printf("[server] send %s to the client...", f_name);
             rc = sqlite3_open("translator.db", &db);
             if( rc ) {
             fprintf(stderr, "Can't open database: %s\n",  sqlite3_errmsg(db));
                exit(1);
            }
           do
           {
             bytes_read = recv(client, line, sizeof(line), rc);
            if (bytes_read > 0) {
                if ( (bytes_read=send(client, line, bytes_read, 0)) < 0 ) {
                        printf("Send failed\n");
                        break;
                }
        } else if (bytes_read == 0 ) {
                printf("Connection closed by client\n");
                break;
        } else {
                printf("Connection has problem\n");
                break;
        }
 } while (bytes_read > 0);
    close(client);
    return arg;
}
/*--------------------------------------------------------------------*/
/*--- main - setup server and await connections (no need to clean  ---*/
/*--- up after terminated children.                                ---*/
/*--------------------------------------------------------------------*/
int main(int argc, char *argv[])
{   int sd,opt,optval;
    struct sockaddr_in addr;
    unsigned short port=0;

    while ((opt = getopt(argc, argv, "p:")) != -1) {
        switch (opt) {
        case 'p':
                port=atoi(optarg);
                break;
        }
    }


    if ( (sd = socket(PF_INET, SOCK_STREAM, 0)) < 0 )
        PANIC("Socket");
    addr.sin_family = AF_INET;

    if ( port > 0 )
                addr.sin_port = htons(port);
    else
                addr.sin_port = htons(PORT);

    addr.sin_addr.s_addr = INADDR_ANY;

   // set SO_REUSEADDR on a socket to true (1):
   optval = 1;
   setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);


    if ( bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
        PANIC("Bind");
    if ( listen(sd, SOMAXCONN) != 0 )
        PANIC("Listen");

    printf("System ready on port %d\n",ntohs(addr.sin_port));

    while (1)
    {
        int client, addr_size = sizeof(addr);
        pthread_t child;

        client = accept(sd, (struct sockaddr*)&addr, &addr_size);
        printf("Connected: %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
        if ( pthread_create(&child, NULL, Child, &client) != 0 )
            perror("Thread creation");
        else
            pthread_detach(child);  /* disassociate from parent */
    }
    return 0;
}
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
idogbo
  • 1
  • 3
  • 1
    permanent solution for these is NLP – Shubham Sharma Dec 24 '17 at 16:21
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – J.J. Hakala Dec 24 '17 at 16:26
  • 1
    `-lsqlite3` is probably missing – J.J. Hakala Dec 24 '17 at 16:27
  • ok i compiled it with the -lsqlite3 and when i try to run i get this: 21701047@db:~$ gcc threadv1.c -o threadv1 -lpthread -lsqlite3 21701047@db:~$ ./threadv1 Bind: Address already in use any possible solution – idogbo Dec 24 '17 at 16:40
  • @idogbo that's another question entirely. Please use a debugger to gather as much information as you can, and post another question with that problem if you can't solve it by yourself. – Quentin Dec 24 '17 at 19:00

0 Answers0