Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Compiling (http://www.go4expert.com/forums/compiling-t20506/)

uBiT 25Dec2009 22:25

Compiling
 
Hello, i'am trying to compile on Ubuntu 9.10.
But i got errors:
Code:

ubyc@ubyc-desktop:~/Downloads/0.0.5.5$ make
g++ -O2 -Wall -lz  -ldl -lcrypt  -oserver main.cpp
main.cpp: In function ‘void daemonize()’:
main.cpp:142: error: ‘strlen’ was not declared in this scope
main.cpp:109: warning: ignoring return value of ‘int dup(int)’, declared with attribute warn_unused_result
main.cpp:110: warning: ignoring return value of ‘int dup(int)’, declared with attribute warn_unused_result
main.cpp: In function ‘int arguments(int, char**)’:
main.cpp:161: error: ‘strcmp’ was not declared in this scope
main.cpp:166: error: ‘strcmp’ was not declared in this scope
main.cpp:202: error: ‘strcpy’ was not declared in this scope
main.cpp: In function ‘int init_sig()’:
main.cpp:258: error: ‘memset’ was not declared in this scope
make: *** [preklad] Error 1

How can i fix it? Please help me, and sorry for my bad english...

techgeek.in 25Dec2009 22:56

Re: Compiling
 
Quote:

Originally Posted by uBiT (Post 62253)
Hello, i'am trying to compile on Ubuntu 9.10.
But i got errors:
Code:

ubyc@ubyc-desktop:~/Downloads/0.0.5.5$ make
g++ -O2 -Wall -lz  -ldl -lcrypt  -oserver main.cpp
main.cpp: In function ‘void daemonize()’:
main.cpp:142: error: ‘strlen’ was not declared in this scope
main.cpp:109: warning: ignoring return value of ‘int dup(int)’, declared with attribute warn_unused_result
main.cpp:110: warning: ignoring return value of ‘int dup(int)’, declared with attribute warn_unused_result
main.cpp: In function ‘int arguments(int, char**)’:
main.cpp:161: error: ‘strcmp’ was not declared in this scope
main.cpp:166: error: ‘strcmp’ was not declared in this scope
main.cpp:202: error: ‘strcpy’ was not declared in this scope
main.cpp: In function ‘int init_sig()’:
main.cpp:258: error: ‘memset’ was not declared in this scope
make: *** [preklad] Error 1

How can i fix it? Please help me, and sorry for my bad english...

what r u trying to compile?:surprised ..give ur code...

uBiT 25Dec2009 23:10

Re: Compiling
 
Code:

#ifdef __WIN32__
 #include <windows.h>
#else
 #include <netinet/in.h>
 #include <crypt.h>
#endif
#include <iostream>
#include <string>
#include <signal.h>
#include <cstdlib>
#include <stdio.h>

#include "commands.h"
#include "user.h"
#include "utils.h"
#include "config.h"
#include "sockets.h"
#include "trigger.h"
#include "defaults.h"
#include "ban.h"
#include "chatroom.h"
#include "elua.h"
#include "hublist.h"
#include "plugin.h"
#include "user_rights.h"

using namespace std;

int cont = 1;              // 1 = run / 0 = shutdown

extern user_t user_list;
extern command_t command_list;
extern config_t config;
extern account_t accounts_list;
extern config_elem_t config_list;

extern int port;                    // ID of port
extern int mainSocket;              // Main socket


/*************************************************************\
|              ARGUMENTS                      |
\*************************************************************/

#ifndef __WIN32__
/* This function was taken from aquila hubsoft and was modified */
void daemonize ()
{
    int i, lockfd;
      char pid[16];

    int ipid = getpid ();

      /* detach if asked */
      if (ipid == 1)
        return;            /* already a daemon */

        /* fork to guarantee we are not process group leader */
        i = fork ();
        if (i < 0)
              exit (1);            /* fork error */
        if (i > 0)
              exit (0);            /* parent exits */

        /* child (daemon) continues */
        setsid ();            /* obtain a new process group */
    ipid = getpid ()+1;

    printf ("> started with pid -> %d\n", ipid);
        /* fork again so we become process group leader
          * and cannot regain a controlling tty
          */
        i = fork ();
        if (i < 0)
              exit (1);            /* fork error */
        if (i > 0)
          exit (0);            /* parent exits */

        /* close all fds */
        for (i = getdtablesize (); i >= 0; --i)
              close (i);        /* close all descriptors */



        /* close parent fds and send output to fds 0, 1 and 2 to bitbucket */
        i = open ("/dev/null", O_RDWR);
        if (i < 0)
              exit (1);
        dup (i);
        dup (i);            /* handle standart I/O */

      /* create local lock */
      lockfd = open ("lamahub.pid", O_RDWR | O_CREAT, 0640);
      if (lockfd < 0) {
            perror ("lock: open");
            exit (1);
      }
    #ifndef __CYGWIN__
    /* lock the file */
    if (lockf (lockfd, F_TLOCK, 0) < 0) {
        perror ("lock: lockf");
        printf (HUBSOFT " is already running.\n");
        exit (0);
    }
    #else
    /* lock the file */
    {
    struct flock lock;
        lock.l_type = F_RDLCK;
        lock.l_start = 0;
        lock.l_whence = SEEK_SET;
        lock.l_len = 0;
       
        if (fcntl (lockfd, F_SETLK, &lock) < 0) {
            printf (HUBSOFT " is already running.\n");
            exit (0);
        }
    }
    #endif
    /* write to pid to lockfile */
    snprintf (pid, 16, "%d\n", getpid ());
    write (lockfd, pid, strlen (pid));
   


    /* restrict created files to 0750 */
    umask (027);
}
#endif

int arguments (int argc, char *argv[])
{
    string str;
    unsigned int i = 0;

    if (argc == 1)
        return 0;

    if (argc == 2) {
#ifndef __WIN32__
        if (!strcmp (argv[1], "-d")) {
            daemonize ();
            return 0;
        }
#endif
        if (!strcmp (argv[1], "-a")) {

            char name [128], passwd [128];
            printf ("Owner account\nYour owner name: ");
            scanf ("%s", name);
            printf ("Password: ");
            scanf ("%s", passwd);

            str += "regnum 1\n43520 44544 306689 306701 323149 323165\n";
            str += name;
            str += " ";
            str += crypt (passwd, HUBSOFT);
            str += " 5";
            i ++;
        }
    }
    if (argc == 3) {
        str += "regnum 1\n43520 44544 306689 306701 323149 323165\n";
        str += argv[1];
        str += " ";
        str += crypt (argv[2], HUBSOFT);
        str += " 5";
        i ++;
    }

      if (!i)
      {
        cerr << "> ERROR -> Syntax (create new owner account):" << endl;
            cerr << argv[0] << " <nick> <password>" << endl;
            return -1;
        }

    FILE *fp;

    char filename[80];
    filename[0] = '\0';
    strcpy (filename, DEFAULT_ACCOUNTSFILE);   

    fp = fopen (filename, "w+");

    if (!fp)
        return -2;
   
    fprintf (fp, "%s", str.c_str());

      fclose (fp);

    printf ("\n** Your Owner account was created. You can start hub now ! **\n");

    return 1;
}

/*************************************************************\
|              APP SIGNALS                      |
\*************************************************************/

void alarm_signal (int z)
{
    //cout << "> WARNING -> alarm signal -> type " << z << endl;
    //usleep (1000);
}

void term_signal (int z)
{
    cout << "> WARNING -> term signal -> type " << z << endl;

    usleep (1024);

    // close all sockets
    user_t *user;
    for (user = user_list.next; user != &user_list; user = user->next) {
        close (user->socket);
        //free (user);
    }

    /*config_elem_t *cfg;        // FIXME memleak in exit (calloc not free'ied)
    for (cfg = config_list.next; cfg != &config_list; cfg = cfg->next)
        if (cfg->value)
            free (cfg->value);*/

    cont = 0;    // Bye Bye :)
}

/*************************************************************\
|          INITIALIZATION OF APP              |
\*************************************************************/

int init_sig (void)
{
#ifndef __WIN32__
  struct sigaction sv; 
 
  memset(&sv, 0, sizeof(struct sigaction));
  sv.sa_flags = 0;
  sigemptyset(&sv.sa_mask);
/*#ifdef SA_NOCLDWAIT
  sv.sa_flags |= SA_NOCLDWAIT;
#endif
#ifdef SA_NOCLDSTOP
  sv.sa_flags |= SA_NOCLDSTOP;
#endif*/
 
  sv.sa_handler = SIG_IGN;
  /* Don't want broken pipes to kill the hub.  */
  sigaction(SIGPIPE, &sv, NULL);
 
  /* ...or any defunct child processes.  */
  sigaction(SIGCHLD, &sv, NULL);
 
  sv.sa_handler = term_signal;
 
  /* Also, shut down properly.  */
  sigaction(SIGTERM, &sv, NULL);
  sigaction(SIGINT, &sv, NULL);
 
  sv.sa_handler = alarm_signal;
 
  /* And set handler for the alarm call.  */
  sigaction(SIGALRM, &sv, NULL);
#endif
  return 1;
}


int init ()
{
    int ret = 0;

    ret += init_config ();
    ret += init_sig ();
    ret += init_commands ();
    ret += init_accounts ();
    ret += init_trigger ();
    ret += init_ban ();
    ret += init_chatroom ();
    ret += init_hublist ();
    ret += init_plugin ();
    ret += init_rights ();
#ifdef LUA
    ret += init_lua ();
#endif   

    char configfile [] = (DEFAULT_HUBFILE);

    /* We need load config at last, after initialization of all */
    config_load (configfile);

    /* Create main socket */
    ret += init_socket ();

    if (ret <= 0) {        // ERROR in initialization ??
       
        cerr << "> ERROR -> init ()" << endl;
        ret = 1;
        }

    return ret;
}

/*************************************************************\
|              MAIN THREAD                      |
\*************************************************************/

int main (int argc, char *argv[])
{
  if (arguments (argc, argv) != 0)
    exit (1);

  if (init () <= 0)
    cont = 0;

  for ( ; cont ; )    // MAIN LOOP
  {

    loop ();

  }

  cout << "> Shutdown" << endl;
  close(mainSocket);

  return 0;
}


uBiT 25Dec2009 23:15

Re: Compiling
 
142line: write (lockfd, pid, strlen (pid));

161line: if (!strcmp (argv[1], "-d")) {

166line: if (!strcmp (argv[1], "-a")) {

202line: strcpy (filename, DEFAULT_ACCOUNTSFILE);

258line: memset(&sv, 0, sizeof(struct sigaction));

Gene Poole 26Dec2009 02:23

Re: Compiling
 
add:

Code:

#include <string.h>  //for Std C string functions

uBiT 26Dec2009 12:21

Re: Compiling
 
More errors...
Code:

ubyc@ubyc-desktop:~/Downloads/0.0.5.6$ g++ main.cpp
/tmp/cczgaybI.o: In function `arguments(int, char**)':
main.cpp:(.text+0x328): undefined reference to `crypt'
main.cpp:(.text+0x3c5): undefined reference to `crypt'
/tmp/cczgaybI.o: In function `term_signal(int)':
main.cpp:(.text+0x584): undefined reference to `user_list'
main.cpp:(.text+0x5a6): undefined reference to `user_list'
/tmp/cczgaybI.o: In function `init()':
main.cpp:(.text+0x6d0): undefined reference to `init_config()'
main.cpp:(.text+0x6e0): undefined reference to `init_commands()'
main.cpp:(.text+0x6e8): undefined reference to `init_accounts()'
main.cpp:(.text+0x6f0): undefined reference to `init_trigger()'
main.cpp:(.text+0x6f8): undefined reference to `init_ban()'
main.cpp:(.text+0x700): undefined reference to `init_chatroom()'
main.cpp:(.text+0x708): undefined reference to `init_hublist()'
main.cpp:(.text+0x710): undefined reference to `init_plugin()'
main.cpp:(.text+0x718): undefined reference to `init_rights()'
main.cpp:(.text+0x720): undefined reference to `init_lua()'
main.cpp:(.text+0x73f): undefined reference to `config_load(char*)'
main.cpp:(.text+0x744): undefined reference to `init_socket()'
/tmp/cczgaybI.o: In function `main':
main.cpp:(.text+0x7dd): undefined reference to `loop()'
main.cpp:(.text+0x814): undefined reference to `mainSocket'
collect2: ld returned 1 exit status


Gene Poole 26Dec2009 21:52

Re: Compiling
 
First of all, you've got a whole list of externs near the top:

Code:

extern user_t user_list;
extern command_t command_list;
extern config_t config;
extern account_t accounts_list;
extern config_elem_t config_list;

extern int port;                    // ID of port
extern int mainSocket;              // Main socket

so those are defined in some other module. What that module is I can't say because I didn't write this code and I can't read the mind of the person(s) that did.

Second, crypt has been deprecated for some years now in Unix systems because of security issues. It is most likely unimplemented on your system.


All times are GMT +5.5. The time now is 19:34.